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,52 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0215. Kth Largest Element In An Array :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0215. Kth Largest Element In An Array][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0215. Kth Largest Element In An Array][0215. Kth Largest Element In An Array]]
:END:
Given an integer array ~nums~ and an integer ~k~, return /the/ ~k^{th}~ /largest element in the array/.
Note that it is the ~k^{th}~ largest element in the sorted order, not the ~k^{th}~ distinct element.
Can you solve it without sorting?
*Example 1:*
#+begin_src
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
#+end_src
*Example 2:*
#+begin_src
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
#+end_src
*Constraints:*
- ~1 <= k <= nums.length <= 10^{5}~
- ~-10^{4} <= nums[i] <= 10^{4}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
}
};
#+end_src
@@ -1,18 +1,101 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0295. Find Median From Data Stream :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0295. Find Median From Data Stream][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0295. Find Median From Data Stream][0295. Find Median From Data Stream]]
:END:
The *median* is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.
- For example, for ~arr = [2,3,4]~, the median is ~3~.
- For example, for ~arr = [2,3]~, the median is ~(2 + 3) / 2 = 2.5~.
Implement the MedianFinder class:
- ~MedianFinder()~ initializes the ~MedianFinder~ object.
- ~void addNum(int num)~ adds the integer ~num~ from the data stream to the data structure.
- ~double findMedian()~ returns the median of all elements so far. Answers within ~10^{-5}~ of the actual answer will be accepted.
*Example 1:*
#+begin_src
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
#+end_src
*Constraints:*
- ~-10^{5} <= num <= 10^{5}~
- There will be at least one element in the data structure before calling ~findMedian~.
- At most ~5 * 10^{4}~ calls will be made to ~addNum~ and ~findMedian~.
*Follow up:*
- If all integer numbers from the stream are in the range ~[0, 100]~, how would you optimize your solution?
- If ~99%~ of all integer numbers from the stream are in the range ~[0, 100]~, how would you optimize your solution?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class MedianFinder:
def __init__(self):
def addNum(self, num: int) -> None:
def findMedian(self) -> float:
# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
#+end_src
** TODO C++
#+begin_src cpp
class MedianFinder {
public:
MedianFinder() {
}
void addNum(int num) {
}
double findMedian() {
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
#+end_src
@@ -1,18 +1,119 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0355. Design Twitter :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0355. Design Twitter][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0355. Design Twitter][0355. Design Twitter]]
:END:
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the ~10~ most recent tweets in the user's news feed.
Implement the ~Twitter~ class:
- ~Twitter()~ Initializes your twitter object.
- ~void postTweet(int userId, int tweetId)~ Composes a new tweet with ID ~tweetId~ by the user ~userId~. Each call to this function will be made with a unique ~tweetId~.
- ~List<Integer> getNewsFeed(int userId)~ Retrieves the ~10~ most recent tweet IDs in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user themself. Tweets must be *ordered from most recent to least recent*.
- ~void follow(int followerId, int followeeId)~ The user with ID ~followerId~ started following the user with ID ~followeeId~.
- ~void unfollow(int followerId, int followeeId)~ The user with ID ~followerId~ started unfollowing the user with ID ~followeeId~.
*Example 1:*
#+begin_src
Input
["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"]
[[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
Output
[null, null, [5], null, null, [6, 5], null, [5]]
Explanation
Twitter twitter = new Twitter();
twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5).
twitter.getNewsFeed(1); // User 1&#39;s news feed should return a list with 1 tweet id -> [5]. return [5]
twitter.follow(1, 2); // User 1 follows user 2.
twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6).
twitter.getNewsFeed(1); // User 1&#39;s news feed should return a list with 2 tweet ids -> [6, 5]. Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.unfollow(1, 2); // User 1 unfollows user 2.
twitter.getNewsFeed(1); // User 1&#39;s news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
#+end_src
*Constraints:*
- ~1 <= userId, followerId, followeeId <= 500~
- ~0 <= tweetId <= 10^{4}~
- All the tweets have *unique* IDs.
- At most ~3 * 10^{4}~ calls will be made to ~postTweet~, ~getNewsFeed~, ~follow~, and ~unfollow~.
- A user cannot follow himself.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Twitter:
def __init__(self):
def postTweet(self, userId: int, tweetId: int) -> None:
def getNewsFeed(self, userId: int) -> List[int]:
def follow(self, followerId: int, followeeId: int) -> None:
def unfollow(self, followerId: int, followeeId: int) -> None:
# Your Twitter object will be instantiated and called as such:
# obj = Twitter()
# obj.postTweet(userId,tweetId)
# param_2 = obj.getNewsFeed(userId)
# obj.follow(followerId,followeeId)
# obj.unfollow(followerId,followeeId)
#+end_src
** TODO C++
#+begin_src cpp
class Twitter {
public:
Twitter() {
}
void postTweet(int userId, int tweetId) {
}
vector<int> getNewsFeed(int userId) {
}
void follow(int followerId, int followeeId) {
}
void unfollow(int followerId, int followeeId) {
}
};
/**
* Your Twitter object will be instantiated and called as such:
* Twitter* obj = new Twitter();
* obj->postTweet(userId,tweetId);
* vector<int> param_2 = obj->getNewsFeed(userId);
* obj->follow(followerId,followeeId);
* obj->unfollow(followerId,followeeId);
*/
#+end_src
@@ -1,18 +1,66 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0621. Task Scheduler :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0621. Task Scheduler][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0621. Task Scheduler][0621. Task Scheduler]]
:END:
You are given an array of CPU ~tasks~, each labeled with a letter from A to Z, and a number ~n~. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of *at least* ~n~ intervals between two tasks with the same label.
Return the *minimum* number of CPU intervals required to complete all tasks.
*Example 1:*
*Input:* tasks = ["A","A","A","B","B","B"], n = 2
*Output:* 8
*Explanation:* A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.
After completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3^{rd} interval, neither A nor B can be done, so you idle. By the 4^{th} interval, you can do A again as 2 intervals have passed.
*Example 2:*
*Input:* tasks = ["A","C","A","B","D","B"], n = 1
*Output:* 6
*Explanation:* A possible sequence is: A -> B -> C -> D -> A -> B.
With a cooling interval of 1, you can repeat a task after just one other task.
*Example 3:*
*Input:* tasks = ["A","A","A", "B","B","B"], n = 3
*Output:* 10
*Explanation:* A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.
There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.
*Constraints:*
- ~1 <= tasks.length <= 10^{4}~
- ~tasks[i]~ is an uppercase English letter.
- ~0 <= n <= 100~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int leastInterval(vector<char>& tasks, int n) {
}
};
#+end_src
@@ -1,18 +1,112 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0703. Kth Largest Element In a Stream :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0703. Kth Largest Element In a Stream][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0703. Kth Largest Element In a Stream][0703. Kth Largest Element In a Stream]]
:END:
You are part of a university admissions office and need to keep track of the ~kth~ highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores.
You are tasked to implement a class which, for a given integer ~k~, maintains a stream of test scores and continuously returns the ~k~th highest test score *after* a new score has been submitted. More specifically, we are looking for the ~k~th highest score in the sorted list of all scores.
Implement the ~KthLargest~ class:
- ~KthLargest(int k, int[] nums)~ Initializes the object with the integer ~k~ and the stream of test scores ~nums~.
- ~int add(int val)~ Adds a new test score ~val~ to the stream and returns the element representing the ~k^{th}~ largest element in the pool of test scores so far.
*Example 1:*
*Input:*
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
*Output:* [null, 4, 5, 5, 8, 8]
*Explanation:*
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8
*Example 2:*
*Input:*
["KthLargest", "add", "add", "add", "add"]
[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
*Output:* [null, 7, 7, 7, 8]
*Explanation:*
KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);
kthLargest.add(2); // return 7
kthLargest.add(10); // return 7
kthLargest.add(9); // return 7
kthLargest.add(9); // return 8
*Constraints:*
- ~0 <= nums.length <= 10^{4}~
- ~1 <= k <= nums.length + 1~
- ~-10^{4} <= nums[i] <= 10^{4}~
- ~-10^{4} <= val <= 10^{4}~
- At most ~10^{4}~ calls will be made to ~add~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class KthLargest:
def __init__(self, k: int, nums: List[int]):
def add(self, val: int) -> int:
# Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)
#+end_src
** TODO C++
#+begin_src cpp
class KthLargest {
public:
KthLargest(int k, vector<int>& nums) {
}
int add(int val) {
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
#+end_src
@@ -1,18 +1,60 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0973. K Closest Points to Origin :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0973. K Closest Points to Origin][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0973. K Closest Points to Origin][0973. K Closest Points to Origin]]
:END:
Given an array of ~points~ where ~points[i] = [x_{i}, y_{i}]~ represents a point on the *X-Y* plane and an integer ~k~, return the ~k~ closest points to the origin ~(0, 0)~.
The distance between two points on the *X-Y* plane is the Euclidean distance (i.e., ~&radic;(x_{1} - x_{2})^{2} + (y_{1} - y_{2})^{2}~).
You may return the answer in *any order*. The answer is *guaranteed* to be *unique* (except for the order that it is in).
*Example 1:*
#+begin_src
Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
#+end_src
*Example 2:*
#+begin_src
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
#+end_src
*Constraints:*
- ~1 <= k <= points.length <= 10^{4}~
- ~-10^{4} <= x_{i}, y_{i} <= 10^{4}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
}
};
#+end_src
@@ -1,18 +1,65 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1046. Last Stone Weight :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1046. Last Stone Weight][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1046. Last Stone Weight][1046. Last Stone Weight]]
:END:
You are given an array of integers ~stones~ where ~stones[i]~ is the weight of the ~i^{th}~ stone.
We are playing a game with the stones. On each turn, we choose the *heaviest two stones* and smash them together. Suppose the heaviest two stones have weights ~x~ and ~y~ with ~x <= y~. The result of this smash is:
- If ~x == y~, both stones are destroyed, and
- If ~x != y~, the stone of weight ~x~ is destroyed, and the stone of weight ~y~ has new weight ~y - x~.
At the end of the game, there is *at most one* stone left.
Return /the weight of the last remaining stone/. If there are no stones left, return ~0~.
*Example 1:*
#+begin_src
Input: stones = [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that&#39;s the value of the last stone.
#+end_src
*Example 2:*
#+begin_src
Input: stones = [1]
Output: 1
#+end_src
*Constraints:*
- ~1 <= stones.length <= 30~
- ~1 <= stones[i] <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
}
};
#+end_src