Latest Posts

11. Container With Most Water

two pointer You are given an integer array height of length n . There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]) . Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store . Notice that you may not slant the container.   Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1   Constraints: I will solve it using two pointer. one will be placed at the beginning and another at the end. they never cross to ensure that the width of the two height is always > 0. Simply putting, checking all the possible pair and get the max_area of all will help me get the solution, but it will take O(n^2), which is not efficient and causes time limit exceed, so I need to come up with something better. suppose that the right pointer is fixed at the end of the given array. if the left pointer value is lower, we know that this will be the height of the area; conversly what this means is that no matter what the other pointer's height is, the height is still be the height of the pointer. therefore, we want to change the left pointer to get different area. so we increase the left value by one in this case. in the case right pointer height is lower, we lower the right pointer by 1 to get different height. In code, this is below: This coce runs in O(n) using two pointer approach. The code above works, but this is still taking quite some time and people came up with further optimization. Below is one additional improvement made, by utilizing the comparison with potential max area. Initially find the maximum height, after each comparison step, the current area is compared with the potential max area. each comparison will have the varied width and multiplying this value with the max height will get you the potential max area. comparing the local max area with this and if the maximum area is already found, we can do early termination to further reduce the steps. Below is the code utilizing this: This problem requires the property of the question in order to come up with the optimal solution; such as how to iterate the pointers.

11. Container With Most Water

Featured Posts

Code Block example for BlockNote

BlockNote is a opensource Block-Based rich text editor. What you see here is built on BlockNote and it allows to easily integrate text editors into your project. After going through some research, I found that this is a great text editor to use for my project on building my blog for the ease of implementation, as they claimed that 'it just works.' BlockNote came with most of the basic features needed for rich text editor and it has the modern look and feel of the text editor from Notion, one of the famous note-taking app out there, that there is not much additional works needed to make it better. Since my first post about creating my own blog, I soon encountered a significant challenge with BlockNote: it does not support code blocks out of the box. As I will be uploading posts heavy on codebase, having a code block feature was essential. After navigating through various GitHub issues, Discord threads, and countless Google searches, I finally found an approach that worked for me. If you're reading this, I assume you've faced the same hurdle, and I hope my solution can help you too. To make it easier for others, I also created a pull request on BlockNote to add an example on their website, making it more accessible for everyone. If you’ve already set up BlockNote , feel free to skip this section. Otherwise, follow the official to get started. Once set, Let's install necessary packages: After you finish installing, you will have below file structures: Let's create a folder called components and create three files: Once your project is ready, let's remove any default codes on app/page.txt to start with fresh. The code will import BlockNote App from components and load it into the main page. If you have an existing project, just import the App into necessary location. This file will contain the main codebase for setting up BlockNote editor and import custom code block into the editor. Let's import all necessary components first. Since it is a client side component, you need to declare that it's client component at the top of the file. We will then going to create schema for the block specs. We will use the default BlockNote's schema and extend with default schema with the code block. To make the Code block selectable through slash menu, Let's create insertCodeBlock component and define the configuration for the block. The title is the text used with slash menu inside BlockNote. This allows the Code block to appear when you type /code. We are going to assign it into Others group as it's a custom block, and we are going to use the Code icon from Lucide-react package. Additionally we are going to provide subtext which will be appeared below title to give more context on what this block do. Lastly, we are going to create the main App component for the BlockNote and return it so that we can import on other components to show the BlockNote editor. If you finish until now and try running the code, you will see an error message. This is expected and the reason is because we have not implemented components/Code.tsx file. Let's go ahead and implement the Code.tsx file now. This file is the main codebase for the Code block extension. Here, we will use React CodeMirror package to create the code block while let BlockNote to manage the code block content. additionally, we are going to create a drop down option to select which language to use for syntax highlighting. Let's import all the necessary components again on components/Code.tsx file. This will be a client component as well so please include 'use client' at the top of the file. We will now going to create the Code Block. We are going to call this codeBlock . This needs to match with the type above for insertCodeBlock on components/App.tsx file. If you set it to something else other than codeBlock , please make sure that they match. We are going to add two more props and keep track of it: data and language. data - it will be used to keep track of the contents of the editor. language - it will keep track of the currently selected language. Let's set the default value be "javascript," and all available languages can be found from " langNames " from package "@uiw/codemirror-extensions-langs" I also imported 'material' theme from CodeMirror for the editor. If you want to change to different theme, you can find all available styles here: When you are done and run the project with npm run dev , you will be able to see it running on localhost:3000 (by default). with below screen. This editor seems fine to use, but there are some rooms to improve in terms of the styling. Before we go and add the feature to select style, let's go ahead and create styles.css file in the same directory. This file will contains rules on how the editor should be look to you. If you add this file, you will be able to see that the editor's appearance has changed like below. The style looks better then before and we now need to add a feature to select, browse, and search which language to use for syntax highlighting. Since Mantine UI, the component UI library used for BlockNote, already comes with search drop down component, let's use this to list out the available language for syntax highlighting. The search drop down component keeps track of the selected value through React state. Since State cannot used inside BlockNote's render function, we need to create a new component and call this new component inside the render. that way the state can still be used for the component and we can safely use the component to list the available language. Since this component will use the default value for the language and BlockNote need to capture any changes on the language, let's pass the props from BlockNote to this component. To do so, we need to create a type for the props since we are using typescript. we can get the props from BlockNote and we need to extend to add the two props, data and language. I placed below code between import statements and the Code editor component. The default value is set to the BlockNote's default value we set for language , and we update the BlockNote's language when an event to change selected variabled is triggered. After you add the selectDropDownSearch component, you won't see any changes on the editor. This is because I commented out the lines for this component on the editor. Let's scroll down to the Code component, and uncomment below code block and save for the effect to take place. After you successfully follow it all, you will see the working editor with language selecting for syntax highlighting as well. By following the steps outlined in this post, you’ve successfully implemented the code block feature in BlockNote with syntax highlighting using codemirror. With the code block, you can display code snippets into your BlockNote based application, which is one of the crutial feature if the purpose of the app is to cover technical posts. Stay tuned for the next post, where I’ll cover how to change the language for syntax highlighting.

