Skip to content

Commit d342948

Browse files
committed
Feb-22
1 parent dd7c4a8 commit d342948

19 files changed

+1105
-625
lines changed

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3636

3737
| Status | List | Progress | Notes |
3838
| ----- | ----- | ----- | ----- |
39-
| [18%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 10/54 | - |
39+
| [16%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 9/54 | - |
4040
| [47%] | [leetcode-google.md](./list/leetcode-google.md) | 223/471 | 2 vips |
4141
| [48%] | [leetcode-75.md](./list/leetcode-75.md) | 36/75 | - |
4242
| [51%] | [leetcode-topics.md](./list/leetcode-topics.md) | 74/144 | - |
@@ -53,9 +53,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5353
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5454
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
5555
| [🔲] | [9c-top.md](./list/9c-top.md) | 41/56 | 2 vips |
56-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 24/2207 | 2 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 26/2207 | 3 vips |
5757

58-
**Solved**: 541 problems
58+
**Solved**: 543 problems
5959

6060
## 类型/Category
6161

@@ -237,7 +237,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
237237

238238
## Greedy
239239

240-
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
240+
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
241241
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
242242
| [Leetcode-870](https://leetcode.com/problems/advantage-shuffle/) | Advantage Shuffle | [c++](./leetcode/870.advantage-shuffle.cpp), [python3](./leetcode/870.advantage-shuffle.py) | Greedy | O\(NlogN\) | O\(N\) | - |
243243
| [Leetcode-455](https://leetcode.com/problems/assign-cookies/) | Assign Cookies | [c++](./leetcode/455.assign-cookies.cpp), [python3](./leetcode/455.assign-cookies.py) | Greedy | O\(NlogN\) | O\(1\) | - |
@@ -268,6 +268,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
268268
| [Leetcode-376](https://leetcode.com/problems/wiggle-subsequence/) | Wiggle Subsequence | [c++](./leetcode/376.wiggle-subsequence.cpp), [python3](./leetcode/376.wiggle-subsequence.py) | Greedy | O\(N\) | O\(N\) | - |
269269
| [Leetcode-44](https://leetcode.com/problems/wildcard-matching/) | Wildcard Matching | [c++](./leetcode/44.wildcard-matching.cpp), [python3](./leetcode/44.wildcard-matching.py) | Greedy | O\(NM\) | O\(NM\) | - |
270270
| [Lintcode-3672](https://www.lintcode.com/problem/connecting-cities-with-minimum-cost/) | Connecting Cities With Minimum Cost | [c++](./lintcode/3672.connecting-cities-with-minimum-cost.cpp), [python3](./lintcode/3672.connecting-cities-with-minimum-cost.py) | Greedy | O\(ElogE\) | O\(E\+V\) | Leetcode-1135 |
271+
| [Lintcode-861](https://www.lintcode.com/problem/k-empty-slots/) | K Empty Slots | [c++](./lintcode/861.k-empty-slots.cpp), [python3](./lintcode/861.k-empty-slots.py) | Greedy | O\(N\) | O\(N\) | Leetcode-683 |
271272
| [Lintcode-919](https://www.lintcode.com/problem/meeting-rooms-ii/) | Meeting Rooms II | [c++](./lintcode/919.meeting-rooms-ii.cpp), [python3](./lintcode/919.meeting-rooms-ii.py) | Greedy | O\(NlogN\) | O\(N\) | Leetcode-253 |
272273

273274
## Dynamic Programming
@@ -617,7 +618,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
617618

618619
## Two Pointers
619620

620-
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
621+
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
621622
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
622623
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
623624
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Two Pointers | O\(N^3\) | O\(1\) | - |
@@ -648,11 +649,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
648649
| [Leetcode-167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | Two Sum Ii Input Array Is Sorted | [c++](./leetcode/167.two-sum-ii-input-array-is-sorted.cpp), [python3](./leetcode/167.two-sum-ii-input-array-is-sorted.py) | Two Pointers | O\(N\) | O\(1\) | - |
649650
| [Leetcode-653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | Two Sum Iv Input Is A Bst | [c++](./leetcode/653.two-sum-iv-input-is-a-bst.cpp), [python3](./leetcode/653.two-sum-iv-input-is-a-bst.py) | Two Pointers | O\(NH\) | O\(H\) | - |
650651
| [Leetcode-680](https://leetcode.com/problems/valid-palindrome-ii/) | Valid Palindrome II | [c++](./leetcode/680.valid-palindrome-ii.cpp), [python3](./leetcode/680.valid-palindrome-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
652+
| [Lintcode-861](https://www.lintcode.com/problem/k-empty-slots/) | K Empty Slots | [c++](./lintcode/861.k-empty-slots.cpp), [python3](./lintcode/861.k-empty-slots.py) | Two Pointers | O\(N\) | O\(N\) | Leetcode-683 |
651653
| [Lintcode-386](https://www.lintcode.com/problem/longest-substring-with-at-most-k-distinct-characters/) | Longest Substring With At Most K Distinct Characters | [c++](./lintcode/386.longest-substring-with-at-most-k-distinct-characters.cpp), [python3](./lintcode/386.longest-substring-with-at-most-k-distinct-characters.py) | Two Pointers | O\(N\) | O\(K\) | Leetcode-159, 340 |
652654

653655
## Sliding Window
654656

655-
| Link | Problem(33) | Solution | Tag | Time | Space | Ref |
657+
| Link | Problem(34) | Solution | Tag | Time | Space | Ref |
656658
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
657659
| [Leetcode-2953](https://leetcode.com/problems/count-complete-substrings/) | Count Complete Substrings | [c++](./leetcode/2953.count-complete-substrings.cpp), [python3](./leetcode/2953.count-complete-substrings.py) | Sliding Window | O\(N\) | O\(N\) | - |
658660
| [Leetcode-1652](https://leetcode.com/problems/defuse-the-bomb/) | Defuse The Bomb | [c++](./leetcode/1652.defuse-the-bomb.cpp), [python3](./leetcode/1652.defuse-the-bomb.py) | Sliding Window | O\(N\) | O\(1\) | - |
@@ -686,6 +688,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
686688
| [Lintcode-3724](https://www.lintcode.com/problem/distinct-numbers-in-each-subarray/) | Distinct Numbers In Each Subarray | [c++](./lintcode/3724.distinct-numbers-in-each-subarray.cpp), [python3](./lintcode/3724.distinct-numbers-in-each-subarray.py) | Sliding Window | O\(N\) | O\(K\) | Leetcode-1852 |
687689
| [Lintcode-3675](https://www.lintcode.com/problem/minimum-swaps-to-group-all-1s-together/) | Minimum Swaps To Group All 1S Together | [c++](./lintcode/3675.minimum-swaps-to-group-all-1s-together.cpp), [python3](./lintcode/3675.minimum-swaps-to-group-all-1s-together.py) | Sliding Window | O\(N\) | O\(1\) | Leetcode-1151 |
688690
| [Lintcode-642](https://www.lintcode.com/problem/moving-average-from-data-stream/) | Moving Average From Data Stream | [c++](./lintcode/642.moving-average-from-data-stream.cpp), [python3](./lintcode/642.moving-average-from-data-stream.py) | Sliding Window | O\(1\) | O\(K\) | Leetcode-346 |
691+
| [Lintcode-3751](https://www.lintcode.com/problem/number-of-equal-count-substrings/) | Number Of Equal Count Substrings | [c++](./lintcode/3751.number-of-equal-count-substrings.cpp), [python3](./lintcode/3751.number-of-equal-count-substrings.py) | Sliding Window | O\(N\) | O\(N\) | Leetcode-2067 |
689692
| [Lintcode-3756](https://www.lintcode.com/problem/number-of-unique-flavors-remaining-after-sharing-k-candies/) | Number Of Unique Flavors Remaining After Sharing K Candies | [c++](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.cpp), [python3](./lintcode/3756.number-of-unique-flavors-remaining-after-sharing-k-candies.py) | Sliding Window | O\(N\) | O\(N\) | Leetcode-2107 |
690693

691694
## Sorting
@@ -834,7 +837,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
834837

835838
## Depth-First Search
836839

837-
| Link | Problem(57) | Solution | Tag | Time | Space | Ref |
840+
| Link | Problem(58) | Solution | Tag | Time | Space | Ref |
838841
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
839842
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Depth-First Search | O\(MlogM\) | O\(N\) | - |
840843
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
@@ -875,6 +878,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
875878
| [Leetcode-1631](https://leetcode.com/problems/path-with-minimum-effort/) | Path With Minimum Effort | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Depth-First Search | O\(NMlogNM\) | O\(NM\) | - |
876879
| [Leetcode-938](https://leetcode.com/problems/range-sum-of-bst/) | Range Sum Of Bst | [c++](./leetcode/938.range-sum-of-bst.cpp), [python3](./leetcode/938.range-sum-of-bst.py) | Depth-First Search | O\(N\) | O\(W\) | - |
877880
| [Leetcode-332](https://leetcode.com/problems/reconstruct-itinerary/) | Reconstruct Itinerary | [c++](./leetcode/332.reconstruct-itinerary.cpp), [python3](./leetcode/332.reconstruct-itinerary.py) | Depth-First Search | O\(ElogE\) | O\(E\+V\) | - |
881+
| [Leetcode-1028](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | Recover A Tree From Preorder Traversal | [c++](./leetcode/1028.recover-a-tree-from-preorder-traversal.cpp), [python3](./leetcode/1028.recover-a-tree-from-preorder-traversal.py) | Depth-First Search | O\(N\) | O\(H\) | - |
878882
| [Leetcode-99](https://leetcode.com/problems/recover-binary-search-tree/) | Recover Binary Search Tree | [c++](./leetcode/99.recover-binary-search-tree.cpp), [python3](./leetcode/99.recover-binary-search-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
879883
| [Leetcode-684](https://leetcode.com/problems/redundant-connection/) | Redundant Connection | [c++](./leetcode/684.redundant-connection.cpp), [python3](./leetcode/684.redundant-connection.py) | Depth-First Search | O\(N\) | O\(1\) | - |
880884
| [Leetcode-100](https://leetcode.com/problems/same-tree/) | Same Tree | [c++](./leetcode/100.same-tree.cpp), [python3](./leetcode/100.same-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
@@ -1075,7 +1079,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
10751079

10761080
## Other
10771081

1078-
| Link | Problem(114) | Solution | Tag | Time | Space | Ref |
1082+
| Link | Problem(113) | Solution | Tag | Time | Space | Ref |
10791083
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
10801084
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
10811085
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -1120,7 +1124,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
11201124
| [Leetcode-771](https://leetcode.com/problems/jewels-and-stones/) | Jewels And Stones | [c++](./leetcode/771.jewels-and-stones.cpp) | Other | \- | \- | - |
11211125
| [Leetcode-45](https://leetcode.com/problems/jump-game-ii/) | Jump Game II | [c++](./leetcode/45.jump-game-ii.cpp), [python3](./leetcode/45.jump-game-ii.py) | Other | \- | \- | - |
11221126
| [Leetcode-532](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | K Diff Pairs In An Array | [c++](./leetcode/532.k-diff-pairs-in-an-array.cpp), [python3](./leetcode/532.k-diff-pairs-in-an-array.py) | Other | \- | \- | - |
1123-
| [Leetcode-683](https://leetcode.com/problems/k-empty-slots/) | K Empty Slots | [c++](./leetcode/683.k-empty-slots.cpp), [python3](./leetcode/683.k-empty-slots.py) | Other | \- | \- | - |
11241127
| [Leetcode-368](https://leetcode.com/problems/largest-divisible-subset/) | Largest Divisible Subset | [c++](./leetcode/368.largest-divisible-subset.cpp), [python3](./leetcode/368.largest-divisible-subset.py) | Other | \- | \- | - |
11251128
| [Leetcode-747](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | Largest Number At Least Twice Of Others | [c++](./leetcode/747.largest-number-at-least-twice-of-others.cpp) | Other | \- | \- | - |
11261129
| [Leetcode-17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | Letter Combinations Of A Phone Number | [c++](./leetcode/17.letter-combinations-of-a-phone-number.cpp), [python3](./leetcode/17.letter-combinations-of-a-phone-number.py) | Other | \- | \- | - |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Tag: String, Tree, Depth-First Search, Binary Tree
2+
// Time: O(N)
3+
// Space: O(H)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/dcu39zy0Uac
7+
8+
// We run a preorder depth-first search (DFS) on the root of a binary tree.
9+
// At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  If the depth of a node is D, the depth of its immediate child is D + 1.  The depth of the root node is 0.
10+
// If a node has only one child, that child is guaranteed to be the left child.
11+
// Given the output traversal of this traversal, recover the tree and return its root.
12+
//  
13+
// Example 1:
14+
//
15+
//
16+
// Input: traversal = "1-2--3--4-5--6--7"
17+
// Output: [1,2,5,3,4,6,7]
18+
//
19+
// Example 2:
20+
//
21+
//
22+
// Input: traversal = "1-2--3---4-5--6---7"
23+
// Output: [1,2,5,3,null,6,null,4,null,7]
24+
//
25+
// Example 3:
26+
//
27+
//
28+
// Input: traversal = "1-401--349---90--88"
29+
// Output: [1,401,null,349,88,90]
30+
//
31+
//  
32+
// Constraints:
33+
//
34+
// The number of nodes in the original tree is in the range [1, 1000].
35+
// 1 <= Node.val <= 109
36+
//
37+
//
38+
39+
/**
40+
* Definition for a binary tree node.
41+
* struct TreeNode {
42+
* int val;
43+
* TreeNode *left;
44+
* TreeNode *right;
45+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
46+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
47+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
48+
* };
49+
*/
50+
class Solution {
51+
public:
52+
TreeNode* recoverFromPreorder(string traversal) {
53+
stack<TreeNode *> st;
54+
vector<pair<int, int>> nodes = parsePreorder(traversal);
55+
for (auto &[depth, val]: nodes) {
56+
while (st.size() > depth) {
57+
st.pop();
58+
}
59+
60+
TreeNode *node = new TreeNode(val);
61+
if (!st.empty()) {
62+
if (!st.top()->left) {
63+
st.top()->left = node;
64+
} else {
65+
st.top()->right = node;
66+
}
67+
}
68+
st.push(node);
69+
}
70+
TreeNode *root = nullptr;
71+
while (!st.empty()) {
72+
root = st.top();
73+
st.pop();
74+
}
75+
return root;
76+
}
77+
78+
vector<pair<int, int>> parsePreorder(string &traversal) {
79+
vector<pair<int, int>> nodes;
80+
int i = 0;
81+
int n = traversal.size();
82+
while (i < n) {
83+
int depth = 0;
84+
while (i < n && traversal[i] == '-') {
85+
depth += 1;
86+
i += 1;
87+
}
88+
89+
int val = 0;
90+
while (i < n && traversal[i] != '-') {
91+
val = val * 10 + traversal[i] - '0';
92+
i += 1;
93+
}
94+
nodes.emplace_back(depth, val);
95+
}
96+
97+
return nodes;
98+
}
99+
100+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Tag: String, Tree, Depth-First Search, Binary Tree
2+
# Time: O(N)
3+
# Space: O(H)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/dcu39zy0Uac
7+
8+
# We run a preorder depth-first search (DFS) on the root of a binary tree.
9+
# At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  If the depth of a node is D, the depth of its immediate child is D + 1.  The depth of the root node is 0.
10+
# If a node has only one child, that child is guaranteed to be the left child.
11+
# Given the output traversal of this traversal, recover the tree and return its root.
12+
#  
13+
# Example 1:
14+
#
15+
#
16+
# Input: traversal = "1-2--3--4-5--6--7"
17+
# Output: [1,2,5,3,4,6,7]
18+
#
19+
# Example 2:
20+
#
21+
#
22+
# Input: traversal = "1-2--3---4-5--6---7"
23+
# Output: [1,2,5,3,null,6,null,4,null,7]
24+
#
25+
# Example 3:
26+
#
27+
#
28+
# Input: traversal = "1-401--349---90--88"
29+
# Output: [1,401,null,349,88,90]
30+
#
31+
#  
32+
# Constraints:
33+
#
34+
# The number of nodes in the original tree is in the range [1, 1000].
35+
# 1 <= Node.val <= 109
36+
#
37+
#
38+
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, val=0, left=None, right=None):
42+
# self.val = val
43+
# self.left = left
44+
# self.right = right
45+
class Solution:
46+
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
47+
nodes = self.parsePreorder(traversal)
48+
stack = []
49+
for depth, val in nodes:
50+
while depth < len(stack):
51+
stack.pop()
52+
53+
node = TreeNode(val)
54+
if len(stack) > 0:
55+
if stack[-1].left is None:
56+
stack[-1].left = node
57+
else:
58+
stack[-1].right = node
59+
60+
stack.append(node)
61+
62+
return stack[0]
63+
64+
65+
def parsePreorder(self, traversal: str) -> list:
66+
i = 0
67+
n = len(traversal)
68+
nodes = []
69+
while i < len(traversal):
70+
depth = 0
71+
while i < n and traversal[i] == '-':
72+
depth += 1
73+
i += 1
74+
75+
val = 0
76+
while i < n and traversal[i] != '-':
77+
val = val * 10 + int(traversal[i])
78+
i += 1
79+
80+
nodes.append((depth, val))
81+
return nodes

leetcode/2524.maximum-frequency-score-of-a-subarray.vip

Whitespace-only changes.

leetcode/683.k-empty-slots.cpp

-41
This file was deleted.

leetcode/683.k-empty-slots.py

-11
This file was deleted.

0 commit comments

Comments
 (0)