feat: populate note files with problem descriptions and code stubs

Add populate-notes.mjs that fetches problem descriptions and
Python/C++ code stubs from LeetCode's GraphQL API. Populated
all 197 NeetCode 150 note files with:
- Problem description (examples, constraints)
- Python code stub (function signature)
- C++ code stub (function signature + includes)

API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
2026-06-01 17:22:07 +08:00
parent e798e449bd
commit 1dec88aaf2
198 changed files with 10459 additions and 534 deletions
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0004. Median of Two Sorted Arrays :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0004. Median of Two Sorted Arrays][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0004. Median of Two Sorted Arrays][0004. Median of Two Sorted Arrays]]
:END:
Given two sorted arrays ~nums1~ and ~nums2~ of size ~m~ and ~n~ respectively, return *the median* of the two sorted arrays.
The overall run time complexity should be ~O(log (m+n))~.
*Example 1:*
#+begin_src
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
#+end_src
*Example 2:*
#+begin_src
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
#+end_src
*Constraints:*
- ~nums1.length == m~
- ~nums2.length == n~
- ~0 <= m <= 1000~
- ~0 <= n <= 1000~
- ~1 <= m + n <= 2000~
- ~-10^{6} <= nums1[i], nums2[i] <= 10^{6}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
}
};
#+end_src
@@ -1,18 +1,67 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0033. Search In Rotated Sorted Array :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0033. Search In Rotated Sorted Array][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0033. Search In Rotated Sorted Array][0033. Search In Rotated Sorted Array]]
:END:
There is an integer array ~nums~ sorted in ascending order (with *distinct* values).
Prior to being passed to your function, ~nums~ is *possibly left rotated* at an unknown index ~k~ (~1 <= k < nums.length~) such that the resulting array is ~[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]~ (*0-indexed*). For example, ~[0,1,2,4,5,6,7]~ might be left rotated by ~3~ indices and become ~[4,5,6,7,0,1,2]~.
Given the array ~nums~ *after* the possible rotation and an integer ~target~, return /the index of /~target~/ if it is in /~nums~/, or /~-1~/ if it is not in /~nums~.
You must write an algorithm with ~O(log n)~ runtime complexity.
*Example 1:*
#+begin_src
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
#+end_src
*Example 2:*
#+begin_src
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
#+end_src
*Example 3:*
#+begin_src
Input: nums = [1], target = 0
Output: -1
#+end_src
*Constraints:*
- ~1 <= nums.length <= 5000~
- ~-10^{4} <= nums[i] <= 10^{4}~
- All values of ~nums~ are *unique*.
- ~nums~ is an ascending array that is possibly rotated.
- ~-10^{4} <= target <= 10^{4}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def search(self, nums: List[int], target: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int search(vector<int>& nums, int target) {
}
};
#+end_src
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0074. Search a 2D Matrix :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0074. Search a 2D Matrix][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0074. Search a 2D Matrix][0074. Search a 2D Matrix]]
:END:
You are given an ~m x n~ integer matrix ~matrix~ with the following two properties:
- Each row is sorted in non-decreasing order.
- The first integer of each row is greater than the last integer of the previous row.
Given an integer ~target~, return ~true~ /if/ ~target~ /is in/ ~matrix~ /or/ ~false~ /otherwise/.
You must write a solution in ~O(log(m * n))~ time complexity.
*Example 1:*
#+begin_src
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true
#+end_src
*Example 2:*
#+begin_src
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false
#+end_src
*Constraints:*
- ~m == matrix.length~
- ~n == matrix[i].length~
- ~1 <= m, n <= 100~
- ~-10^{4} <= matrix[i][j], target <= 10^{4}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
}
};
#+end_src
@@ -1,18 +1,78 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0153. Find Minimum In Rotated Sorted Array :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0153. Find Minimum In Rotated Sorted Array][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0153. Find Minimum In Rotated Sorted Array][0153. Find Minimum In Rotated Sorted Array]]
:END:
Suppose an array of length ~n~ sorted in ascending order is *rotated* between ~1~ and ~n~ times. For example, the array ~nums = [0,1,2,4,5,6,7]~ might become:
- ~[4,5,6,7,0,1,2]~ if it was rotated ~4~ times.
- ~[0,1,2,4,5,6,7]~ if it was rotated ~7~ times.
Notice that *rotating* an array ~[a[0], a[1], a[2], ..., a[n-1]]~ 1 time results in the array ~[a[n-1], a[0], a[1], a[2], ..., a[n-2]]~.
Given the sorted rotated array ~nums~ of *unique* elements, return /the minimum element of this array/.
You must write an algorithm that runs in ~O(log n) time~.
*Example 1:*
#+begin_src
Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
#+end_src
*Example 2:*
#+begin_src
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
#+end_src
*Example 3:*
#+begin_src
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
#+end_src
*Constraints:*
- ~n == nums.length~
- ~1 <= n <= 5000~
- ~-5000 <= nums[i] <= 5000~
- All the integers of ~nums~ are *unique*.
- ~nums~ is sorted and rotated between ~1~ and ~n~ times.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findMin(self, nums: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findMin(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,58 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0704. Binary Search :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0704. Binary Search][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0704. Binary Search][0704. Binary Search]]
:END:
Given an array of integers ~nums~ which is sorted in ascending order, and an integer ~target~, write a function to search ~target~ in ~nums~. If ~target~ exists, then return its index. Otherwise, return ~-1~.
You must write an algorithm with ~O(log n)~ runtime complexity.
*Example 1:*
#+begin_src
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
#+end_src
*Example 2:*
#+begin_src
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
#+end_src
*Constraints:*
- ~1 <= nums.length <= 10^{4}~
- ~-10^{4} < nums[i], target < 10^{4}~
- All the integers in ~nums~ are *unique*.
- ~nums~ is sorted in ascending order.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def search(self, nums: List[int], target: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int search(vector<int>& nums, int target) {
}
};
#+end_src
@@ -1,18 +1,70 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0719. Find K-th Smallest Pair Distance :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0719. Find K-th Smallest Pair Distance][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0719. Find K-th Smallest Pair Distance][0719. Find K-th Smallest Pair Distance]]
:END:
The *distance of a pair* of integers ~a~ and ~b~ is defined as the absolute difference between ~a~ and ~b~.
Given an integer array ~nums~ and an integer ~k~, return /the/ ~k^{th}~ /smallest *distance among all the pairs*/ ~nums[i]~ /and/ ~nums[j]~ /where/ ~0 <= i < j < nums.length~.
*Example 1:*
#+begin_src
Input: nums = [1,3,1], k = 1
Output: 0
Explanation: Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.
#+end_src
*Example 2:*
#+begin_src
Input: nums = [1,1,1], k = 2
Output: 0
#+end_src
*Example 3:*
#+begin_src
Input: nums = [1,6,1], k = 3
Output: 5
#+end_src
*Constraints:*
- ~n == nums.length~
- ~2 <= n <= 10^{4}~
- ~0 <= nums[i] <= 10^{6}~
- ~1 <= k <= n * (n - 1) / 2~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def smallestDistancePair(self, nums: List[int], k: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
}
};
#+end_src
@@ -1,18 +1,67 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0875. Koko Eating Bananas :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0875. Koko Eating Bananas][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0875. Koko Eating Bananas][0875. Koko Eating Bananas]]
:END:
Koko loves to eat bananas. There are ~n~ piles of bananas, the ~i^{th}~ pile has ~piles[i]~ bananas. The guards have gone and will come back in ~h~ hours.
Koko can decide her bananas-per-hour eating speed of ~k~. Each hour, she chooses some pile of bananas and eats ~k~ bananas from that pile. If the pile has less than ~k~ bananas, she eats all of them instead and will not eat any more bananas during this hour.
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return /the minimum integer/ ~k~ /such that she can eat all the bananas within/ ~h~ /hours/.
*Example 1:*
#+begin_src
Input: piles = [3,6,7,11], h = 8
Output: 4
#+end_src
*Example 2:*
#+begin_src
Input: piles = [30,11,23,4,20], h = 5
Output: 30
#+end_src
*Example 3:*
#+begin_src
Input: piles = [30,11,23,4,20], h = 6
Output: 23
#+end_src
*Constraints:*
- ~1 <= piles.length <= 10^{4}~
- ~piles.length <= h <= 10^{9}~
- ~1 <= piles[i] <= 10^{9}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int minEatingSpeed(vector<int>& piles, int h) {
}
};
#+end_src
@@ -1,18 +1,96 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0981. Time Based Key Value Store :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0981. Time Based Key Value Store][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0981. Time Based Key Value Store][0981. Time Based Key Value Store]]
:END:
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp.
Implement the ~TimeMap~ class:
- ~TimeMap()~ Initializes the object of the data structure.
- ~void set(String key, String value, int timestamp)~ Stores the key ~key~ with the value ~value~ at the given time ~timestamp~.
- ~String get(String key, int timestamp)~ Returns a value such that ~set~ was called previously, with ~timestamp_prev <= timestamp~. If there are multiple such values, it returns the value associated with the largest ~timestamp_prev~. If there are no values, it returns ~""~.
*Example 1:*
#+begin_src
Input
["TimeMap", "set", "get", "get", "set", "get", "get"]
[[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]]
Output
[null, null, "bar", "bar", null, "bar2", "bar2"]
Explanation
TimeMap timeMap = new TimeMap();
timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1.
timeMap.get("foo", 1); // return "bar"
timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar".
timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4.
timeMap.get("foo", 4); // return "bar2"
timeMap.get("foo", 5); // return "bar2"
#+end_src
*Constraints:*
- ~1 <= key.length, value.length <= 100~
- ~key~ and ~value~ consist of lowercase English letters and digits.
- ~1 <= timestamp <= 10^{7}~
- All the timestamps ~timestamp~ of ~set~ are strictly increasing.
- At most ~2 * 10^{5}~ calls will be made to ~set~ and ~get~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class TimeMap:
def __init__(self):
def set(self, key: str, value: str, timestamp: int) -> None:
def get(self, key: str, timestamp: int) -> str:
# Your TimeMap object will be instantiated and called as such:
# obj = TimeMap()
# obj.set(key,value,timestamp)
# param_2 = obj.get(key,timestamp)
#+end_src
** TODO C++
#+begin_src cpp
class TimeMap {
public:
TimeMap() {
}
void set(string key, string value, int timestamp) {
}
string get(string key, int timestamp) {
}
};
/**
* Your TimeMap object will be instantiated and called as such:
* TimeMap* obj = new TimeMap();
* obj->set(key,value,timestamp);
* string param_2 = obj->get(key,timestamp);
*/
#+end_src
@@ -1,18 +1,70 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2300. Successful Pairs of Spells and Potions :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2300. Successful Pairs of Spells and Potions][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2300. Successful Pairs of Spells and Potions][2300. Successful Pairs of Spells and Potions]]
:END:
You are given two positive integer arrays ~spells~ and ~potions~, of length ~n~ and ~m~ respectively, where ~spells[i]~ represents the strength of the ~i^{th}~ spell and ~potions[j]~ represents the strength of the ~j^{th}~ potion.
You are also given an integer ~success~. A spell and potion pair is considered *successful* if the *product* of their strengths is *at least* ~success~.
Return /an integer array /~pairs~/ of length /~n~/ where /~pairs[i]~/ is the number of *potions* that will form a successful pair with the /~i^{th}~/ spell./
*Example 1:*
#+begin_src
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
Output: [4,0,3]
Explanation:
- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
Thus, [4,0,3] is returned.
#+end_src
*Example 2:*
#+begin_src
Input: spells = [3,1,2], potions = [8,5,8], success = 16
Output: [2,0,2]
Explanation:
- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
Thus, [2,0,2] is returned.
#+end_src
*Constraints:*
- ~n == spells.length~
- ~m == potions.length~
- ~1 <= n, m <= 10^{5}~
- ~1 <= spells[i], potions[i] <= 10^{5}~
- ~1 <= success <= 10^{10}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
}
};
#+end_src