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 0127. Word Ladder :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0127. Word Ladder][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0127. Word Ladder][0127. Word Ladder]]
:END:
A *transformation sequence* from word ~beginWord~ to word ~endWord~ using a dictionary ~wordList~ is a sequence of words ~beginWord -> s_{1} -> s_{2} -> ... -> s_{k}~ such that:
- Every adjacent pair of words differs by a single letter.
- Every ~s_{i}~ for ~1 <= i <= k~ is in ~wordList~. Note that ~beginWord~ does not need to be in ~wordList~.
- ~s_{k} == endWord~
Given two words, ~beginWord~ and ~endWord~, and a dictionary ~wordList~, return /the *number of words* in the *shortest transformation sequence* from/ ~beginWord~ /to/ ~endWord~/, or /~0~/ if no such sequence exists./
*Example 1:*
#+begin_src
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
#+end_src
*Example 2:*
#+begin_src
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
#+end_src
*Constraints:*
- ~1 <= beginWord.length <= 10~
- ~endWord.length == beginWord.length~
- ~1 <= wordList.length <= 5000~
- ~wordList[i].length == beginWord.length~
- ~beginWord~, ~endWord~, and ~wordList[i]~ consist of lowercase English letters.
- ~beginWord != endWord~
- All the words in ~wordList~ are *unique*.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
}
};
#+end_src
@@ -1,18 +1,63 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0130. Surrounded Regions :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0130. Surrounded Regions][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0130. Surrounded Regions][0130. Surrounded Regions]]
:END:
You are given an ~m x n~ matrix ~board~ containing *letters* ~'X'~ and ~'O'~, *capture regions* that are *surrounded*:
- *Connect*: A cell is connected to adjacent cells horizontally or vertically.
- *Region*: To form a region *connect every* ~'O'~ cell.
- *Surround*: A region is surrounded if none of the ~'O'~ cells in that region are on the edge of the board. Such regions are *completely enclosed *by ~'X'~ cells.
To capture a *surrounded region*, replace all ~'O'~s with ~'X'~s *in-place* within the original board. You do not need to return anything.
*Example 1:*
*Input:* board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
*Output:* [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
*Explanation:*
In the above diagram, the bottom region is not captured because it is on the edge of the board and cannot be surrounded.
*Example 2:*
*Input:* board = [["X"]]
*Output:* [["X"]]
*Constraints:*
- ~m == board.length~
- ~n == board[i].length~
- ~1 <= m, n <= 200~
- ~board[i][j]~ is ~'X'~ or ~'O'~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
void solve(vector<vector<char>>& board) {
}
};
#+end_src
@@ -1,18 +1,123 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0133. Clone Graph :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0133. Clone Graph][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0133. Clone Graph][0133. Clone Graph]]
:END:
Given a reference of a node in a *connected* undirected graph.
Return a *deep copy* (clone) of the graph.
Each node in the graph contains a value (~int~) and a list (~List[Node]~) of its neighbors.
#+begin_src
class Node {
public int val;
public List<Node> neighbors;
}
#+end_src
*Test case format:*
For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with ~val == 1~, the second node with ~val == 2~, and so on. The graph is represented in the test case using an adjacency list.
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with ~val = 1~. You must return the *copy of the given node* as a reference to the cloned graph.
*Example 1:*
#+begin_src
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)&#39;s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)&#39;s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)&#39;s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)&#39;s neighbors are 1st node (val = 1) and 3rd node (val = 3).
#+end_src
*Example 2:*
#+begin_src
Input: adjList = [[]]
Output: [[]]
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
#+end_src
*Example 3:*
#+begin_src
Input: adjList = []
Output: []
Explanation: This an empty graph, it does not have any nodes.
#+end_src
*Constraints:*
- The number of nodes in the graph is in the range ~[0, 100]~.
- ~1 <= Node.val <= 100~
- ~Node.val~ is unique for each node.
- There are no repeated edges and no self-loops in the graph.
- The Graph is connected and all nodes can be visited starting from the given node.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
from typing import Optional
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
#+end_src
** TODO C++
#+begin_src cpp
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
Node* cloneGraph(Node* node) {
}
};
#+end_src
@@ -1,18 +1,66 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0200. Number of Islands :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0200. Number of Islands][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0200. Number of Islands][0200. Number of Islands]]
:END:
Given an ~m x n~ 2D binary grid ~grid~ which represents a map of ~'1'~s (land) and ~'0'~s (water), return /the number of islands/.
An *island* is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
*Example 1:*
#+begin_src
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
#+end_src
*Example 2:*
#+begin_src
Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
#+end_src
*Constraints:*
- ~m == grid.length~
- ~n == grid[i].length~
- ~1 <= m, n <= 300~
- ~grid[i][j]~ is ~'0'~ or ~'1'~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
}
};
#+end_src
@@ -1,18 +1,64 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0207. Course Schedule :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0207. Course Schedule][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0207. Course Schedule][0207. Course Schedule]]
:END:
There are a total of ~numCourses~ courses you have to take, labeled from ~0~ to ~numCourses - 1~. You are given an array ~prerequisites~ where ~prerequisites[i] = [a_{i}, b_{i}]~ indicates that you *must* take course ~b_{i}~ first if you want to take course ~a_{i}~.
- For example, the pair ~[0, 1]~, indicates that to take course ~0~ you have to first take course ~1~.
Return ~true~ if you can finish all courses. Otherwise, return ~false~.
*Example 1:*
#+begin_src
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
#+end_src
*Example 2:*
#+begin_src
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
#+end_src
*Constraints:*
- ~1 <= numCourses <= 2000~
- ~0 <= prerequisites.length <= 5000~
- ~prerequisites[i].length == 2~
- ~0 <= a_{i}, b_{i} < numCourses~
- All the pairs prerequisites[i] are *unique*.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
}
};
#+end_src
@@ -1,18 +1,74 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0210. Course Schedule II :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0210. Course Schedule II][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0210. Course Schedule II][0210. Course Schedule II]]
:END:
There are a total of ~numCourses~ courses you have to take, labeled from ~0~ to ~numCourses - 1~. You are given an array ~prerequisites~ where ~prerequisites[i] = [a_{i}, b_{i}]~ indicates that you *must* take course ~b_{i}~ first if you want to take course ~a_{i}~.
- For example, the pair ~[0, 1]~, indicates that to take course ~0~ you have to first take course ~1~.
Return /the ordering of courses you should take to finish all courses/. If there are many valid answers, return *any* of them. If it is impossible to finish all courses, return *an empty array*.
*Example 1:*
#+begin_src
Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
#+end_src
*Example 2:*
#+begin_src
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
#+end_src
*Example 3:*
#+begin_src
Input: numCourses = 1, prerequisites = []
Output: [0]
#+end_src
*Constraints:*
- ~1 <= numCourses <= 2000~
- ~0 <= prerequisites.length <= numCourses * (numCourses - 1)~
- ~prerequisites[i].length == 2~
- ~0 <= a_{i}, b_{i} < numCourses~
- ~a_{i} != b_{i}~
- All the pairs ~[a_{i}, b_{i}]~ are *distinct*.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
}
};
#+end_src
@@ -1,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0261. Graph Valid Tree :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0261. Graph Valid Tree][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0261. Graph Valid Tree][0261. Graph Valid Tree]]
:END:
** TODO Approach
@@ -1,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0286. Walls And Gates :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0286. Walls And Gates][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0286. Walls And Gates][0286. Walls And Gates]]
:END:
** TODO Approach
@@ -1,7 +1,7 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0323. Number of Connected Components In An Undirected Graph :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0323. Number of Connected Components In An Undirected Graph][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0323. Number of Connected Components In An Undirected Graph][0323. Number of Connected Components In An Undirected Graph]]
:END:
** TODO Approach
@@ -1,18 +1,77 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0417. Pacific Atlantic Water Flow :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0417. Pacific Atlantic Water Flow][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0417. Pacific Atlantic Water Flow][0417. Pacific Atlantic Water Flow]]
:END:
There is an ~m x n~ rectangular island that borders both the *Pacific Ocean* and *Atlantic Ocean*. The *Pacific Ocean* touches the island's left and top edges, and the *Atlantic Ocean* touches the island's right and bottom edges.
The island is partitioned into a grid of square cells. You are given an ~m x n~ integer matrix ~heights~ where ~heights[r][c]~ represents the *height above sea level* of the cell at coordinate ~(r, c)~.
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is *less than or equal to* the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return /a *2D list* of grid coordinates /~result~/ where /~result[i] = [r_{i}, c_{i}]~/ denotes that rain water can flow from cell /~(r_{i}, c_{i})~/ to *both* the Pacific and Atlantic oceans/.
*Example 1:*
#+begin_src
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
#+end_src
*Example 2:*
#+begin_src
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
#+end_src
*Constraints:*
- ~m == heights.length~
- ~n == heights[r].length~
- ~1 <= m, n <= 200~
- ~0 <= heights[r][c] <= 10^{5}~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
}
};
#+end_src
@@ -1,18 +1,64 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0684. Redundant Connection :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0684. Redundant Connection][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0684. Redundant Connection][0684. Redundant Connection]]
:END:
In this problem, a tree is an *undirected graph* that is connected and has no cycles.
You are given a graph that started as a tree with ~n~ nodes labeled from ~1~ to ~n~, with one additional edge added. The added edge has two *different* vertices chosen from ~1~ to ~n~, and was not an edge that already existed. The graph is represented as an array ~edges~ of length ~n~ where ~edges[i] = [a_{i}, b_{i}]~ indicates that there is an edge between nodes ~a_{i}~ and ~b_{i}~ in the graph.
Return /an edge that can be removed so that the resulting graph is a tree of /~n~/ nodes/. If there are multiple answers, return the answer that occurs last in the input.
*Example 1:*
#+begin_src
Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
#+end_src
*Example 2:*
#+begin_src
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
#+end_src
*Constraints:*
- ~n == edges.length~
- ~3 <= n <= 1000~
- ~edges[i].length == 2~
- ~1 <= a_{i} < b_{i} <= edges.length~
- ~a_{i} != b_{i}~
- There are no repeated edges.
- The given graph is connected.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
}
};
#+end_src
@@ -1,18 +1,59 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0695. Max Area of Island :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0695. Max Area of Island][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0695. Max Area of Island][0695. Max Area of Island]]
:END:
You are given an ~m x n~ binary matrix ~grid~. An island is a group of ~1~'s (representing land) connected *4-directionally* (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The *area* of an island is the number of cells with a value ~1~ in the island.
Return /the maximum *area* of an island in /~grid~. If there is no island, return ~0~.
*Example 1:*
#+begin_src
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0
#+end_src
*Constraints:*
- ~m == grid.length~
- ~n == grid[i].length~
- ~1 <= m, n <= 50~
- ~grid[i][j]~ is either ~0~ or ~1~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
}
};
#+end_src
@@ -1,18 +1,69 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0802. Find Eventual Safe States :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0802. Find Eventual Safe States][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0802. Find Eventual Safe States][0802. Find Eventual Safe States]]
:END:
There is a directed graph of ~n~ nodes with each node labeled from ~0~ to ~n - 1~. The graph is represented by a *0-indexed* 2D integer array ~graph~ where ~graph[i]~ is an integer array of nodes adjacent to node ~i~, meaning there is an edge from node ~i~ to each node in ~graph[i]~.
A node is a *terminal node* if there are no outgoing edges. A node is a *safe node* if every possible path starting from that node leads to a *terminal node* (or another safe node).
Return /an array containing all the *safe nodes* of the graph/. The answer should be sorted in *ascending* order.
*Example 1:*
#+begin_src
Input: graph = [[1,2],[2,3],[5],[0],[5],[],[]]
Output: [2,4,5,6]
Explanation: The given graph is shown above.
Nodes 5 and 6 are terminal nodes as there are no outgoing edges from either of them.
Every path starting at nodes 2, 4, 5, and 6 all lead to either node 5 or 6.
#+end_src
*Example 2:*
#+begin_src
Input: graph = [[1,2,3,4],[1,2],[3,4],[0,4],[]]
Output: [4]
Explanation:
Only node 4 is a terminal node, and every path starting at node 4 leads to node 4.
#+end_src
*Constraints:*
- ~n == graph.length~
- ~1 <= n <= 10^{4}~
- ~0 <= graph[i].length <= n~
- ~0 <= graph[i][j] <= n - 1~
- ~graph[i]~ is sorted in a strictly increasing order.
- The graph may contain self-loops.
- The number of edges in the graph will be in the range ~[1, 4 * 10^{4}]~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> eventualSafeNodes(vector<vector<int>>& graph) {
}
};
#+end_src
@@ -1,18 +1,75 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0994. Rotting Oranges :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0994. Rotting Oranges][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0994. Rotting Oranges][0994. Rotting Oranges]]
:END:
You are given an ~m x n~ ~grid~ where each cell can have one of three values:
- ~0~ representing an empty cell,
- ~1~ representing a fresh orange, or
- ~2~ representing a rotten orange.
Every minute, any fresh orange that is *4-directionally adjacent* to a rotten orange becomes rotten.
Return /the minimum number of minutes that must elapse until no cell has a fresh orange/. If /this is impossible, return/ ~-1~.
*Example 1:*
#+begin_src
Input: grid = [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
#+end_src
*Example 3:*
#+begin_src
Input: grid = [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
#+end_src
*Constraints:*
- ~m == grid.length~
- ~n == grid[i].length~
- ~1 <= m, n <= 10~
- ~grid[i][j]~ is ~0~, ~1~, or ~2~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
}
};
#+end_src
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 1905. Count Sub Islands :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*1905. Count Sub Islands][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*1905. Count Sub Islands][1905. Count Sub Islands]]
:END:
You are given two ~m x n~ binary matrices ~grid1~ and ~grid2~ containing only ~0~'s (representing water) and ~1~'s (representing land). An *island* is a group of ~1~'s connected *4-directionally* (horizontal or vertical). Any cells outside of the grid are considered water cells.
An island in ~grid2~ is considered a *sub-island *if there is an island in ~grid1~ that contains *all* the cells that make up *this* island in ~grid2~.
Return the /*number* of islands in /~grid2~ /that are considered *sub-islands*/.
*Example 1:*
#+begin_src
Input: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
#+end_src
*Example 2:*
#+begin_src
Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
#+end_src
*Constraints:*
- ~m == grid1.length == grid2.length~
- ~n == grid1[i].length == grid2[i].length~
- ~1 <= m, n <= 500~
- ~grid1[i][j]~ and ~grid2[i][j]~ are either ~0~ or ~1~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def countSubIslands(self, grid1: List[List[int]], grid2: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
}
};
#+end_src
@@ -1,18 +1,92 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2092. Find All People With Secret :hard:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2092. Find All People With Secret][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2092. Find All People With Secret][2092. Find All People With Secret]]
:END:
You are given an integer ~n~ indicating there are ~n~ people numbered from ~0~ to ~n - 1~. You are also given a *0-indexed* 2D integer array ~meetings~ where ~meetings[i] = [x_{i}, y_{i}, time_{i}]~ indicates that person ~x_{i}~ and person ~y_{i}~ have a meeting at ~time_{i}~. A person may attend *multiple meetings* at the same time. Finally, you are given an integer ~firstPerson~.
Person ~0~ has a *secret* and initially shares the secret with a person ~firstPerson~ at time ~0~. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person ~x_{i}~ has the secret at ~time_{i}~, then they will share the secret with person ~y_{i}~, and vice versa.
The secrets are shared *instantaneously*. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return /a list of all the people that have the secret after all the meetings have taken place. /You may return the answer in *any order*.
*Example 1:*
#+begin_src
Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
Output: [0,1,2,3,5]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 5, person 1 shares the secret with person 2.
At time 8, person 2 shares the secret with person 3.
At time 10, person 1 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
#+end_src
*Example 2:*
#+begin_src
Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
Output: [0,1,3]
Explanation:
At time 0, person 0 shares the secret with person 3.
At time 2, neither person 1 nor person 2 know the secret.
At time 3, person 3 shares the secret with person 0 and person 1.
Thus, people 0, 1, and 3 know the secret after all the meetings.
#+end_src
*Example 3:*
#+begin_src
Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
Output: [0,1,2,3,4]
Explanation:
At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
Note that person 2 can share the secret at the same time as receiving it.
At time 2, person 3 shares the secret with person 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
#+end_src
*Constraints:*
- ~2 <= n <= 10^{5}~
- ~1 <= meetings.length <= 10^{5}~
- ~meetings[i].length == 3~
- ~0 <= x_{i}, y_{i }<= n - 1~
- ~x_{i} != y_{i}~
- ~1 <= time_{i} <= 10^{5}~
- ~1 <= firstPerson <= n - 1~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
}
};
#+end_src
@@ -1,18 +1,70 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2658. Maximum Number of Fish in a Grid :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2658. Maximum Number of Fish in a Grid][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2658. Maximum Number of Fish in a Grid][2658. Maximum Number of Fish in a Grid]]
:END:
You are given a *0-indexed* 2D matrix ~grid~ of size ~m x n~, where ~(r, c)~ represents:
- A *land* cell if ~grid[r][c] = 0~, or
- A *water* cell containing ~grid[r][c]~ fish, if ~grid[r][c] > 0~.
A fisher can start at any *water* cell ~(r, c)~ and can do the following operations any number of times:
- Catch all the fish at cell ~(r, c)~, or
- Move to any adjacent *water* cell.
Return /the *maximum* number of fish the fisher can catch if he chooses his starting cell optimally, or /~0~ if no water cell exists.
An *adjacent* cell of the cell ~(r, c)~, is one of the cells ~(r, c + 1)~, ~(r, c - 1)~, ~(r + 1, c)~ or ~(r - 1, c)~ if it exists.
*Example 1:*
#+begin_src
Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
Output: 7
Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move to cell (2,3) and collect 4 fish.
#+end_src
*Example 2:*
#+begin_src
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
Output: 1
Explanation: The fisher can start at cells (0,0) or (3,3) and collect a single fish.
#+end_src
*Constraints:*
- ~m == grid.length~
- ~n == grid[i].length~
- ~1 <= m, n <= 10~
- ~0 <= grid[i][j] <= 10~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findMaxFish(self, grid: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findMaxFish(vector<vector<int>>& grid) {
}
};
#+end_src
@@ -1,18 +1,78 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2924. Find Champion II :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2924. Find Champion II][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2924. Find Champion II][2924. Find Champion II]]
:END:
There are ~n~ teams numbered from ~0~ to ~n - 1~ in a tournament; each team is also a node in a *DAG*.
You are given the integer ~n~ and a *0-indexed* 2D integer array ~edges~ of length ~m~ representing the *DAG*, where ~edges[i] = [u_{i}, v_{i}]~ indicates that there is a directed edge from team ~u_{i}~ to team ~v_{i}~ in the graph.
A directed edge from ~a~ to ~b~ in the graph means that team ~a~ is *stronger* than team ~b~ and team ~b~ is *weaker* than team ~a~.
Team ~a~ will be the *champion* of the tournament if there is no team ~b~ that is *stronger* than team ~a~.
Return /the team that will be the *champion* of the tournament if there is a *unique* champion, otherwise, return /~-1~/./
*Notes*
- A *cycle* is a series of nodes ~a_{1}, a_{2}, ..., a_{n}, a_{n+1}~ such that node ~a_{1}~ is the same node as node ~a_{n+1}~, the nodes ~a_{1}, a_{2}, ..., a_{n}~ are distinct, and there is a directed edge from the node ~a_{i}~ to node ~a_{i+1}~ for every ~i~ in the range ~[1, n]~.
- A *DAG* is a directed graph that does not have any *cycle*.
*Example 1:*
#+begin_src
Input: n = 3, edges = [[0,1],[1,2]]
Output: 0
Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
#+end_src
*Example 2:*
#+begin_src
Input: n = 4, edges = [[0,2],[1,3],[1,2]]
Output: -1
Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
#+end_src
*Constraints:*
- ~1 <= n <= 100~
- ~m == edges.length~
- ~0 <= m <= n * (n - 1) / 2~
- ~edges[i].length == 2~
- ~0 <= edge[i][j] <= n - 1~
- ~edges[i][0] != edges[i][1]~
- The input is generated such that if team ~a~ is stronger than team ~b~, team ~b~ is not stronger than team ~a~.
- The input is generated such that if team ~a~ is stronger than team ~b~ and team ~b~ is stronger than team ~c~, then team ~a~ is stronger than team ~c~.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def findChampion(self, n: int, edges: List[List[int]]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int findChampion(int n, vector<vector<int>>& edges) {
}
};
#+end_src