Skip to content

Commit 06a8d6e

Browse files
add leetcode solutions
1 parent 676d6bc commit 06a8d6e

File tree

117 files changed

+2799
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+2799
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int largestRectangleArea(vector<int>& histo) {
4+
stack<int> st;
5+
int maxA = 0, n = histo.size();
6+
for (int i = 0; i <= n; i++) {
7+
while (st.size() && (i == n || histo[st.top()] >= histo[i])) {
8+
int height = histo[st.top()]; st.pop();
9+
int width = st.empty() ? i: i - st.top() - 1;
10+
maxA = max(maxA, width * height);
11+
}
12+
st.push(i);
13+
}
14+
return maxA;
15+
}
16+
17+
int maximalRectangle(vector<vector<char>>& matrix) {
18+
int maxiArea = 0, n = matrix.size(), m = matrix[0].size();
19+
vector<int> height(m, 0);
20+
for (int i = 0; i < n; i++) {
21+
for (int j = 0; j < m; j++)
22+
if (matrix[i][j] == '1') height[j]++; else height[j] = 0;
23+
int area = largestRectangleArea(height);
24+
maxiArea = max(maxiArea, area);
25+
}
26+
return maxiArea;
27+
}
28+
};
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h2><a href="https://leetcode.com/problems/maximal-rectangle/">85. Maximal Rectangle</a></h2><h3>Hard</h3><hr><div><p>Given a <code>rows x cols</code>&nbsp;binary <code>matrix</code> filled with <code>0</code>'s and <code>1</code>'s, find the largest rectangle containing only <code>1</code>'s and return <em>its area</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg" style="width: 402px; height: 322px;">
6+
<pre><strong>Input:</strong> matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
7+
<strong>Output:</strong> 6
8+
<strong>Explanation:</strong> The maximal rectangle is shown in the above picture.
9+
</pre>
10+
11+
<p><strong class="example">Example 2:</strong></p>
12+
13+
<pre><strong>Input:</strong> matrix = [["0"]]
14+
<strong>Output:</strong> 0
15+
</pre>
16+
17+
<p><strong class="example">Example 3:</strong></p>
18+
19+
<pre><strong>Input:</strong> matrix = [["1"]]
20+
<strong>Output:</strong> 1
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
<p><strong>Constraints:</strong></p>
25+
26+
<ul>
27+
<li><code>rows == matrix.length</code></li>
28+
<li><code>cols == matrix[i].length</code></li>
29+
<li><code>1 &lt;= row, cols &lt;= 200</code></li>
30+
<li><code>matrix[i][j]</code> is <code>'0'</code> or <code>'1'</code>.</li>
31+
</ul>
32+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<int> findOrder(int n, vector<vector<int>>& prereq) {
4+
vector<vector<int>> g(n);
5+
vector<int> indegree(n);
6+
for (auto e: prereq) {
7+
auto take = e[0], pre = e[1];
8+
g[pre].push_back(take),
9+
indegree[take]++;
10+
}
11+
queue<int> q;
12+
for (auto i = 0; i < n; i++) if (!indegree[i]) q.push(i);
13+
vector<int> res;
14+
while (q.size()) {
15+
auto course = q.front(); q.pop();
16+
res.push_back(course);
17+
for (auto to: g[course]) if (--indegree[to] == 0) q.push(to);
18+
}
19+
return res.size() == n ? res: vector<int>{};
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h2><a href="https://leetcode.com/problems/course-schedule-ii/">210. Course Schedule II</a></h2><h3>Medium</h3><hr><div><p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p>
2+
3+
<ul>
4+
<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li>
5+
</ul>
6+
7+
<p>Return <em>the ordering of courses you should take to finish all courses</em>. If there are many valid answers, return <strong>any</strong> of them. If it is impossible to finish all courses, return <strong>an empty array</strong>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
13+
<strong>Output:</strong> [0,1]
14+
<strong>Explanation:</strong> 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].
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
20+
<strong>Output:</strong> [0,2,1,3]
21+
<strong>Explanation:</strong> 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.
22+
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre><strong>Input:</strong> numCourses = 1, prerequisites = []
28+
<strong>Output:</strong> [0]
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= numCourses &lt;= 2000</code></li>
36+
<li><code>0 &lt;= prerequisites.length &lt;= numCourses * (numCourses - 1)</code></li>
37+
<li><code>prerequisites[i].length == 2</code></li>
38+
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
39+
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
40+
<li>All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are <strong>distinct</strong>.</li>
41+
</ul>
42+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int addDigits(int num) {
4+
5+
auto get_digit_sum = [](int n, int sum = 0) {
6+
while (n) sum += n % 10, n /= 10;
7+
return sum;
8+
};
9+
do num = get_digit_sum(num);
10+
while (num > 9 && num != 2);
11+
return num;
12+
}
13+
};