Code Block example for BlockNote

Making My Own Blog - 1.Introduction

In this series, I will walk through the process of building my blog from scratch, all the way to deployment. Each post will detail the steps and decisions involved in developing a web project, with a focus on creating a blog. By following this series, you’ll learn how to build your own blog, as well as key components and considerations when developing software that can be useful for various projects. Rather than using an existing blogging platform, I decided to build my own blog to showcase my skills and projects. This blog serves as a portfolio where I can display my work to others. In the past, I worked at startups, where I developed internal software. Although this experience gave me valuable knowledge, the proprietary nature of these projects meant I couldn't share them with others. As a result, I decided to embark on personal projects that I could fully own and showcase publicly. I chose to start with a blog because it is a manageable project size and offers a platform to document and share future endeavors. Here are the core features I aimed to implement for my blog: For this project, I used technologies I am already familiar with, as the goal is to demonstrate my skills in building web applications. Initially, I considered using Material UI (MUI), but it was in the process of releasing new components based on Material Design v3. Since not all the components I needed were available, I looked for an alternative UI system. I wanted a UI library compatible with Tailwind CSS, which has become a popular choice among developers. I found Shadcn , a well-supported library with many components, an active community, and seamless integration with Next.js and rich text editors. This made it an ideal choice for my project. For the text editor, I needed a modern, feature-rich tool, as writing and displaying posts effectively is a crucial aspect of a blog. Initially, I considered Tiptap , but I hesitated due to its licensing structure and the complexity of implementing every features by myself as it's headless text editor. During further search, I found BlockNote , which offered the functionality I needed without limitations and at no cost, and also I can implement most of the features out of the box. Due to this reason, I chose BlockNote as the main text editor. I wanted to experiment with public and private subnets to control how data flows between the user, website, backend, and database. Below is a simple layout illustrating how I structured the network. The primary difference between public and private subnets is whether they have access to the public internet. In the next posts, I’ll dive into the frontend development, starting with the initial setup. We’ll explore how to implement authentication and authorization using Auth.js , with a focus on integrating OAuth providers like Google and managing user sessions securely. I’ll also cover how to implement role-based access control, ensuring that different users have appropriate permissions. Along the way, we’ll tackle some of the common challenges developers face, such as handling session persistence and protecting routes. Stay tuned and feel free to ask questions as we go through each step in building a fully functional and secure blog platform!

Making My Own Blog - 1.Introduction