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 0009. Palindrome Number :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0009. Palindrome Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0009. Palindrome Number][0009. Palindrome Number]]
:END:
Given an integer ~x~, return ~true~/ if /~x~/ is a //*palindrome*//, and /~false~/ otherwise/.
*Example 1:*
#+begin_src
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
#+end_src
*Example 2:*
#+begin_src
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
#+end_src
*Example 3:*
#+begin_src
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
#+end_src
*Constraints:*
- ~-2^{31} <= x <= 2^{31} - 1~
*Follow up:* Could you solve it without converting the integer to a string?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def isPalindrome(self, x: int) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool isPalindrome(int x) {
}
};
#+end_src
@@ -1,18 +1,114 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0012. Integer to Roman :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0012. Integer to Roman][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0012. Integer to Roman][0012. Integer to Roman]]
:END:
Seven different symbols represent Roman numerals with the following values:
Symbol
Value
I
1
V
5
X
10
L
50
C
100
D
500
M
1000
Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
- If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
- If the value starts with 4 or 9 use the *subtractive form* representing one symbol subtracted from the following symbol, for example, 4 is 1 (~I~) less than 5 (~V~): ~IV~ and 9 is 1 (~I~) less than 10 (~X~): ~IX~. Only the following subtractive forms are used: 4 (~IV~), 9 (~IX~), 40 (~XL~), 90 (~XC~), 400 (~CD~) and 900 (~CM~).
- Only powers of 10 (~I~, ~X~, ~C~, ~M~) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (~V~), 50 (~L~), or 500 (~D~) multiple times. If you need to append a symbol 4 times use the *subtractive form*.
Given an integer, convert it to a Roman numeral.
*Example 1:*
*Input:* num = 3749
*Output:* "MMMDCCXLIX"
*Explanation:*
#+begin_src
3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
700 = DCC as 500 (D) + 100 (C) + 100 (C)
40 = XL as 10 (X) less of 50 (L)
9 = IX as 1 (I) less of 10 (X)
Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places
#+end_src
*Example 2:*
*Input:* num = 58
*Output:* "LVIII"
*Explanation:*
#+begin_src
50 = L
8 = VIII
#+end_src
*Example 3:*
*Input:* num = 1994
*Output:* "MCMXCIV"
*Explanation:*
#+begin_src
1000 = M
900 = CM
90 = XC
4 = IV
#+end_src
*Constraints:*
- ~1 <= num <= 3999~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def intToRoman(self, num: int) -> str:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
string intToRoman(int num) {
}
};
#+end_src
@@ -1,18 +1,52 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0043. Multiply Strings :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0043. Multiply Strings][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0043. Multiply Strings][0043. Multiply Strings]]
:END:
Given two non-negative integers ~num1~ and ~num2~ represented as strings, return the product of ~num1~ and ~num2~, also represented as a string.
*Note:* You must not use any built-in BigInteger library or convert the inputs to integer directly.
*Example 1:*
#+begin_src
Input: num1 = "2", num2 = "3"
Output: "6"
#+end_src
*Example 2:*
#+begin_src
Input: num1 = "123", num2 = "456"
Output: "56088"
#+end_src
*Constraints:*
- ~1 <= num1.length, num2.length <= 200~
- ~num1~ and ~num2~ consist of digits only.
- Both ~num1~ and ~num2~ do not contain any leading zero, except the number ~0~ itself.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def multiply(self, num1: str, num2: str) -> str:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
string multiply(string num1, string num2) {
}
};
#+end_src
@@ -1,18 +1,57 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0048. Rotate Image :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0048. Rotate Image][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0048. Rotate Image][0048. Rotate Image]]
:END:
You are given an ~n x n~ 2D ~matrix~ representing an image, rotate the image by *90* degrees (clockwise).
You have to rotate the image *in-place*, which means you have to modify the input 2D matrix directly. *DO NOT* allocate another 2D matrix and do the rotation.
*Example 1:*
#+begin_src
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
#+end_src
*Example 2:*
#+begin_src
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
#+end_src
*Constraints:*
- ~n == matrix.length == matrix[i].length~
- ~1 <= n <= 20~
- ~-1000 <= matrix[i][j] <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
}
};
#+end_src
@@ -1,18 +1,54 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0054. Spiral Matrix :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0054. Spiral Matrix][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0054. Spiral Matrix][0054. Spiral Matrix]]
:END:
Given an ~m x n~ ~matrix~, return /all elements of the/ ~matrix~ /in spiral order/.
*Example 1:*
#+begin_src
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
#+end_src
*Example 2:*
#+begin_src
Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
#+end_src
*Constraints:*
- ~m == matrix.length~
- ~n == matrix[i].length~
- ~1 <= m, n <= 10~
- ~-100 <= matrix[i][j] <= 100~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
}
};
#+end_src
@@ -1,18 +1,72 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0066. Plus One :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0066. Plus One][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0066. Plus One][0066. Plus One]]
:END:
You are given a *large integer* represented as an integer array ~digits~, where each ~digits[i]~ is the ~i^{th}~ digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading ~0~'s.
Increment the large integer by one and return /the resulting array of digits/.
*Example 1:*
#+begin_src
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
#+end_src
*Example 2:*
#+begin_src
Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].
#+end_src
*Example 3:*
#+begin_src
Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].
#+end_src
*Constraints:*
- ~1 <= digits.length <= 100~
- ~0 <= digits[i] <= 9~
- ~digits~ does not contain any leading ~0~'s.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
}
};
#+end_src
@@ -1,18 +1,67 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0073. Set Matrix Zeroes :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0073. Set Matrix Zeroes][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0073. Set Matrix Zeroes][0073. Set Matrix Zeroes]]
:END:
Given an ~m x n~ integer matrix ~matrix~, if an element is ~0~, set its entire row and column to ~0~'s.
You must do it in place.
*Example 1:*
#+begin_src
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
#+end_src
*Example 2:*
#+begin_src
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
#+end_src
*Constraints:*
- ~m == matrix.length~
- ~n == matrix[0].length~
- ~1 <= m, n <= 200~
- ~-2^{31} <= matrix[i][j] <= 2^{31} - 1~
*Follow up:*
- A straightforward solution using ~O(mn)~ space is probably a bad idea.
- A simple improvement uses ~O(m + n)~ space, but still not the best solution.
- Could you devise a constant space solution?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
}
};
#+end_src
@@ -1,18 +1,63 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0202. Happy Number :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0202. Happy Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0202. Happy Number][0202. Happy Number]]
:END:
Write an algorithm to determine if a number ~n~ is happy.
A *happy number* is a number defined by the following process:
- Starting with any positive integer, replace the number by the sum of the squares of its digits.
- Repeat the process until the number equals 1 (where it will stay), or it *loops endlessly in a cycle* which does not include 1.
- Those numbers for which this process *ends in 1* are happy.
Return ~true~ /if/ ~n~ /is a happy number, and/ ~false~ /if not/.
*Example 1:*
#+begin_src
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
#+end_src
*Example 2:*
#+begin_src
Input: n = 2
Output: false
#+end_src
*Constraints:*
- ~1 <= n <= 2^{31} - 1~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def isHappy(self, n: int) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool isHappy(int n) {
}
};
#+end_src
@@ -1,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0296. Best Meeting Point :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0296. Best Meeting Point][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0296. Best Meeting Point][0296. Best Meeting Point]]
:END:
** TODO Approach
@@ -1,18 +1,64 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0840. Magic Squares In Grid :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0840. Magic Squares In Grid][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0840. Magic Squares In Grid][0840. Magic Squares In Grid]]
:END:
A ~3 x 3~ *magic square* is a ~3 x 3~ grid filled with distinct numbers *from *1* to *9 such that each row, column, and both diagonals all have the same sum.
Given a ~row x col~ ~grid~ of integers, how many ~3 x 3~ magic square subgrids are there?
Note: while a magic square can only contain numbers from 1 to 9, ~grid~ may contain numbers up to 15.
*Example 1:*
#+begin_src
Input: grid = [[4,3,8,4],[9,5,1,9],[2,7,6,2]]
Output: 1
Explanation:
The following subgrid is a 3 x 3 magic square:
while this one is not:
In total, there is only one magic square inside the given grid.
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[8]]
Output: 0
#+end_src
*Constraints:*
- ~row == grid.length~
- ~col == grid[i].length~
- ~1 <= row, col <= 10~
- ~0 <= grid[i][j] <= 15~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
}
};
#+end_src
@@ -1,18 +1,61 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1780. Check if Number is a Sum of Powers of Three :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1780. Check if Number is a Sum of Powers of Three][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1780. Check if Number is a Sum of Powers of Three][1780. Check if Number is a Sum of Powers of Three]]
:END:
Given an integer ~n~, return ~true~ /if it is possible to represent /~n~/ as the sum of distinct powers of three./ Otherwise, return ~false~.
An integer ~y~ is a power of three if there exists an integer ~x~ such that ~y == 3^{x}~.
*Example 1:*
#+begin_src
Input: n = 12
Output: true
Explanation: 12 = 31 + 32
#+end_src
*Example 2:*
#+begin_src
Input: n = 91
Output: true
Explanation: 91 = 30 + 32 + 34
#+end_src
*Example 3:*
#+begin_src
Input: n = 21
Output: false
#+end_src
*Constraints:*
- ~1 <= n <= 10^{7}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def checkPowersOfThree(self, n: int) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool checkPowersOfThree(int n) {
}
};
#+end_src
@@ -1,18 +1,102 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2013. Detect Squares :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2013. Detect Squares][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2013. Detect Squares][2013. Detect Squares]]
:END:
You are given a stream of points on the X-Y plane. Design an algorithm that:
- *Adds* new points from the stream into a data structure. *Duplicate* points are allowed and should be treated as different points.
- Given a query point, *counts* the number of ways to choose three points from the data structure such that the three points and the query point form an *axis-aligned square* with *positive area*.
An *axis-aligned square* is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
Implement the ~DetectSquares~ class:
- ~DetectSquares()~ Initializes the object with an empty data structure.
- ~void add(int[] point)~ Adds a new point ~point = [x, y]~ to the data structure.
- ~int count(int[] point)~ Counts the number of ways to form *axis-aligned squares* with point ~point = [x, y]~ as described above.
*Example 1:*
#+begin_src
Input
["DetectSquares", "add", "add", "add", "count", "count", "add", "count"]
[[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
Output
[null, null, null, null, 1, 0, null, 2]
Explanation
DetectSquares detectSquares = new DetectSquares();
detectSquares.add([3, 10]);
detectSquares.add([11, 2]);
detectSquares.add([3, 2]);
detectSquares.count([11, 10]); // return 1. You can choose:
// - The first, second, and third points
detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure.
detectSquares.add([11, 2]); // Adding duplicate points is allowed.
detectSquares.count([11, 10]); // return 2. You can choose:
// - The first, second, and third points
// - The first, third, and fourth points
#+end_src
*Constraints:*
- ~point.length == 2~
- ~0 <= x, y <= 1000~
- At most ~3000~ calls *in total* will be made to ~add~ and ~count~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class DetectSquares:
def __init__(self):
def add(self, point: List[int]) -> None:
def count(self, point: List[int]) -> int:
# Your DetectSquares object will be instantiated and called as such:
# obj = DetectSquares()
# obj.add(point)
# param_2 = obj.count(point)
#+end_src
** TODO C++
#+begin_src cpp
class DetectSquares {
public:
DetectSquares() {
}
void add(vector<int> point) {
}
int count(vector<int> point) {
}
};
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares* obj = new DetectSquares();
* obj->add(point);
* int param_2 = obj->count(point);
*/
#+end_src
@@ -1,18 +1,79 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2326. Spiral Matrix IV :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2326. Spiral Matrix IV][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2326. Spiral Matrix IV][2326. Spiral Matrix IV]]
:END:
You are given two integers ~m~ and ~n~, which represent the dimensions of a matrix.
You are also given the ~head~ of a linked list of integers.
Generate an ~m x n~ matrix that contains the integers in the linked list presented in *spiral* order *(clockwise)*, starting from the *top-left* of the matrix. If there are remaining empty spaces, fill them with ~-1~.
Return /the generated matrix/.
*Example 1:*
#+begin_src
Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
Explanation: The diagram above shows how the values are printed in the matrix.
Note that the remaining spaces in the matrix are filled with -1.
#+end_src
*Example 2:*
#+begin_src
Input: m = 1, n = 4, head = [0,1,2]
Output: [[0,1,2,-1]]
Explanation: The diagram above shows how the values are printed from left to right in the matrix.
The last space in the matrix is set to -1.
#+end_src
*Constraints:*
- ~1 <= m, n <= 10^{5}~
- ~1 <= m * n <= 10^{5}~
- The number of nodes in the list is in the range ~[1, m * n]~.
- ~0 <= Node.val <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
#+end_src
** TODO C++
#+begin_src cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
}
};
#+end_src
@@ -1,18 +1,65 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2698. Find the Punishment Number of an Integer :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2698. Find the Punishment Number of an Integer][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2698. Find the Punishment Number of an Integer][2698. Find the Punishment Number of an Integer]]
:END:
Given a positive integer ~n~, return /the *punishment number*/ of ~n~.
The *punishment number* of ~n~ is defined as the sum of the squares of all integers ~i~ such that:
- ~1 <= i <= n~
- The decimal representation of ~i * i~ can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals ~i~.
*Example 1:*
#+begin_src
Input: n = 10
Output: 182
Explanation: There are exactly 3 integers i in the range [1, 10] that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 and 1 with a sum equal to 8 + 1 == 9.
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 and 0 with a sum equal to 10 + 0 == 10.
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
#+end_src
*Example 2:*
#+begin_src
Input: n = 37
Output: 1478
Explanation: There are exactly 4 integers i in the range [1, 37] that satisfy the conditions in the statement:
- 1 since 1 * 1 = 1.
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
#+end_src
*Constraints:*
- ~1 <= n <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def punishmentNumber(self, n: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int punishmentNumber(int n) {
}
};
#+end_src