leetcode/0258-add-digits/NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

leetcode/0258-add-digits/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2><a href="https://leetcode.com/problems/add-digits/">258. Add Digits</a></h2><h3>Easy</h3><hr><div><p>Given an integer <code>num</code>, repeatedly add all its digits until the result has only one digit, and return it.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre><strong>Input:</strong> num = 38
7+
<strong>Output:</strong> 2
8+
<strong>Explanation:</strong> The process is
9+
38 --&gt; 3 + 8 --&gt; 11
10+
11 --&gt; 1 + 1 --&gt; 2
11+
Since 2 has only one digit, return it.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre><strong>Input:</strong> num = 0
17+
<strong>Output:</strong> 0
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>0 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
25+
</ul>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Follow up:</strong> Could you do it without any loop/recursion in <code>O(1)</code> runtime?</p>
29+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int bulbSwitch(int n) {
4+
return sqrt(n);
5+
}
6+
};

leetcode/0319-bulb-switcher/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<h2><a href="https://leetcode.com/problems/bulb-switcher/">319. Bulb Switcher</a></h2><h3>Medium</h3><hr><div><p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then&nbsp;you turn off every second bulb.</p>
2+
3+
<p>On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</code> bulb. For the <code>n<sup>th</sup></code> round, you only toggle the last bulb.</p>
4+
5+
<p>Return <em>the number of bulbs that are on after <code>n</code> rounds</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg" style="width: 421px; height: 321px;">
10+
<pre><strong>Input:</strong> n = 3
11+
<strong>Output:</strong> 1
12+
<strong>Explanation:</strong> At first, the three bulbs are [off, off, off].
13+
After the first round, the three bulbs are [on, on, on].
14+
After the second round, the three bulbs are [on, off, on].
15+
After the third round, the three bulbs are [on, off, off].
16+
So you should return 1 because there is only one bulb is on.</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre><strong>Input:</strong> n = 0
21+
<strong>Output:</strong> 0
22+
</pre>
23+
24+
<p><strong class="example">Example 3:</strong></p>
25+
26+
<pre><strong>Input:</strong> n = 1
27+
<strong>Output:</strong> 1
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
35+
</ul>
36+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
// Definition for a QuadTree node.
3+
class Node {
4+
public:
5+
bool val;
6+
bool isLeaf;
7+
Node* topLeft;
8+
Node* topRight;
9+
Node* bottomLeft;
10+
Node* bottomRight;
11+
12+
Node() {
13+
val = false;
14+
isLeaf = false;
15+
topLeft = NULL;
16+
topRight = NULL;
17+
bottomLeft = NULL;
18+
bottomRight = NULL;
19+
}
20+
21+
Node(bool _val, bool _isLeaf) {
22+
val = _val;
23+
isLeaf = _isLeaf;
24+
topLeft = NULL;
25+
topRight = NULL;
26+
bottomLeft = NULL;
27+
bottomRight = NULL;
28+
}
29+
30+
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
31+
val = _val;
32+
isLeaf = _isLeaf;
33+
topLeft = _topLeft;
34+
topRight = _topRight;
35+
bottomLeft = _bottomLeft;
36+
bottomRight = _bottomRight;
37+
}
38+
};
39+
*/
40+
41+
class Solution {
42+
public:
43+
Node* construct(vector<vector<int>>& grid) {
44+
function<Node*(int, int, int)> go = [&](auto x, auto y, auto len) {
45+
auto val = grid[x][y];
46+
auto isDiff = [&]() {
47+
for (int i = x; i < x + len; i++)
48+
for (int j = y; j < y + len; j++)
49+
if (grid[i][j] != val) return true;
50+
return false;
51+
};
52+
if (isDiff()) {
53+
auto l = len >> 1;
54+
auto tl = go(x, y, l), tr = go(x , y + l, l);
55+
auto bl = go(x + l, y, l), br = go(x + l, y + l, l);
56+
return new Node(val, false, tl, tr, bl, br);
57+
}
58+
return new Node(val, true);
59+
};
60+
return go(0, 0, grid.size());
61+
}
62+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<h2><a href="https://leetcode.com/problems/construct-quad-tree/">427. Construct Quad Tree</a></h2><h3>Medium</h3><hr><div><p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0's</code> and <code>1's</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p>
2+
3+
<p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p>
4+
5+
<p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p>
6+
7+
<ul>
8+
<li><code>val</code>: True if the node represents a grid of 1's or False if the node represents a grid of 0's. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li>
9+
<li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li>
10+
</ul>
11+
12+
<pre>class Node {
13+
public boolean val;
14+
public boolean isLeaf;
15+
public Node topLeft;
16+
public Node topRight;
17+
public Node bottomLeft;
18+
public Node bottomRight;
19+
}</pre>
20+
21+
<p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p>
22+
23+
<ol>
24+
<li>If the current grid has the same value (i.e all <code>1's</code> or all <code>0's</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li>
25+
<li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li>
26+
<li>Recurse for each of the children with the proper sub-grid.</li>
27+
</ol>
28+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;">
29+
<p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p>
30+
31+
<p><strong>Quad-Tree format:</strong></p>
32+
33+
<p>You don't need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p>
34+
35+
<p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p>
36+
37+
<p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p>
38+
39+
<p>&nbsp;</p>
40+
<p><strong class="example">Example 1:</strong></p>
41+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;">
42+
<pre><strong>Input:</strong> grid = [[0,1],[1,0]]
43+
<strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]]
44+
<strong>Explanation:</strong> The explanation of this example is shown below:
45+
Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree.
46+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;">
47+
</pre>
48+
49+
<p><strong class="example">Example 2:</strong></p>
50+
51+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;"></p>
52+
53+
<pre><strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
54+
<strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
55+
<strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids.
56+
The topLeft, bottomLeft and bottomRight each has the same value.
57+
The topRight have different values so we divide it into 4 sub-grids where each has the same value.
58+
Explanation is shown in the photo below:
59+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;">
60+
</pre>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>n == grid.length == grid[i].length</code></li>
67+
<li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li>
68+
</ul>
69+
</div>

leetcode/0502-ipo copy/0502-ipo.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int findMaximizedCapital(int k,
4+
int w,
5+
vector<int>& pfs,
6+
vector<int>& cap) {
7+
int n = pfs.size();
8+
vector<pair<int, int>> p;
9+
for (int i = 0; i < n; i++) p.emplace_back(cap[i], pfs[i]);
10+
sort(p.begin(), p.end());
11+
12+
priority_queue<int> pq;
13+
for (int i = 0, curr = 0; i < k; i++) {
14+
while (curr < n && p[curr].first <= w) pq.push(p[curr++].second);
15+
if (pq.empty()) break;
16+
w += pq.top();
17+
pq.pop();
18+
}
19+
return w;
20+
}
21+
};

leetcode/0502-ipo copy/NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)