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,70 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0001. Two Sum :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0001. Two Sum][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0001. Two Sum][0001. Two Sum]]
:END:
Given an array of integers ~nums~ and an integer ~target~, return /indices of the two numbers such that they add up to ~target~/.
You may assume that each input would have */exactly/ one solution*, and you may not use the /same/ element twice.
You can return the answer in any order.
*Example 1:*
#+begin_src
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
#+end_src
*Example 2:*
#+begin_src
Input: nums = [3,2,4], target = 6
Output: [1,2]
#+end_src
*Example 3:*
#+begin_src
Input: nums = [3,3], target = 6
Output: [0,1]
#+end_src
*Constraints:*
- ~2 <= nums.length <= 10^{4}~
- ~-10^{9} <= nums[i] <= 10^{9}~
- ~-10^{9} <= target <= 10^{9}~
- *Only one valid answer exists.*
*Follow-up: *Can you come up with an algorithm that is less than ~O(n^{2})~ time complexity?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
}
};
#+end_src
@@ -1,18 +1,83 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0036. Valid Sudoku :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0036. Valid Sudoku][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0036. Valid Sudoku][0036. Valid Sudoku]]
:END:
Determine if a ~9 x 9~ Sudoku board is valid. Only the filled cells need to be validated *according to the following rules*:
- Each row must contain the digits ~1-9~ without repetition.
- Each column must contain the digits ~1-9~ without repetition.
- Each of the nine ~3 x 3~ sub-boxes of the grid must contain the digits ~1-9~ without repetition.
*Note:*
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
*Example 1:*
#+begin_src
Input: board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: true
#+end_src
*Example 2:*
#+begin_src
Input: board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8&#39;s in the top left 3x3 sub-box, it is invalid.
#+end_src
*Constraints:*
- ~board.length == 9~
- ~board[i].length == 9~
- ~board[i][j]~ is a digit ~1-9~ or ~'.'~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
}
};
#+end_src
@@ -1,18 +1,60 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0049. Group Anagrams :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0049. Group Anagrams][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0049. Group Anagrams][0049. Group Anagrams]]
:END:
Given an array of strings ~strs~, group the anagrams together. You can return the answer in *any order*.
*Example 1:*
*Input:* strs = ["eat","tea","tan","ate","nat","bat"]
*Output:* [["bat"],["nat","tan"],["ate","eat","tea"]]
*Explanation:*
- There is no string in strs that can be rearranged to form ~"bat"~.
- The strings ~"nat"~ and ~"tan"~ are anagrams as they can be rearranged to form each other.
- The strings ~"ate"~, ~"eat"~, and ~"tea"~ are anagrams as they can be rearranged to form each other.
*Example 2:*
*Input:* strs = [""]
*Output:* [[""]]
*Example 3:*
*Input:* strs = ["a"]
*Output:* [["a"]]
*Constraints:*
- ~1 <= strs.length <= 10^{4}~
- ~0 <= strs[i].length <= 100~
- ~strs[i]~ consists of lowercase English letters.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
}
};
#+end_src
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0128. Longest Consecutive Sequence :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0128. Longest Consecutive Sequence][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0128. Longest Consecutive Sequence][0128. Longest Consecutive Sequence]]
:END:
Given an unsorted array of integers ~nums~, return /the length of the longest consecutive elements sequence./
You must write an algorithm that runs in ~O(n)~ time.
*Example 1:*
#+begin_src
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
#+end_src
*Example 2:*
#+begin_src
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
#+end_src
*Example 3:*
#+begin_src
Input: nums = [1,0,1,2]
Output: 3
#+end_src
*Constraints:*
- ~0 <= nums.length <= 10^{5}~
- ~-10^{9} <= nums[i] <= 10^{9}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,58 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0217. Contains Duplicate :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0217. Contains Duplicate][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0217. Contains Duplicate][0217. Contains Duplicate]]
:END:
Given an integer array ~nums~, return ~true~ if any value appears *at least twice* in the array, and return ~false~ if every element is distinct.
*Example 1:*
*Input:* nums = [1,2,3,1]
*Output:* true
*Explanation:*
The element 1 occurs at the indices 0 and 3.
*Example 2:*
*Input:* nums = [1,2,3,4]
*Output:* false
*Explanation:*
All elements are distinct.
*Example 3:*
*Input:* nums = [1,1,1,3,3,4,3,2,4,2]
*Output:* true
*Constraints:*
- ~1 <= nums.length <= 10^{5}~
- ~-10^{9} <= nums[i] <= 10^{9}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,56 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0238. Product of Array Except Self :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0238. Product of Array Except Self][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0238. Product of Array Except Self][0238. Product of Array Except Self]]
:END:
Given an integer array ~nums~, return /an array/ ~answer~ /such that/ ~answer[i]~ /is equal to the product of all the elements of/ ~nums~ /except/ ~nums[i]~.
The product of any prefix or suffix of ~nums~ is *guaranteed* to fit in a *32-bit* integer.
You must write an algorithm that runs in ~O(n)~ time and without using the division operation.
*Example 1:*
#+begin_src
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
#+end_src
*Example 2:*
#+begin_src
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
#+end_src
*Constraints:*
- ~2 <= nums.length <= 10^{5}~
- ~-30 <= nums[i] <= 30~
- The input is generated such that ~answer[i]~ is *guaranteed* to fit in a *32-bit* integer.
*Follow up:* Can you solve the problem in ~O(1)~ extra space complexity? (The output array *does not* count as extra space for space complexity analysis.)
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,46 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0242. Valid Anagram :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0242. Valid Anagram][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0242. Valid Anagram][0242. Valid Anagram]]
:END:
Given two strings ~s~ and ~t~, return ~true~ if ~t~ is an anagram of ~s~, and ~false~ otherwise.
*Example 1:*
*Input:* s = "anagram", t = "nagaram"
*Output:* true
*Example 2:*
*Input:* s = "rat", t = "car"
*Output:* false
*Constraints:*
- ~1 <= s.length, t.length <= 5 * 10^{4}~
- ~s~ and ~t~ consist of lowercase English letters.
*Follow up:* What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool isAnagram(string s, string t) {
}
};
#+end_src
@@ -1,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0271. Encode and Decode Strings :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0271. Encode and Decode Strings][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0271. Encode and Decode Strings][0271. Encode and Decode Strings]]
:END:
** TODO Approach
@@ -1,18 +1,56 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0347. Top K Frequent Elements :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0347. Top K Frequent Elements][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0347. Top K Frequent Elements][0347. Top K Frequent Elements]]
:END:
Given an integer array ~nums~ and an integer ~k~, return /the/ ~k~ /most frequent elements/. You may return the answer in *any order*.
*Example 1:*
*Input:* nums = [1,1,1,2,2,3], k = 2
*Output:* [1,2]
*Example 2:*
*Input:* nums = [1], k = 1
*Output:* [1]
*Example 3:*
*Input:* nums = [1,2,1,2,1,2,3,1,3,2], k = 2
*Output:* [1,2]
*Constraints:*
- ~1 <= nums.length <= 10^{5}~
- ~-10^{4} <= nums[i] <= 10^{4}~
- ~k~ is in the range ~[1, the number of unique elements in the array]~.
- It is *guaranteed* that the answer is *unique*.
*Follow up:* Your algorithm's time complexity must be better than ~O(n log n)~, where n is the array's size.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
}
};
#+end_src
@@ -1,18 +1,67 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1408. String Matching in an Array :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1408. String Matching in an Array][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1408. String Matching in an Array][1408. String Matching in an Array]]
:END:
Given an array of string ~words~, return all strings in/ /~words~/ /that are a substring of another word. You can return the answer in *any order*.
*Example 1:*
#+begin_src
Input: words = ["mass","as","hero","superhero"]
Output: ["as","hero"]
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
["hero","as"] is also a valid answer.
#+end_src
*Example 2:*
#+begin_src
Input: words = ["leetcode","et","code"]
Output: ["et","code"]
Explanation: "et", "code" are substring of "leetcode".
#+end_src
*Example 3:*
#+begin_src
Input: words = ["blue","green","bu"]
Output: []
Explanation: No string of words is substring of another string.
#+end_src
*Constraints:*
- ~1 <= words.length <= 100~
- ~1 <= words[i].length <= 30~
- ~words[i]~ contains only lowercase English letters.
- All the strings of ~words~ are *unique*.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
}
};
#+end_src
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1769. Minimum Number of Operations to Move All Balls to Each Box :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1769. Minimum Number of Operations to Move All Balls to Each Box][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1769. Minimum Number of Operations to Move All Balls to Each Box][1769. Minimum Number of Operations to Move All Balls to Each Box]]
:END:
You have ~n~ boxes. You are given a binary string ~boxes~ of length ~n~, where ~boxes[i]~ is ~'0'~ if the ~i^{th}~ box is *empty*, and ~'1'~ if it contains *one* ball.
In one operation, you can move *one* ball from a box to an adjacent box. Box ~i~ is adjacent to box ~j~ if ~abs(i - j) == 1~. Note that after doing so, there may be more than one ball in some boxes.
Return an array ~answer~ of size ~n~, where ~answer[i]~ is the *minimum* number of operations needed to move all the balls to the ~i^{th}~ box.
Each ~answer[i]~ is calculated considering the *initial* state of the boxes.
*Example 1:*
#+begin_src
Input: boxes = "110"
Output: [1,1,3]
Explanation: The answer for each box is as follows:
1) First box: you will have to move one ball from the second box to the first box in one operation.
2) Second box: you will have to move one ball from the first box to the second box in one operation.
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
#+end_src
*Example 2:*
#+begin_src
Input: boxes = "001011"
Output: [11,8,5,4,3,4]
#+end_src
*Constraints:*
- ~n == boxes.length~
- ~1 <= n <= 2000~
- ~boxes[i]~ is either ~'0'~ or ~'1'~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def minOperations(self, boxes: str) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> minOperations(string boxes) {
}
};
#+end_src
@@ -1,18 +1,68 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2678. Number of Senior Citizens :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2678. Number of Senior Citizens][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2678. Number of Senior Citizens][2678. Number of Senior Citizens]]
:END:
You are given a *0-indexed* array of strings ~details~. Each element of ~details~ provides information about a given passenger compressed into a string of length ~15~. The system is such that:
- The first ten characters consist of the phone number of passengers.
- The next character denotes the gender of the person.
- The following two characters are used to indicate the age of the person.
- The last two characters determine the seat allotted to that person.
Return /the number of passengers who are *strictly **more than 60 years old*./
*Example 1:*
#+begin_src
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2
Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
#+end_src
*Example 2:*
#+begin_src
Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
Explanation: None of the passengers are older than 60.
#+end_src
*Constraints:*
- ~1 <= details.length <= 100~
- ~details[i].length == 15~
- ~details[i] consists of digits from '0' to '9'.~
- ~details[i][10] is either 'M' or 'F' or 'O'.~
- The phone numbers and seat numbers of the passengers are distinct.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def countSeniors(self, details: List[str]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int countSeniors(vector<string>& details) {
}
};
#+end_src