chore: clean up content phase organization

This commit is contained in:
Prad Nukala committed 2026-09-24 15:57:38 -04:00
1 parent 403a2d7f23
commit bf0ac34a43
138 files changed
+1436 -368

No files matched your search

@@ -0,0 +1,68 @@
---
title: '11. Container With Most Water'
description: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])
sidebar:
label: 'Container With Most Water'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
::::warning
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:
- `n == height.length`
- `2 <= n <= 10^5`
- `0 <= height[i] <= 10^4`
## Approach
```mermaid
flowchart TD
S(["maxArea(height)"]) --> I["res = 0, left = 0, right = len - 1"]
I --> W{"left < right?"}
W -- no --> E(["return res"])
W -- yes --> A["area = (right - left) * min(height[left], height[right])"]
A --> M["res = max(res, area)"]
M --> C{"height[left] < height[right]?"}
C -- yes --> L["left += 1 — drop the shorter wall"]
C -- no --> R["right -= 1 — drop the shorter wall"]
L --> W
R --> W
```
## Solution
```py
class Solution:
def maxArea(self, height: List[int]) -> int: # noqa: F821
res = 0
left = 0
right = len(height) - 1
while left < right:
area = (right - left) * min(height[left], height[right])
res = max(res, area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return res
```
## Explanation
<YouTube id="UuiTKBwPgAo" title="Container with Most Water - Leetcode 11 - Python" />
@@ -0,0 +1,69 @@
---
title: '125. Valid Palindrome'
description: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers
sidebar:
label: 'Valid Palindrome'
badge: 'Easy'
---
<Badge variant="accent">Two Pointers</Badge>
### Example 1:
- Input: `s = "A man, a plan, a canal: Panama"`
- Output: `true`
- Explanation: `"amanaplanacanalpanama"` is a palindrome.
### Example 2:
- Input: `s = "race a car"`
- Output: `false`
- Explanation: `"raceacar"` is not a palindrome.
### Example 3:
- Input: `s = " "`
- Output: `true`
- Explanation: `s` is an empty string `""` after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
### Constraints:
- `1 <= s.length <= 2 * 10^5`
- `s` consists only of printable ASCII characters.
## Approach
```mermaid
flowchart TD
S(["isPalindrome(s)"]) --> N["normal = s stripped of non-alphanumerics, lowercased"]
N --> I["i = 0, j = normal.length - 1"]
I --> W{"i < j?"}
W -- no --> E(["return true — the pointers met in the middle"])
W -- yes --> Q{"normal[i] != normal[j]?"}
Q -- yes --> F(["return false"])
Q -- no --> A["i++; j-- — step both inward"]
A --> W
```
## Solution
```js
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
let normal = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()
let i = 0, j = normal.length - 1;
while (i < j) {
if(normal[i] !== normal[j]) {
return false;
}
i++;
j--;
}
return true;
};
```
## Explanation
<YouTube id="jJXJ16kPFWg" title="Valid Palindrome - Leetcode 125 - Python" />
@@ -0,0 +1,104 @@
---
title: '15. 3Sum'
description: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0
sidebar:
label: '3Sum'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
::::warning
Notice that the solution set must not contain duplicate triplets.
::::
### Example 1:
- Input: `nums = [-1,0,1,2,-1,-4]`
- Output: `[[-1,-1,2],[-1,0,1]]`
- Explanation: `nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0`. `nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0`. `nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0`. The distinct triplets are `[-1,0,1]` and `[-1,-1,2]`. Notice that the order of the output and the order of the triplets does not matter.
### Example 2:
- Input: `nums = [0,1,1]`
- Output: `[]`
- Explanation: The only possible triplet does not sum up to `0`.
### Example 3:
- Input: `nums = [0,0,0]`
- Output: `[[0,0,0]]`
- Explanation: The only possible triplet sums up to `0`.
### Constraints:
- `3 <= nums.length <= 3000`
- `-10^5 <= nums[i] <= 10^5`
## Approach
```mermaid
flowchart TD
S(["threeSum(nums)"]) --> O["nums.sort() — duplicates become adjacent"]
O --> F{"more i in 0..n-1?"}
F -- no --> E(["return result"])
F -- yes --> D{"nums[i] == nums[i-1]?"}
D -- yes --> F
D -- no --> P["left = i+1, right = n-1, target = -nums[i]"]
P --> W{"left < right?"}
W -- no --> F
W -- yes --> C["current = nums[left] + nums[right]"]
C --> Q{"current vs target"}
Q -- equal --> A["append triplet, skip equal neighbours, left += 1, right -= 1"]
A --> W
Q -- "current < target" --> L["left += 1 — need a bigger sum"]
L --> W
Q -- "current > target" --> R["right -= 1 — need a smaller sum"]
R --> W
```
## Solution
```py
class Solution:
def threeSum(self, nums: list[int]) -> list[list[int]]:
nums.sort()
result = []
n = len(nums)
for i in range(n):
# skip all zero
if i > 0 and nums[i] == nums[i - 1]:
continue
# two pointers
left = i + 1
right = n - 1
target = -nums[i]
while left < right:
current = nums[left] + nums[right]
if current == target:
result.append([nums[i], nums[left], nums[right]])
# skip duplicates
while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
# shift pointers
left += 1
right -= 1
# since sorted, if current < target, then move left
elif current < target:
left += 1
# since sorted, if current > target, then move right
else:
right -= 1
return result
```
## Explanation
<YouTube id="jzZsG8n2R9A" title="3Sum - Leetcode 15 - Python" />
@@ -0,0 +1,75 @@
---
title: '167. Two Sum II - Input Array Is Sorted'
description: Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length
sidebar:
label: 'Two Sum II - Input Array Is Sorted'
badge: 'Medium'
---
<Badge variant="accent">Array</Badge>
::::warning
Your solution must use only constant extra space.
::::
### Example 1:
- Input: `numbers = [2,7,11,15], target = 9`
- Output: `[1,2]`
- Explanation: The sum of `2` and `7` is `9`. Therefore, `index1 = 1`, `index2 = 2`. We return `[1, 2]`.
### Example 2:
- Input: `numbers = [2,3,4], target = 6`
- Output: `[1,3]`
- Explanation: The sum of `2` and `4` is `6`. Therefore `index1 = 1`, `index2 = 3`. We return `[1, 3]`.
### Example 3:
- Input: `numbers = [-1,0], target = -1`
- Output: `[1,2]`
- Explanation: The sum of `-1` and `0` is `-1`. Therefore `index1 = 1`, `index2 = 2`. We return `[1, 2]`.
### Constraints:
- `2 <= numbers.length <= 3 * 10^4`
- `-1000 <= numbers[i] <= 1000`
- `numbers` is sorted in non-decreasing order.
- `-1000 <= target <= 1000`
- The tests are generated such that there is exactly one solution.
## Approach
```mermaid
flowchart TD
S(["twoSum(numbers, target)"]) --> I["i = 0, j = len - 1"]
I --> W{"i < j?"}
W -- no --> E(["return []"])
W -- yes --> C["c = numbers[i] + numbers[j]"]
C --> Q{"c vs target"}
Q -- equal --> R(["return [i+1, j+1] — 1-indexed"])
Q -- "c < target" --> L["i += 1 — grow the sum"]
L --> W
Q -- "c > target" --> H["j -= 1 — shrink the sum"]
H --> W
```
## Solution
```py
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i = 0
j = len(numbers) - 1
while i < j:
c = numbers[i] + numbers[j]
if c == target:
return [i + 1, j + 1]
elif c < target:
i+=1
else:
j-=1
return []
```
## Explanation
<YouTube id="cQ1Oz4ckceM" title="TWO SUM II - Amazon Coding Interview Question - Leetcode 167 - Python" />
@@ -0,0 +1,61 @@
---
title: '344. Reverse String'
description: Write a function that reverses a string. The input string is given as an array of characters s
sidebar:
label: 'Reverse String'
badge: 'Easy'
---
<Badge variant="accent">Two Pointers</Badge>
:::warning
You must do this by modifying the input array in-place with O(1) extra memory.
:::
### Example 1:
- Input: `s = ["h","e","l","l","o"]`
- Output: `["o","l","l","e","h"]`
### Example 2:
- Input: `s = ["H","a","n","n","a","h"]`
- Output: `["h","a","n","n","a","H"]`
### Constraints:
- `1 <= s.length <= 10^5`
- `s[i]` is a printable ascii character.
## Approach
```mermaid
flowchart TD
S(["reverseString(s)"]) --> I["i = 0, j = s.length - 1"]
I --> W{"i < j?"}
W -- no --> E(["done — s was reversed in place, nothing returned"])
W -- yes --> A["swap s[i] and s[j]"]
A --> B["i++; j--"]
B --> W
```
## Solution
```js
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let i = 0;
let j = s.length - 1;
while (i < j) {
[s[i], s[j]] = [s[j], s[i]];
j--;
i++;
}
};
```
## Explanation
<YouTube id="_d0T_2Lk2qA" title="Reverse String - 3 Ways - Leetcode 344 - Python" />
@@ -0,0 +1,64 @@
---
title: '392. Is Subsequence'
description: Given two strings s and t, return true if s is a subsequence of t, or false otherwise
sidebar:
label: 'Is Subsequence'
badge: 'Easy'
---
<Badge variant="accent">Two Pointers</Badge>
::::warning
Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?
::::
### Example 1:
- Input: `s = "abc", t = "ahbgdc"`
- Output: `true`
### Example 2:
- Input: `s = "axc", t = "ahbgdc"`
- Output: `false`
### Constraints:
- `0 <= s.length <= 100`
- `0 <= t.length <= 10^4`
- `s` and `t` consist only of lowercase English letters.
## Approach
```mermaid
flowchart TD
S(["isSubsequence(s, t)"]) --> G{"len(s) > len(t)?"}
G -- yes --> X(["return False"])
G -- no --> I["i = 0 into s, j = 0 into t"]
I --> W{"i < len(s) and j < len(t)?"}
W -- no --> E(["return i == len(s) — every char of s was matched"])
W -- yes --> Q{"s[i] == t[j]?"}
Q -- yes --> A["i += 1 — consume the match"]
Q -- no --> B["j += 1 — always advance t"]
A --> B
B --> W
```
## Solution
```py
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):
return False
i, j = 0, 0
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
return i == len(s)
```
## Explanation
<YouTube id="99RVfqklbCE" title="Is Subsequence - Leetcode 392" />
@@ -0,0 +1,74 @@
---
title: '977. Squares of a Sorted Array'
description: Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order
sidebar:
label: 'Squares of a Sorted Array'
badge: 'Easy'
---
<Badge variant="accent">Array</Badge>
::::warning
Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
::::
### Example 1:
- Input: `nums = [-4,-1,0,3,10]`
- Output: `[0,1,9,16,100]`
- Explanation: After squaring, the array becomes `[16,1,0,9,100]`. After sorting, it becomes `[0,1,9,16,100]`.
### Example 2:
- Input: `nums = [-7,-3,2,3,11]`
- Output: `[4,9,9,49,121]`
### Constraints:
- `1 <= nums.length <= 10^4`
- `-10^4 <= nums[i] <= 10^4`
- `nums` is sorted in non-decreasing order.
## Approach
```mermaid
flowchart TD
S(["sortedSquares(nums)"]) --> I["ans = Array(n), left = 0, right = n - 1"]
I --> L{"more i from n-1 down to 0?"}
L -- no --> E(["return ans"])
L -- yes --> C{"abs(nums[left]) < abs(nums[right])?"}
C -- yes --> R["square = nums[right]; right--"]
C -- no --> F["square = nums[left]; left++"]
R --> W["ans[i] = square * square — fill from the back, largest first"]
F --> W
W --> L
```
## Solution
```js
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function(nums) {
let n = nums.length;
let ans = new Array(nums.length);
let left = 0, right = nums.length - 1;
for (let i = n -1; i >= 0; i--){
let square;
if(Math.abs(nums[left]) < Math.abs(nums[right])){
square = nums[right];
right--;
}else{
square = nums[left];
left++;
}
ans[i] = square*square;
}
return ans;
};
```
## Explanation
<YouTube id="FPCZsG_AkUg" title="Squares of a Sorted Array - Leetcode 977 - Python" />
@@ -0,0 +1,5 @@
---
title: Two Pointers
description: 'Two indices move through the data from different positions and meet at the answer.'
hidden: true
---
@@ -0,0 +1,3 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Two Pointers", order: 3, icon: "arrow-left-right", display: "group" });