diff --git a/apps/docs/content/(array)/1-two-sum.mdx b/apps/docs/content/(array)/1-two-sum.mdx index 6c7753f..20bded5 100644 --- a/apps/docs/content/(array)/1-two-sum.mdx +++ b/apps/docs/content/(array)/1-two-sum.mdx @@ -32,6 +32,20 @@ Can you come up with an algorithm that is less than O(n^2) time complexity? - `-10^9 <= target <= 10^9` - Only one valid answer exists. +## Approach + +```mermaid +flowchart TD + S(["twoSum(nums, target)"]) --> I["seen = {} — value to index"] + I --> L{"more (i, num) in nums?"} + L -- no --> E(["return []"]) + L -- yes --> C["complement = target - num"] + C --> H{"complement in seen?"} + H -- yes --> R(["return [seen[complement], i]"]) + H -- no --> W["seen[num] = i"] + W --> L +``` + ## Solution ```py @@ -45,3 +59,7 @@ class Solution: seen[num] = i return [] ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/11-container-with-most-water.mdx b/apps/docs/content/(array)/11-container-with-most-water.mdx index 0b01c2e..8695ca8 100644 --- a/apps/docs/content/(array)/11-container-with-most-water.mdx +++ b/apps/docs/content/(array)/11-container-with-most-water.mdx @@ -27,6 +27,22 @@ Notice that you may not slant the container. - `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 @@ -46,3 +62,7 @@ class Solution: return res ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/121-best-time-to-buy-and-sell-stock.mdx b/apps/docs/content/(array)/121-best-time-to-buy-and-sell-stock.mdx index d9c2243..75c980c 100644 --- a/apps/docs/content/(array)/121-best-time-to-buy-and-sell-stock.mdx +++ b/apps/docs/content/(array)/121-best-time-to-buy-and-sell-stock.mdx @@ -23,6 +23,24 @@ sidebar: - `1 <= prices.length <= 10^5` - `0 <= prices[i] <= 10^4` +## Approach + +::::warning +The diagram traces the code below literally. That code does not run: `curr` and `ans` are never initialised, so the first `curr > left` test raises `NameError`, and `left` holds a price rather than an index, so `prices[left]` indexes by value. +:::: + +```mermaid +flowchart TD + S(["maxProfit(prices)"]) --> I["left = min(prices)"] + I --> F{"more right in range(len(prices))?"} + F -- no --> E(["return ans"]) + F -- yes --> W{"curr > left?"} + W -- yes --> P["curr -= prices[left]; left += 1"] + P --> W + W -- no --> M["ans = max(ans, curr)"] + M --> F +``` + ## Solution ```py @@ -36,3 +54,7 @@ class Solution: ans = max(ans, curr) return ans ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/1365-how-many-numbers-are-smaller-than-the-current-number.mdx b/apps/docs/content/(array)/1365-how-many-numbers-are-smaller-than-the-current-number.mdx index e250cd2..7d83301 100644 --- a/apps/docs/content/(array)/1365-how-many-numbers-are-smaller-than-the-current-number.mdx +++ b/apps/docs/content/(array)/1365-how-many-numbers-are-smaller-than-the-current-number.mdx @@ -26,6 +26,20 @@ sidebar: - `2 <= nums.length <= 500` - `0 <= nums[i] <= 100` +## Approach + +```mermaid +flowchart TD + S(["smallerNumbersThanCurrent(nums)"]) --> F["freq: count every value in nums"] + F --> O["sorted = keys of freq, ascending"] + O --> I["count = 0, smaller = {}"] + I --> L{"more num in sorted?"} + L -- yes --> A["smaller[num] = count — everything already passed is smaller"] + A --> B["count += freq[num]"] + B --> L + L -- no --> M(["return nums.map(n => smaller[n])"]) +``` + ## Solution ```js diff --git a/apps/docs/content/(array)/1413-minimum-value-to-get-positive-step-by-step-sum.mdx b/apps/docs/content/(array)/1413-minimum-value-to-get-positive-step-by-step-sum.mdx index 5d4bf8b..fde0a7a 100644 --- a/apps/docs/content/(array)/1413-minimum-value-to-get-positive-step-by-step-sum.mdx +++ b/apps/docs/content/(array)/1413-minimum-value-to-get-positive-step-by-step-sum.mdx @@ -37,6 +37,18 @@ startValue = 4 | startValue = 5 | nums - `1 <= nums.length <= 100` - `-100 <= nums[i] <= 100` +## Approach + +```mermaid +flowchart TD + S(["minStartValue(nums)"]) --> I["prefix = [nums[0]]"] + I --> L{"more i in 1..n-1?"} + L -- yes --> P["prefix.push(prefix[i-1] + nums[i])"] + P --> L + L -- no --> M["min = smallest running total"] + M --> R(["return max(1, 1 - min) — lift the dip to 1"]) +``` + ## Solution ```js diff --git a/apps/docs/content/(array)/1426-counting-elements.mdx b/apps/docs/content/(array)/1426-counting-elements.mdx index ae081da..1377109 100644 --- a/apps/docs/content/(array)/1426-counting-elements.mdx +++ b/apps/docs/content/(array)/1426-counting-elements.mdx @@ -23,6 +23,19 @@ sidebar: - `1 <= arr.length <= 1000` - `0 <= arr[i] <= 1000` +## Approach + +```mermaid +flowchart TD + S(["countElements(arr)"]) --> I["arrSet = Set(arr), count = 0"] + I --> L{"more n in arr?"} + L -- no --> E(["return count"]) + L -- yes --> H{"arrSet has n + 1?"} + H -- yes --> C["count++"] + C --> L + H -- no --> L +``` + ## Solution ```js diff --git a/apps/docs/content/(array)/1480-running-sum-of-1d-array.mdx b/apps/docs/content/(array)/1480-running-sum-of-1d-array.mdx index a3aef67..3906a0d 100644 --- a/apps/docs/content/(array)/1480-running-sum-of-1d-array.mdx +++ b/apps/docs/content/(array)/1480-running-sum-of-1d-array.mdx @@ -27,6 +27,17 @@ sidebar: - `1 <= nums.length <= 1000` - `-10^6 <= nums[i] <= 10^6` +## Approach + +```mermaid +flowchart TD + S(["runningSum(nums)"]) --> I["prefix = [nums[0]]"] + I --> L{"more i in 1..n-1?"} + L -- yes --> P["prefix.push(prefix[i-1] + nums[i])"] + P --> L + L -- no --> E(["return prefix"]) +``` + ## Solution ```js diff --git a/apps/docs/content/(array)/15-3sum.mdx b/apps/docs/content/(array)/15-3sum.mdx index 9ca8ec4..92e1a94 100644 --- a/apps/docs/content/(array)/15-3sum.mdx +++ b/apps/docs/content/(array)/15-3sum.mdx @@ -32,6 +32,28 @@ Notice that the solution set must not contain duplicate triplets. - `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 @@ -76,3 +98,7 @@ class Solution: return result ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/150-evaluate-reverse-polish-notation.mdx b/apps/docs/content/(array)/150-evaluate-reverse-polish-notation.mdx index 04a5134..42eb2ec 100644 --- a/apps/docs/content/(array)/150-evaluate-reverse-polish-notation.mdx +++ b/apps/docs/content/(array)/150-evaluate-reverse-polish-notation.mdx @@ -28,6 +28,21 @@ sidebar: - `1 <= tokens.length <= 10^4` - `tokens[i]` is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200]. +## Approach + +```mermaid +flowchart TD + S(["evalRPN(tokens)"]) --> I["stack = [], operations = + - * /"] + I --> L{"more token c?"} + L -- no --> E(["return stack[0]"]) + L -- yes --> Q{"c is an operator?"} + Q -- no --> N["stack.append(int(c))"] + N --> L + Q -- yes --> P["y = stack.pop(), x = stack.pop() — order matters"] + P --> C["stack.append(operations[c](x, y))"] + C --> L +``` + ## Solution ```py @@ -53,3 +68,7 @@ class Solution: return stack[0] ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/153-find-minimum-in-rotated-sorted-array.mdx b/apps/docs/content/(array)/153-find-minimum-in-rotated-sorted-array.mdx index e4bc0b8..f285bb5 100644 --- a/apps/docs/content/(array)/153-find-minimum-in-rotated-sorted-array.mdx +++ b/apps/docs/content/(array)/153-find-minimum-in-rotated-sorted-array.mdx @@ -35,6 +35,21 @@ Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in - All the integers of `nums` are unique. - `nums` is sorted and rotated between 1 and `n` times. +## Approach + +```mermaid +flowchart TD + S(["findMin(nums)"]) --> I["l = 0, r = n-1, lowest_index = -1"] + I --> W{"l <= r?"} + W -- no --> E(["return nums[lowest_index]"]) + W -- yes --> M["m = (l + r) // 2"] + M --> C{"nums[m] <= nums[-1]?"} + C -- yes --> A["in the right sorted run: record lowest_index = m, r = m - 1"] + A --> W + C -- no --> B["still in the left sorted run: l = m + 1"] + B --> W +``` + ## Solution ```py @@ -53,3 +68,7 @@ class Solution: return nums[lowest_index] ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/1636-sort-array-by-increasing-frequency.mdx b/apps/docs/content/(array)/1636-sort-array-by-increasing-frequency.mdx index eafaf3a..20d1edc 100644 --- a/apps/docs/content/(array)/1636-sort-array-by-increasing-frequency.mdx +++ b/apps/docs/content/(array)/1636-sort-array-by-increasing-frequency.mdx @@ -27,6 +27,18 @@ sidebar: - `1 <= nums.length <= 100` - `-100 <= nums[i] <= 100` +## Approach + +```mermaid +flowchart TD + S(["frequencySort(nums)"]) --> F["freq: count every value in nums"] + F --> C["sort nums with a two-key comparator"] + C --> K1["primary: freq[a] - freq[b] — rarer first"] + C --> K2["tie-break: b - a — larger value first"] + K1 --> R(["return nums"]) + K2 --> R +``` + ## Solution ```js @@ -40,3 +52,7 @@ var frequencySort = function (nums) { return nums.sort((a, b) => freq[a] - freq[b] || b - a); }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/167-two-sum-ii-input-array-is-sorted.mdx b/apps/docs/content/(array)/167-two-sum-ii-input-array-is-sorted.mdx index 0853ffd..16922ae 100644 --- a/apps/docs/content/(array)/167-two-sum-ii-input-array-is-sorted.mdx +++ b/apps/docs/content/(array)/167-two-sum-ii-input-array-is-sorted.mdx @@ -35,6 +35,22 @@ Your solution must use only constant extra space. - `-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 @@ -53,3 +69,7 @@ class Solution: j-=1 return [] ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/217-contains-duplicate.mdx b/apps/docs/content/(array)/217-contains-duplicate.mdx index bde42cd..ea8ff7e 100644 --- a/apps/docs/content/(array)/217-contains-duplicate.mdx +++ b/apps/docs/content/(array)/217-contains-duplicate.mdx @@ -27,6 +27,19 @@ sidebar: - `1 <= nums.length <= 10^5` - `-10^9 <= nums[i] <= 10^9` +## Approach + +```mermaid +flowchart TD + S(["containsDuplicate(nums)"]) --> I["freq = {}"] + I --> L{"more n in nums?"} + L -- no --> E(["return false"]) + L -- yes --> C["freq[n] = (freq[n] or 0) + 1"] + C --> Q{"freq[n] >= 2?"} + Q -- yes --> R(["return true"]) + Q -- no --> L +``` + ## Solution ```js @@ -45,3 +58,7 @@ var containsDuplicate = function(nums) { return false; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/238-product-of-array-except-self.mdx b/apps/docs/content/(array)/238-product-of-array-except-self.mdx index 647c1ea..3e622d3 100644 --- a/apps/docs/content/(array)/238-product-of-array-except-self.mdx +++ b/apps/docs/content/(array)/238-product-of-array-except-self.mdx @@ -27,6 +27,17 @@ Can you solve the problem in O(1) extra space complexity? (The output array does - `-30 <= nums[i] <= 30` - The input is generated such that `answer[i]` is guaranteed to fit in a 32-bit integer. +## Approach + +```mermaid +flowchart TD + S(["productExceptSelf(nums)"]) --> I["answer = ones(n), rightArr = ones(n)"] + I --> P1["Pass 1 — left to right: answer[i] = nums[i-1] * answer[i-1]"] + P1 --> P2["Pass 2 — right to left: rightArr[i] = nums[i+1] * rightArr[i+1]"] + P2 --> P3["Pass 3: answer[i] *= rightArr[i]"] + P3 --> E(["return answer — no division needed"]) +``` + ## Solution ```js @@ -51,3 +62,7 @@ var productExceptSelf = function(nums) { return answer; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/268-missing-number.mdx b/apps/docs/content/(array)/268-missing-number.mdx index 36100cf..7309442 100644 --- a/apps/docs/content/(array)/268-missing-number.mdx +++ b/apps/docs/content/(array)/268-missing-number.mdx @@ -34,6 +34,18 @@ Could you implement a solution using only O(1) extra space complexity and O(n) r - `0 <= nums[i] <= n` - All the numbers of `nums` are unique. +## Approach + +```mermaid +flowchart TD + S(["missingNumber(nums)"]) --> I["numSet = Set(nums), expectedCount = n + 1"] + I --> L{"more i in 0..expectedCount-1?"} + L -- yes --> Q{"numSet has i?"} + Q -- no --> R(["return i"]) + Q -- yes --> L + L -- no --> E(["return -1"]) +``` + ## Solution ```js @@ -53,3 +65,7 @@ var missingNumber = function(nums) { return -1; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/303-range-sum-query-immutable.mdx b/apps/docs/content/(array)/303-range-sum-query-immutable.mdx index bbf2916..821498f 100644 --- a/apps/docs/content/(array)/303-range-sum-query-immutable.mdx +++ b/apps/docs/content/(array)/303-range-sum-query-immutable.mdx @@ -20,6 +20,21 @@ sidebar: - `0 <= left <= right < nums.length` - At most `10^4` calls will be made to `sumRange`. +## Approach + +```mermaid +flowchart TD + subgraph build["constructor(nums) — O(n) once"] + A["prefix = [0]"] --> B{"more n in nums?"} + B -- yes --> C["prefix.push(last + n)"] + C --> B + end + build --> Q + subgraph Q["sumRange(left, right) — O(1) per call"] + D(["return prefix[right + 1] - prefix[left]"]) + end +``` + ## Solution ```js @@ -50,3 +65,7 @@ class NumArray { * var param_1 = obj.sumRange(left,right) */ ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/33-search-in-rotated-sorted-array.mdx b/apps/docs/content/(array)/33-search-in-rotated-sorted-array.mdx index 185460a..81515a0 100644 --- a/apps/docs/content/(array)/33-search-in-rotated-sorted-array.mdx +++ b/apps/docs/content/(array)/33-search-in-rotated-sorted-array.mdx @@ -32,6 +32,29 @@ You must write an algorithm with O(log n) runtime complexity. - `nums` is an ascending array that is possibly rotated. - `-10^4 <= target <= 10^4` +## Approach + +```mermaid +flowchart TD + S(["search(nums, target)"]) --> I["l = 0, r = n - 1"] + I --> W{"l <= r?"} + W -- no --> E(["return -1"]) + W -- yes --> M["mid = floor((l + r) / 2)"] + M --> F{"nums[mid] == target?"} + F -- yes --> R(["return mid"]) + F -- no --> H{"nums[l] <= nums[mid]? — left half sorted"} + H -- yes --> A{"target outside [nums[l], nums[mid]]?"} + A -- yes --> A1["l = mid + 1 — search the right half"] + A -- no --> A2["r = mid - 1"] + H -- no --> B{"target outside [nums[mid], nums[r]]?"} + B -- yes --> B1["r = mid - 1 — search the left half"] + B -- no --> B2["l = mid + 1"] + A1 --> W + A2 --> W + B1 --> W + B2 --> W +``` + ## Solution ```js @@ -69,3 +92,7 @@ var search = function(nums, target) { return -1; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/347-top-k-frequent-elements.mdx b/apps/docs/content/(array)/347-top-k-frequent-elements.mdx index cf4d31c..bf9b30c 100644 --- a/apps/docs/content/(array)/347-top-k-frequent-elements.mdx +++ b/apps/docs/content/(array)/347-top-k-frequent-elements.mdx @@ -31,6 +31,16 @@ Your algorithm's time complexity must be better than O(n log n), where n is the - `k` is in the range `[1, the number of unique elements in the array]`. - It is guaranteed that the answer is unique. +## Approach + +```mermaid +flowchart TD + S(["topKFrequent(nums, k)"]) --> F["freq: count every value in nums"] + F --> K["keys of freq, cast to Number"] + K --> O["sort by freq[b] - freq[a] — most frequent first"] + O --> R(["return slice(0, k)"]) +``` + ## Solution ```js @@ -49,3 +59,7 @@ var topKFrequent = function (nums, k) { .slice(0, k); }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/35-search-insert-position.mdx b/apps/docs/content/(array)/35-search-insert-position.mdx index 889e15a..ed458c2 100644 --- a/apps/docs/content/(array)/35-search-insert-position.mdx +++ b/apps/docs/content/(array)/35-search-insert-position.mdx @@ -31,6 +31,22 @@ You must write an algorithm with O(log n) runtime complexity. - `nums` contains distinct values sorted in ascending order. - `-10^4 <= target <= 10^4` +## Approach + +```mermaid +flowchart TD + S(["searchInsert(nums, target)"]) --> I["l = 0, r = n - 1"] + I --> W{"l <= r?"} + W -- yes --> M["m = (l + r) // 2"] + M --> Q{"nums[m] vs target"} + Q -- equal --> R(["return m"]) + Q -- "nums[m] < target" --> L["l = m + 1"] + L --> W + Q -- "nums[m] > target" --> H["r = m - 1"] + H --> W + W -- no --> E(["return (l + r) // 2 + 1 — the insert slot"]) +``` + ## Solution ```py @@ -49,3 +65,7 @@ class Solution: return (l + r) // 2 + 1 ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/49-group-anagrams.mdx b/apps/docs/content/(array)/49-group-anagrams.mdx index 75c52bc..a6730ca 100644 --- a/apps/docs/content/(array)/49-group-anagrams.mdx +++ b/apps/docs/content/(array)/49-group-anagrams.mdx @@ -27,6 +27,21 @@ sidebar: - `0 <= strs[i].length <= 100` - `strs[i]` consists of lowercase English letters. +## Approach + +```mermaid +flowchart TD + S(["groupAnagrams(strs)"]) --> K["sorted[i] = letters of strs[i], sorted — the anagram key"] + K --> I["anagrams = {}"] + I --> L{"more i in 0..n-1?"} + L -- no --> E(["return Object.values(anagrams)"]) + L -- yes --> Q{"anagrams has sorted[i]?"} + Q -- no --> N["anagrams[sorted[i]] = [strs[i]]"] + Q -- yes --> P["anagrams[sorted[i]].push(strs[i])"] + N --> L + P --> L +``` + ## Solution ```js @@ -49,3 +64,7 @@ var groupAnagrams = function(strs) { return Object.values(anagrams); }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/560-subarray-sum-equals-k.mdx b/apps/docs/content/(array)/560-subarray-sum-equals-k.mdx index 2678d07..5e67546 100644 --- a/apps/docs/content/(array)/560-subarray-sum-equals-k.mdx +++ b/apps/docs/content/(array)/560-subarray-sum-equals-k.mdx @@ -22,6 +22,21 @@ sidebar: - `-1000 <= nums[i] <= 1000` - `-10^7 <= k <= 10^7` +## Approach + +```mermaid +flowchart TD + S(["subarraySum(nums, k)"]) --> I["map = {0: 1} — the empty prefix; sum = 0, count = 0"] + I --> L{"more n in nums?"} + L -- no --> E(["return count"]) + L -- yes --> A["sum += n"] + A --> Q{"map has sum - k?"} + Q -- yes --> C["count += map.get(sum - k) — every earlier prefix that closes a window"] + Q -- no --> W["map.set(sum, count of sum + 1)"] + C --> W + W --> L +``` + ## Solution ```js @@ -49,3 +64,7 @@ var subarraySum = function(nums, k) { return count }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/704-binary-search.mdx b/apps/docs/content/(array)/704-binary-search.mdx index f92972b..513d1c7 100644 --- a/apps/docs/content/(array)/704-binary-search.mdx +++ b/apps/docs/content/(array)/704-binary-search.mdx @@ -29,6 +29,22 @@ You must write an algorithm with O(log n) runtime complexity. - All the integers in `nums` are unique. - `nums` is sorted in ascending order. +## Approach + +```mermaid +flowchart TD + S(["search(nums, target)"]) --> I["left = 0, right = n - 1"] + I --> W{"left <= right?"} + W -- no --> E(["return -1"]) + W -- yes --> M["mid = left + floor((right - left) / 2)"] + M --> Q{"nums[mid] vs target"} + Q -- equal --> R(["return mid"]) + Q -- "nums[mid] < target" --> L["left = mid + 1"] + L --> W + Q -- "nums[mid] > target" --> H["right = mid - 1"] + H --> W +``` + ## Solution ```js @@ -54,3 +70,7 @@ var search = function(nums, target) { return -1; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/739-daily-temperatures.mdx b/apps/docs/content/(array)/739-daily-temperatures.mdx index 56ccefb..2b8e63a 100644 --- a/apps/docs/content/(array)/739-daily-temperatures.mdx +++ b/apps/docs/content/(array)/739-daily-temperatures.mdx @@ -25,6 +25,20 @@ sidebar: - `1 <= temperatures.length <= 10^5` - `30 <= temperatures[i] <= 100` +## Approach + +```mermaid +flowchart TD + S(["dailyTemperatures(temperatures)"]) --> I["ans = zeros(n), stack = [] — indices of days still waiting"] + I --> L{"more (i, temp)?"} + L -- no --> E(["return ans"]) + L -- yes --> W{"stack non-empty and temperatures[stack[-1]] < temp?"} + W -- yes --> P["prev = stack.pop(); ans[prev] = i - prev — today resolves that day"] + P --> W + W -- no --> A["stack.append(i)"] + A --> L +``` + ## Solution ```py @@ -39,3 +53,7 @@ class Solution: stack.append(i) return ans ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/74-search-a-2d-matrix.mdx b/apps/docs/content/(array)/74-search-a-2d-matrix.mdx index 3af2355..d53794c 100644 --- a/apps/docs/content/(array)/74-search-a-2d-matrix.mdx +++ b/apps/docs/content/(array)/74-search-a-2d-matrix.mdx @@ -27,6 +27,32 @@ You must write a solution in O(log(m * n)) time complexity. - `1 <= m, n <= 100` - `-10^4 <= matrix[i][j], target <= 10^4` +## Approach + +```mermaid +flowchart TD + S(["searchMatrix(matrix, target)"]) --> I["top = 0, bot = ROWS - 1"] + I --> W1{"top <= bot?"} + W1 -- yes --> R1["row = (top + bot) // 2"] + R1 --> C1{"target vs that row's range"} + C1 -- "above matrix[row][-1]" --> U["top = row + 1"] + C1 -- "below matrix[row][0]" --> V["bot = row - 1"] + C1 -- inside --> B(["break — row found"]) + U --> W1 + V --> W1 + W1 -- no --> F(["return False — no row can hold it"]) + B --> P["row = (top + bot) // 2; l = 0, r = COLS - 1"] + P --> W2{"l <= r?"} + W2 -- no --> G(["return False"]) + W2 -- yes --> M["m = (l + r) // 2"] + M --> C2{"target vs matrix[row][m]"} + C2 -- greater --> X["l = m + 1"] + C2 -- smaller --> Y["r = m - 1"] + C2 -- equal --> T(["return True"]) + X --> W2 + Y --> W2 +``` + ## Solution ```py @@ -59,3 +85,7 @@ class Solution: return True return False ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/853-car-fleet.mdx b/apps/docs/content/(array)/853-car-fleet.mdx index f05f906..89068d1 100644 --- a/apps/docs/content/(array)/853-car-fleet.mdx +++ b/apps/docs/content/(array)/853-car-fleet.mdx @@ -32,6 +32,22 @@ sidebar: - All the values of `position` are unique. - `0 < speed[i] <= 10^6` +## Approach + +```mermaid +flowchart TD + S(["carFleet(target, position, speed)"]) --> O["zip and sort by position, descending — nearest the target first"] + O --> I["stack = [] — arrival time of each fleet's leader"] + I --> L{"more (pos, spd)?"} + L -- no --> E(["return len(stack) — one entry per fleet"]) + L -- yes --> T["time = (target - pos) / spd"] + T --> Q{"stack empty or time > stack[-1]?"} + Q -- yes --> A["stack.append(time) — slower, so it starts a new fleet"] + Q -- no --> B["discard — it catches the car ahead and joins that fleet"] + A --> L + B --> L +``` + ## Solution ```py @@ -45,3 +61,7 @@ class Solution: return len(stack) ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/875-koko-eating-bananas.mdx b/apps/docs/content/(array)/875-koko-eating-bananas.mdx index 7682605..721f566 100644 --- a/apps/docs/content/(array)/875-koko-eating-bananas.mdx +++ b/apps/docs/content/(array)/875-koko-eating-bananas.mdx @@ -26,6 +26,22 @@ sidebar: - `piles.length <= h <= 10^9` - `1 <= piles[i] <= 10^9` +## Approach + +```mermaid +flowchart TD + S(["minEatingSpeed(piles, h)"]) --> I["l = 1, r = max(piles)"] + I --> W{"l < r?"} + W -- no --> E(["return l — the smallest workable speed"]) + W -- yes --> M["mid = floor((l + r) / 2)"] + M --> K["kWorks(mid): hours = sum of ceil(p / mid)"] + K --> Q{"hours <= h?"} + Q -- yes --> A["r = mid — keep mid, try slower"] + Q -- no --> B["l = mid + 1 — too slow, speed up"] + A --> W + B --> W +``` + ## Solution ```js @@ -60,3 +76,7 @@ var minEatingSpeed = function(piles, h) { return l; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(array)/977-squares-of-a-sorted-array.mdx b/apps/docs/content/(array)/977-squares-of-a-sorted-array.mdx index a79dd32..a94ca09 100644 --- a/apps/docs/content/(array)/977-squares-of-a-sorted-array.mdx +++ b/apps/docs/content/(array)/977-squares-of-a-sorted-array.mdx @@ -27,6 +27,21 @@ Squaring each element and sorting the new array is very trivial, could you find - `-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 @@ -53,3 +68,7 @@ var sortedSquares = function(nums) { return ans; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/141-linked-list-cycle.mdx b/apps/docs/content/(hash-table)/141-linked-list-cycle.mdx index 996012b..f1413d9 100644 --- a/apps/docs/content/(hash-table)/141-linked-list-cycle.mdx +++ b/apps/docs/content/(hash-table)/141-linked-list-cycle.mdx @@ -33,6 +33,20 @@ Can you solve it using O(1) (i.e. constant) memory? - `-10^5 <= Node.val <= 10^5` - pos is -1 or a valid index in the linked-list. +## Approach + +```mermaid +flowchart TD + S(["hasCycle(head)"]) --> I["fast = slow = head"] + I --> W{"fast and fast.next?"} + W -- no --> E(["return False — ran off the end, no cycle"]) + W -- yes --> A["fast = fast.next.next — two steps"] + A --> B["slow = slow.next — one step"] + B --> Q{"fast is slow?"} + Q -- yes --> R(["return True — the gap closed, so it loops"]) + Q -- no --> W +``` + ## Solution ```py @@ -54,3 +68,7 @@ class Solution: return True return False ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/146-lru-cache.mdx b/apps/docs/content/(hash-table)/146-lru-cache.mdx index 60b72e0..53f4791 100644 --- a/apps/docs/content/(hash-table)/146-lru-cache.mdx +++ b/apps/docs/content/(hash-table)/146-lru-cache.mdx @@ -20,6 +20,31 @@ sidebar: - `0 <= value <= 10^5` - At most 2 * 10^5 calls will be made to get and put. +## Approach + +```mermaid +flowchart TD + subgraph state["state — dict for O(1) lookup, doubly linked list for O(1) reorder"] + H["head — least recent"] --- N["... nodes ..."] --- T["tail — most recent"] + end + subgraph get["get(key)"] + G1{"key in cache?"} -- no --> G2(["return -1"]) + G1 -- yes --> G3["_remove_node then _add_node — move it beside the tail"] + G3 --> G4(["return node.value"]) + end + subgraph put["put(key, value)"] + P1{"key in cache?"} -- yes --> P2["update value, _remove_node then _add_node"] + P1 -- no --> P3["new Node, cache[key] = node, _add_node"] + P3 --> P4{"len(cache) > capacity?"} + P4 -- yes --> P5["evict head.next — the least recent — and del cache[lru.key]"] + P4 -- no --> P6(["done"]) + P2 --> P6 + P5 --> P6 + end + state --> get + state --> put +``` + ## Solution ```py @@ -84,3 +109,7 @@ class LRUCache: # param_1 = obj.get(key) # obj.put(key,value) ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/1832-check-if-the-sentence-is-pangram.mdx b/apps/docs/content/(hash-table)/1832-check-if-the-sentence-is-pangram.mdx index e669c06..954e1c8 100644 --- a/apps/docs/content/(hash-table)/1832-check-if-the-sentence-is-pangram.mdx +++ b/apps/docs/content/(hash-table)/1832-check-if-the-sentence-is-pangram.mdx @@ -22,6 +22,19 @@ sidebar: - `1 <= sentence.length <= 1000` - `sentence` consists of lowercase English letters. +## Approach + +```mermaid +flowchart TD + S(["checkIfPangram(sentence)"]) --> I["freq = {}"] + I --> L{"more char c?"} + L -- yes --> C["freq[c] = (freq[c] or 0) + 1"] + C --> L + L -- no --> Q{"distinct keys in freq == 26?"} + Q -- yes --> R(["return true"]) + Q -- no --> F(["return false"]) +``` + ## Solution ```js diff --git a/apps/docs/content/(hash-table)/242-valid-anagram.mdx b/apps/docs/content/(hash-table)/242-valid-anagram.mdx index dff1203..89ba2fa 100644 --- a/apps/docs/content/(hash-table)/242-valid-anagram.mdx +++ b/apps/docs/content/(hash-table)/242-valid-anagram.mdx @@ -25,6 +25,19 @@ What if the inputs contain Unicode characters? How would you adapt your solution - `1 <= s.length, t.length <= 5 * 10^4` - `s` and `t` consist of lowercase English letters. +## Approach + +```mermaid +flowchart TD + S(["isAnagram(s, t)"]) --> I["freq: count every char of s"] + I --> L{"more char c in t?"} + L -- no --> E(["return true"]) + L -- yes --> Q{"freq[c] missing or 0?"} + Q -- yes --> R(["return false — t has a char s cannot cover"]) + Q -- no --> D["freq[c] -= 1"] + D --> L +``` + ## Solution ```js @@ -46,3 +59,7 @@ var isAnagram = function(s, t) { return true; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/3-longest-substring-without-repeating-characters.mdx b/apps/docs/content/(hash-table)/3-longest-substring-without-repeating-characters.mdx index c5fbe45..72d73d2 100644 --- a/apps/docs/content/(hash-table)/3-longest-substring-without-repeating-characters.mdx +++ b/apps/docs/content/(hash-table)/3-longest-substring-without-repeating-characters.mdx @@ -28,6 +28,21 @@ sidebar: - `0 <= s.length <= 10^5` - `s` consists of English letters, digits, symbols and spaces. +## Approach + +```mermaid +flowchart TD + S(["lengthOfLongestSubstring(s)"]) --> I["left = 0, ans = 0, window = set()"] + I --> L{"more (right, c) in s?"} + L -- no --> E(["return ans"]) + L -- yes --> W{"c already in window?"} + W -- yes --> P["window.remove(s[left]); left += 1 — shrink until c is free"] + P --> W + W -- no --> A["window.add(c)"] + A --> M["ans = max(ans, right - left + 1)"] + M --> L +``` + ## Solution ```py @@ -45,3 +60,7 @@ class Solution: return ans ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/387-first-unique-character-in-a-string.mdx b/apps/docs/content/(hash-table)/387-first-unique-character-in-a-string.mdx index 065a16c..d5f831e 100644 --- a/apps/docs/content/(hash-table)/387-first-unique-character-in-a-string.mdx +++ b/apps/docs/content/(hash-table)/387-first-unique-character-in-a-string.mdx @@ -26,6 +26,18 @@ sidebar: - `1 <= s.length <= 10^5` - `s` consists of only lowercase English letters. +## Approach + +```mermaid +flowchart TD + S(["firstUniqChar(s)"]) --> F["Pass 1 — freq: count every char of s"] + F --> L{"Pass 2 — more i in 0..len-1?"} + L -- no --> E(["return -1"]) + L -- yes --> Q{"freq[s[i]] == 1?"} + Q -- yes --> R(["return i — leftmost wins because the scan is in order"]) + Q -- no --> L +``` + ## Solution ```js @@ -45,3 +57,7 @@ var firstUniqChar = function (s) { return -1; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/424-longest-repeating-character-replacement.mdx b/apps/docs/content/(hash-table)/424-longest-repeating-character-replacement.mdx index c807620..f4deb7d 100644 --- a/apps/docs/content/(hash-table)/424-longest-repeating-character-replacement.mdx +++ b/apps/docs/content/(hash-table)/424-longest-repeating-character-replacement.mdx @@ -24,6 +24,21 @@ sidebar: - `s` consists of only uppercase English letters. - `0 <= k <= s.length` +## Approach + +```mermaid +flowchart TD + S(["characterReplacement(s, k)"]) --> I["count = {}, res = 0, l = 0, maxF = 0"] + I --> L{"more (r, c) in s?"} + L -- no --> E(["return res"]) + L -- yes --> A["count[c] += 1; maxF = max(maxF, count[c])"] + A --> W{"(r - l + 1) - maxF > k? — too many chars to replace"} + W -- yes --> P["count[s[l]] -= 1; l += 1 — shrink from the left"] + P --> W + W -- no --> M["res = max(res, r - l + 1)"] + M --> L +``` + ## Solution ```py @@ -45,3 +60,7 @@ class Solution: res = max(res, r - l + 1) return res ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/451-sort-characters-by-frequency.mdx b/apps/docs/content/(hash-table)/451-sort-characters-by-frequency.mdx index a0415a1..a5d3d30 100644 --- a/apps/docs/content/(hash-table)/451-sort-characters-by-frequency.mdx +++ b/apps/docs/content/(hash-table)/451-sort-characters-by-frequency.mdx @@ -28,6 +28,18 @@ sidebar: - `1 <= s.length <= 5 * 10^5` - `s` consists of uppercase and lowercase English letters and digits. +## Approach + +```mermaid +flowchart TD + S(["frequencySort(s)"]) --> F["freq: count every char of s"] + F --> C["split s and sort with a two-key comparator"] + C --> K1["primary: freq[b] - freq[a] — most frequent first"] + C --> K2["tie-break: a.localeCompare(b) — alphabetical"] + K1 --> R(["join and return"]) + K2 --> R +``` + ## Solution ```js @@ -47,3 +59,7 @@ var frequencySort = function (s) { .join(""); }; ``` + +## Explanation + + diff --git a/apps/docs/content/(hash-table)/567-permutation-in-string.mdx b/apps/docs/content/(hash-table)/567-permutation-in-string.mdx index 3483848..f5e4734 100644 --- a/apps/docs/content/(hash-table)/567-permutation-in-string.mdx +++ b/apps/docs/content/(hash-table)/567-permutation-in-string.mdx @@ -22,6 +22,24 @@ sidebar: - `1 <= s1.length, s2.length <= 10^4` - `s1` and `s2` consist of lowercase English letters. +## Approach + +```mermaid +flowchart TD + S(["checkInclusion(s1, s2)"]) --> G{"k = len(s1) > len(s2)?"} + G -- yes --> X(["return False"]) + G -- no --> I["need = Counter(s1), window = Counter()"] + I --> L{"more (right, c) in s2?"} + L -- no --> E(["return False"]) + L -- yes --> A["window[c] += 1"] + A --> B{"right >= k? — window is now longer than k"} + B -- yes --> C["drop s2[right - k] from window, deleting the key at 0"] + B -- no --> Q + C --> Q{"window == need?"} + Q -- yes --> R(["return True"]) + Q -- no --> L +``` + ## Solution ```py @@ -49,3 +67,7 @@ class Solution: return False ``` + +## Explanation + + diff --git a/apps/docs/content/(linked-list)/206-reverse-linked-list.mdx b/apps/docs/content/(linked-list)/206-reverse-linked-list.mdx index 76f547d..537c3ae 100644 --- a/apps/docs/content/(linked-list)/206-reverse-linked-list.mdx +++ b/apps/docs/content/(linked-list)/206-reverse-linked-list.mdx @@ -29,6 +29,19 @@ A linked list can be reversed either iteratively or recursively. Could you imple - The number of nodes in the list is the range [0, 5000]. - `-5000 <= Node.val <= 5000` +## Approach + +```mermaid +flowchart TD + S(["reverseList(head)"]) --> I["prev = None, curr = head"] + I --> W{"curr?"} + W -- no --> E(["return prev — the old tail is the new head"]) + W -- yes --> A["next_ = curr.next — save it before overwriting"] + A --> B["curr.next = prev — flip the link"] + B --> C["prev = curr; curr = next_ — step forward"] + C --> W +``` + ## Solution ```py @@ -48,3 +61,7 @@ class Solution: curr = next_ return prev ``` + +## Explanation + + diff --git a/apps/docs/content/(linked-list)/21-merge-two-sorted-lists.mdx b/apps/docs/content/(linked-list)/21-merge-two-sorted-lists.mdx index 24f2fbb..d74695e 100644 --- a/apps/docs/content/(linked-list)/21-merge-two-sorted-lists.mdx +++ b/apps/docs/content/(linked-list)/21-merge-two-sorted-lists.mdx @@ -26,6 +26,23 @@ sidebar: - `-100 <= Node.val <= 100` - Both list1 and list2 are sorted in non-decreasing order. +## Approach + +```mermaid +flowchart TD + S(["mergeTwoLists(list1, list2)"]) --> G1{"list1 empty?"} + G1 -- yes --> X1(["return list2"]) + G1 -- no --> G2{"list2 empty?"} + G2 -- yes --> X2(["return list1"]) + G2 -- no --> H["head = the smaller first node; advance that list"] + H --> C["current = head"] + C --> W{"list1 and list2 both non-empty?"} + W -- yes --> P["current.next = the smaller node; advance that list; current = current.next"] + P --> W + W -- no --> T["current.next = list1 or list2 — append the leftover tail"] + T --> E(["return head"]) +``` + ## Solution ```py @@ -64,3 +81,7 @@ class Solution: current.next = list1 or list2 return head ``` + +## Explanation + + diff --git a/apps/docs/content/(stack)/155-min-stack.mdx b/apps/docs/content/(stack)/155-min-stack.mdx index e284439..364b963 100644 --- a/apps/docs/content/(stack)/155-min-stack.mdx +++ b/apps/docs/content/(stack)/155-min-stack.mdx @@ -23,6 +23,28 @@ You must implement a solution with O(1) time complexity for each function. - Methods pop, top and getMin operations will always be called on non-empty stacks. - At most 3 * 10^4 calls will be made to push, pop, top, and getMin. +## Approach + +```mermaid +flowchart TD + subgraph state["two parallel stacks — same depth, always"] + A["stack — the values"] + B["minStack — the minimum as of that depth"] + end + state --> P + subgraph P["push(value)"] + P1["stack.append(value)"] --> P2["minStack.append(min(value, minStack[-1]))"] + end + state --> O + subgraph O["pop()"] + O1["pop both stacks together"] + end + state --> R + subgraph R["top() / getMin() — O(1)"] + R1(["stack[-1] / minStack[-1]"]) + end +``` + ## Solution ```py @@ -54,3 +76,7 @@ class MinStack: # param_3 = obj.top() # param_4 = obj.getMin() ``` + +## Explanation + + diff --git a/apps/docs/content/(string)/20-valid-parentheses.mdx b/apps/docs/content/(string)/20-valid-parentheses.mdx index e26830b..d67f136 100644 --- a/apps/docs/content/(string)/20-valid-parentheses.mdx +++ b/apps/docs/content/(string)/20-valid-parentheses.mdx @@ -33,6 +33,24 @@ sidebar: - `1 <= s.length <= 10^4` - `s` consists of parentheses only '()[]{}'. +## Approach + +```mermaid +flowchart TD + S(["isValid(s)"]) --> I["stack = [], pairs = closer to opener"] + I --> L{"more char c?"} + L -- no --> Z{"stack empty?"} + Z -- yes --> T(["return True"]) + Z -- no --> U(["return False — unclosed openers left"]) + L -- yes --> Q{"c is a closer?"} + Q -- no --> A["stack.append(c) — it is an opener"] + A --> L + Q -- yes --> M{"stack non-empty and pairs[c] == stack[-1]?"} + M -- yes --> P["stack.pop() — matched"] + P --> L + M -- no --> F(["return False — mismatch"]) +``` + ## Solution ```py @@ -52,3 +70,7 @@ class Solution: return True if not stack else False ``` + +## Explanation + + diff --git a/apps/docs/content/(two-pointers)/125-valid-palindrome.mdx b/apps/docs/content/(two-pointers)/125-valid-palindrome.mdx index 027d09f..1237832 100644 --- a/apps/docs/content/(two-pointers)/125-valid-palindrome.mdx +++ b/apps/docs/content/(two-pointers)/125-valid-palindrome.mdx @@ -28,6 +28,20 @@ sidebar: - `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 @@ -49,3 +63,7 @@ var isPalindrome = function(s) { return true; }; ``` + +## Explanation + + diff --git a/apps/docs/content/(two-pointers)/344-reverse-string.mdx b/apps/docs/content/(two-pointers)/344-reverse-string.mdx index 9e94efe..6da31a9 100644 --- a/apps/docs/content/(two-pointers)/344-reverse-string.mdx +++ b/apps/docs/content/(two-pointers)/344-reverse-string.mdx @@ -25,6 +25,18 @@ You must do this by modifying the input array in-place with O(1) extra memory. - `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 @@ -43,3 +55,7 @@ var reverseString = function(s) { } }; ``` + +## Explanation + + diff --git a/apps/docs/content/(two-pointers)/392-is-subsequence.mdx b/apps/docs/content/(two-pointers)/392-is-subsequence.mdx index 36c8b59..66d4087 100644 --- a/apps/docs/content/(two-pointers)/392-is-subsequence.mdx +++ b/apps/docs/content/(two-pointers)/392-is-subsequence.mdx @@ -26,6 +26,22 @@ Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and y - `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 @@ -42,3 +58,7 @@ class Solution: return i == len(s) ``` + +## Explanation + +