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,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0269. Alien Dictionary :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0269. Alien Dictionary][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0269. Alien Dictionary][0269. Alien Dictionary]]
:END:
** TODO Approach
@@ -1,18 +1,65 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0332. Reconstruct Itinerary :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0332. Reconstruct Itinerary][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0332. Reconstruct Itinerary][0332. Reconstruct Itinerary]]
:END:
You are given a list of airline ~tickets~ where ~tickets[i] = [from_{i}, to_{i}]~ represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from ~"JFK"~, thus, the itinerary must begin with ~"JFK"~. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
- For example, the itinerary ~["JFK", "LGA"]~ has a smaller lexical order than ~["JFK", "LGB"]~.
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
*Example 1:*
#+begin_src
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
Output: ["JFK","MUC","LHR","SFO","SJC"]
#+end_src
*Example 2:*
#+begin_src
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
#+end_src
*Constraints:*
- ~1 <= tickets.length <= 300~
- ~tickets[i].length == 2~
- ~from_{i}.length == 3~
- ~to_{i}.length == 3~
- ~from_{i}~ and ~to_{i}~ consist of uppercase English letters.
- ~from_{i} != to_{i}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
}
};
#+end_src
@@ -1,18 +1,71 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0743. Network Delay Time :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0743. Network Delay Time][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0743. Network Delay Time][0743. Network Delay Time]]
:END:
You are given a network of ~n~ nodes, labeled from ~1~ to ~n~. You are also given ~times~, a list of travel times as directed edges ~times[i] = (u_{i}, v_{i}, w_{i})~, where ~u_{i}~ is the source node, ~v_{i}~ is the target node, and ~w_{i}~ is the time it takes for a signal to travel from source to target.
We will send a signal from a given node ~k~. Return /the *minimum* time it takes for all the/ ~n~ /nodes to receive the signal/. If it is impossible for all the ~n~ nodes to receive the signal, return ~-1~.
*Example 1:*
#+begin_src
Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
#+end_src
*Example 2:*
#+begin_src
Input: times = [[1,2,1]], n = 2, k = 1
Output: 1
#+end_src
*Example 3:*
#+begin_src
Input: times = [[1,2,1]], n = 2, k = 2
Output: -1
#+end_src
*Constraints:*
- ~1 <= k <= n <= 100~
- ~1 <= times.length <= 6000~
- ~times[i].length == 3~
- ~1 <= u_{i}, v_{i} <= n~
- ~u_{i} != v_{i}~
- ~0 <= w_{i} <= 100~
- All the pairs ~(u_{i}, v_{i})~ are *unique*. (i.e., no multiple edges.)
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
}
};
#+end_src
@@ -1,18 +1,69 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0778. Swim In Rising Water :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0778. Swim In Rising Water][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0778. Swim In Rising Water][0778. Swim In Rising Water]]
:END:
You are given an ~n x n~ integer matrix ~grid~ where each value ~grid[i][j]~ represents the elevation at that point ~(i, j)~.
It starts raining, and water gradually rises over time. At time ~t~, the water level is ~t~, meaning *any* cell with elevation less than equal to ~t~ is submerged or reachable.
You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most ~t~. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.
Return /the minimum time until you can reach the bottom right square /~(n - 1, n - 1)~/ if you start at the top left square /~(0, 0)~.
*Example 1:*
#+begin_src
Input: grid = [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output: 16
Explanation: The final route is shown.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
#+end_src
*Constraints:*
- ~n == grid.length~
- ~n == grid[i].length~
- ~1 <= n <= 50~
- ~0 <= grid[i][j] < n^{2}~
- Each value ~grid[i][j]~ is *unique*.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def swimInWater(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
}
};
#+end_src
@@ -1,18 +1,85 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0787. Cheapest Flights Within K Stops :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0787. Cheapest Flights Within K Stops][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0787. Cheapest Flights Within K Stops][0787. Cheapest Flights Within K Stops]]
:END:
There are ~n~ cities connected by some number of flights. You are given an array ~flights~ where ~flights[i] = [from_{i}, to_{i}, price_{i}]~ indicates that there is a flight from city ~from_{i}~ to city ~to_{i}~ with cost ~price_{i}~.
You are also given three integers ~src~, ~dst~, and ~k~, return /*the cheapest price* from /~src~/ to /~dst~/ with at most /~k~/ stops. /If there is no such route, return/ /~-1~.
*Example 1:*
#+begin_src
Input: n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
Output: 700
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 3 is marked in red and has cost 100 + 600 = 700.
Note that the path through cities [0,1,2,3] is cheaper but is invalid because it uses 2 stops.
#+end_src
*Example 2:*
#+begin_src
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph is shown above.
The optimal path with at most 1 stop from city 0 to 2 is marked in red and has cost 100 + 100 = 200.
#+end_src
*Example 3:*
#+begin_src
Input: n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph is shown above.
The optimal path with no stops from city 0 to 2 is marked in red and has cost 500.
#+end_src
*Constraints:*
- ~2 <= n <= 100~
- ~0 <= flights.length <= (n * (n - 1) / 2)~
- ~flights[i].length == 3~
- ~0 <= from_{i}, to_{i} < n~
- ~from_{i} != to_{i}~
- ~1 <= price_{i} <= 10^{4}~
- There will not be any multiple flights between two cities.
- ~0 <= src, dst, k < n~
- ~src != dst~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
}
};
#+end_src
@@ -1,18 +1,60 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1584. Min Cost to Connect All Points :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1584. Min Cost to Connect All Points][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1584. Min Cost to Connect All Points][1584. Min Cost to Connect All Points]]
:END:
You are given an array ~points~ representing integer coordinates of some points on a 2D-plane, where ~points[i] = [x_{i}, y_{i}]~.
The cost of connecting two points ~[x_{i}, y_{i}]~ and ~[x_{j}, y_{j}]~ is the *manhattan distance* between them: ~|x_{i} - x_{j}| + |y_{i} - y_{j}|~, where ~|val|~ denotes the absolute value of ~val~.
Return /the minimum cost to make all points connected./ All points are connected if there is *exactly one* simple path between any two points.
*Example 1:*
#+begin_src
Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]
Output: 20
Explanation:
We can connect the points as shown above to get the minimum cost of 20.
Notice that there is a unique path between every pair of points.
#+end_src
*Example 2:*
#+begin_src
Input: points = [[3,12],[-2,5],[-4,1]]
Output: 18
#+end_src
*Constraints:*
- ~1 <= points.length <= 1000~
- ~-10^{6} <= x_{i}, y_{i} <= 10^{6}~
- All pairs ~(x_{i}, y_{i})~ are distinct.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int minCostConnectPoints(vector<vector<int>>& points) {
}
};
#+end_src
@@ -1,18 +1,77 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2493. Divide Nodes Into the Maximum Number of Groups :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2493. Divide Nodes Into the Maximum Number of Groups][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2493. Divide Nodes Into the Maximum Number of Groups][2493. Divide Nodes Into the Maximum Number of Groups]]
:END:
You are given a positive integer ~n~ representing the number of nodes in an *undirected* graph. The nodes are labeled from ~1~ to ~n~.
You are also given a 2D integer array ~edges~, where ~edges[i] = [a_{i, }b_{i}]~ indicates that there is a *bidirectional* edge between nodes ~a_{i}~ and ~b_{i}~. *Notice* that the given graph may be disconnected.
Divide the nodes of the graph into ~m~ groups (*1-indexed*) such that:
- Each node in the graph belongs to exactly one group.
- For every pair of nodes in the graph that are connected by an edge ~[a_{i, }b_{i}]~, if ~a_{i}~ belongs to the group with index ~x~, and ~b_{i}~ belongs to the group with index ~y~, then ~|y - x| = 1~.
Return /the maximum number of groups (i.e., maximum /~m~/) into which you can divide the nodes/. Return ~-1~ /if it is impossible to group the nodes with the given conditions/.
*Example 1:*
#+begin_src
Input: n = 6, edges = [[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]]
Output: 4
Explanation: As shown in the image we:
- Add node 5 to the first group.
- Add node 1 to the second group.
- Add nodes 2 and 4 to the third group.
- Add nodes 3 and 6 to the fourth group.
We can see that every edge is satisfied.
It can be shown that that if we create a fifth group and move any node from the third or fourth group to it, at least on of the edges will not be satisfied.
#+end_src
*Example 2:*
#+begin_src
Input: n = 3, edges = [[1,2],[2,3],[3,1]]
Output: -1
Explanation: If we add node 1 to the first group, node 2 to the second group, and node 3 to the third group to satisfy the first two edges, we can see that the third edge will not be satisfied.
It can be shown that no grouping is possible.
#+end_src
*Constraints:*
- ~1 <= n <= 500~
- ~1 <= edges.length <= 10^{4}~
- ~edges[i].length == 2~
- ~1 <= a_{i}, b_{i} <= n~
- ~a_{i} != b_{i}~
- There is at most one edge between any pair of vertices.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def magnificentSets(self, n: int, edges: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int magnificentSets(int n, vector<vector<int>>& edges) {
}
};
#+end_src
@@ -1,18 +1,85 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2812. Find the Safest Path in a Grid :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2812. Find the Safest Path in a Grid][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2812. Find the Safest Path in a Grid][2812. Find the Safest Path in a Grid]]
:END:
You are given a *0-indexed* 2D matrix ~grid~ of size ~n x n~, where ~(r, c)~ represents:
- A cell containing a thief if ~grid[r][c] = 1~
- An empty cell if ~grid[r][c] = 0~
You are initially positioned at cell ~(0, 0)~. In one move, you can move to any adjacent cell in the grid, including cells containing thieves.
The *safeness factor* of a path on the grid is defined as the *minimum* manhattan distance from any cell in the path to any thief in the grid.
Return /the *maximum safeness factor* of all paths leading to cell /~(n - 1, n - 1)~/./
An *adjacent* cell of cell ~(r, c)~, is one of the cells ~(r, c + 1)~, ~(r, c - 1)~, ~(r + 1, c)~ and ~(r - 1, c)~ if it exists.
The *Manhattan distance* between two cells ~(a, b)~ and ~(x, y)~ is equal to ~|a - x| + |b - y|~, where ~|val|~ denotes the absolute value of val.
*Example 1:*
#+begin_src
Input: grid = [[1,0,0],[0,0,0],[0,0,1]]
Output: 0
Explanation: All paths from (0, 0) to (n - 1, n - 1) go through the thieves in cells (0, 0) and (n - 1, n - 1).
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[0,0,1],[0,0,0],[0,0,0]]
Output: 2
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 2) is cell (0, 0). The distance between them is | 0 - 0 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
#+end_src
*Example 3:*
#+begin_src
Input: grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
Output: 2
Explanation: The path depicted in the picture above has a safeness factor of 2 since:
- The closest cell of the path to the thief at cell (0, 3) is cell (1, 2). The distance between them is | 0 - 1 | + | 3 - 2 | = 2.
- The closest cell of the path to the thief at cell (3, 0) is cell (3, 2). The distance between them is | 3 - 3 | + | 0 - 2 | = 2.
It can be shown that there are no other paths with a higher safeness factor.
#+end_src
*Constraints:*
- ~1 <= grid.length == n <= 400~
- ~grid[i].length == n~
- ~grid[i][j]~ is either ~0~ or ~1~.
- There is at least one thief in the ~grid~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
}
};
#+end_src