Skip to content

Commit 6021a3c

Browse files
committed
Feb-18
1 parent 585eff8 commit 6021a3c

6 files changed

+220
-33
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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) | 18/2207 | 2 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 19/2207 | 2 vips |
5757

58-
**Solved**: 534 problems
58+
**Solved**: 535 problems
5959

6060
## 类型/Category
6161

@@ -236,14 +236,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
236236

237237
## Greedy
238238

239-
| Link | Problem(29) | Solution | Tag | Time | Space | Ref |
239+
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
240240
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
241241
| [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\) | - |
242242
| [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\) | - |
243243
| [Leetcode-122](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | Best Time To Buy And Sell Stock II | [c++](./leetcode/122.best-time-to-buy-and-sell-stock-ii.cpp), [python3](./leetcode/122.best-time-to-buy-and-sell-stock-ii.py) | Greedy | O\(N\) | O\(1\) | - |
244244
| [Leetcode-714](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/) | Best Time To Buy And Sell Stock With Transaction Fee | [c++](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.cpp), [python3](./leetcode/714.best-time-to-buy-and-sell-stock-with-transaction-fee.py) | Greedy | O\(N\) | O\(N\) | - |
245245
| [Leetcode-605](https://leetcode.com/problems/can-place-flowers/) | Can Place Flowers | [c++](./leetcode/605.can-place-flowers.cpp), [python3](./leetcode/605.can-place-flowers.py) | Greedy | O\(N\) | O\(N\) | - |
246246
| [Leetcode-135](https://leetcode.com/problems/candy/) | Candy | [c++](./leetcode/135.candy.cpp), [python3](./leetcode/135.candy.py) | Greedy | O\(N\) | O\(N\) | - |
247+
| [Leetcode-2375](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | Construct Smallest Number From Di String | [c++](./leetcode/2375.construct-smallest-number-from-di-string.cpp), [python3](./leetcode/2375.construct-smallest-number-from-di-string.py) | Greedy | O\(N\) | O\(N\) | - |
247248
| [Leetcode-3048](https://leetcode.com/problems/earliest-second-to-mark-indices-i/) | Earliest Second To Mark Indices I | [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp), [python3](./leetcode/3048.earliest-second-to-mark-indices-i.py) | Greedy | O\(NlogN\) | O\(N\) | - |
248249
| [Leetcode-134](https://leetcode.com/problems/gas-station/) | Gas Station | [c++](./leetcode/134.gas-station.cpp), [python3](./leetcode/134.gas-station.py) | Greedy | O\(N\) | O\(1\) | - |
249250
| [Leetcode-846](https://leetcode.com/problems/hand-of-straights/) | Hand Of Straights | [c++](./leetcode/846.hand-of-straights.cpp), [python3](./leetcode/846.hand-of-straights.py) | Greedy | O\(NlogN\) | O\(N\) | - |
@@ -515,7 +516,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
515516

516517
## Stack
517518

518-
| Link | Problem(25) | Solution | Tag | Time | Space | Ref |
519+
| Link | Problem(26) | Solution | Tag | Time | Space | Ref |
519520
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
520521
| [Leetcode-735](https://leetcode.com/problems/asteroid-collision/) | Asteroid Collision | [c++](./leetcode/735.asteroid-collision.cpp), [python3](./leetcode/735.asteroid-collision.py) | Stack | O\(N\) | O\(N\) | - |
521522
| [Leetcode-224](https://leetcode.com/problems/basic-calculator/) | Basic Calculator | [c++](./leetcode/224.basic-calculator.cpp), [python3](./leetcode/224.basic-calculator.py) | Stack | O\(N\) | O\(N\) | - |
@@ -525,6 +526,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
525526
| [Leetcode-145](https://leetcode.com/problems/binary-tree-postorder-traversal/) | Binary Tree Postorder Traversal | [c++](./leetcode/145.binary-tree-postorder-traversal.cpp), [python3](./leetcode/145.binary-tree-postorder-traversal.py) | Stack | O\(N\) | O\(H\) | - |
526527
| [Leetcode-144](https://leetcode.com/problems/binary-tree-preorder-traversal/) | Binary Tree Preorder Traversal | [c++](./leetcode/144.binary-tree-preorder-traversal.cpp), [python3](./leetcode/144.binary-tree-preorder-traversal.py) | Stack | O\(N\) | O\(H\) | - |
527528
| [Leetcode-3174](https://leetcode.com/problems/clear-digits/) | Clear Digits | [c++](./leetcode/3174.clear-digits.cpp), [python3](./leetcode/3174.clear-digits.py) | Stack | O\(N\) | O\(N\) | - |
529+
| [Leetcode-2375](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | Construct Smallest Number From Di String | [c++](./leetcode/2375.construct-smallest-number-from-di-string.cpp), [python3](./leetcode/2375.construct-smallest-number-from-di-string.py) | Stack | O\(N\) | O\(N\) | - |
528530
| [Leetcode-394](https://leetcode.com/problems/decode-string/) | Decode String | [c++](./leetcode/394.decode-string.cpp), [python3](./leetcode/394.decode-string.py) | Stack | O\(A\) | O\(A\) | - |
529531
| [Leetcode-150](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Evaluate Reverse Polish Notation | [c++](./leetcode/150.evaluate-reverse-polish-notation.cpp), [python3](./leetcode/150.evaluate-reverse-polish-notation.py) | Stack | O\(N\) | O\(N\) | - |
530532
| [Leetcode-114](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | Flatten Binary Tree To Linked List | [c++](./leetcode/114.flatten-binary-tree-to-linked-list.cpp), [python3](./leetcode/114.flatten-binary-tree-to-linked-list.py) | Stack | O\(N\) | O\(H\) | - |
@@ -903,11 +905,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
903905

904906
## Backtracking
905907

906-
| Link | Problem(17) | Solution | Tag | Time | Space | Ref |
908+
| Link | Problem(18) | Solution | Tag | Time | Space | Ref |
907909
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
908910
| [Leetcode-257](https://leetcode.com/problems/binary-tree-paths/) | Binary Tree Paths | [c++](./leetcode/257.binary-tree-paths.cpp), [python3](./leetcode/257.binary-tree-paths.py) | Backtracking | O\(N\) | O\(H\) | - |
909911
| [Leetcode-40](https://leetcode.com/problems/combination-sum-ii/) | Combination Sum II | [c++](./leetcode/40.combination-sum-ii.cpp), [python3](./leetcode/40.combination-sum-ii.py) | Backtracking | O\(2^N\) | O\(N\) | - |
910912
| [Leetcode-77](https://leetcode.com/problems/combinations/) | Combinations | [c++](./leetcode/77.combinations.cpp), [python3](./leetcode/77.combinations.py) | Backtracking | O\(k \* C\(n, k\)\) | O\(K\) | - |
913+
| [Leetcode-2375](https://leetcode.com/problems/construct-smallest-number-from-di-string/) | Construct Smallest Number From Di String | [c++](./leetcode/2375.construct-smallest-number-from-di-string.cpp), [python3](./leetcode/2375.construct-smallest-number-from-di-string.py) | Backtracking | O\(N\) | O\(N\) | - |
911914
| [Leetcode-1718](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/) | Construct The Lexicographically Largest Valid Sequence | [c++](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.cpp), [python3](./leetcode/1718.construct-the-lexicographically-largest-valid-sequence.py) | Backtracking | O\(N\!\) | O\(N\) | - |
912915
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Backtracking | O\(N \* 2^K\) | O\(K\) | - |
913916
| [Leetcode-22](https://leetcode.com/problems/generate-parentheses/) | Generate Parentheses | [c++](./leetcode/22.generate-parentheses.cpp), [python3](./leetcode/22.generate-parentheses.py) | Backtracking | O\(2^N\) | O\(N\) | - |
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Tag: String, Backtracking, Stack, Greedy
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/P8i_9XLrj3w
7+
8+
// You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.
9+
// A 0-indexed string num of length n + 1 is created using the following conditions:
10+
//
11+
// num consists of the digits '1' to '9', where each digit is used at most once.
12+
// If pattern[i] == 'I', then num[i] < num[i + 1].
13+
// If pattern[i] == 'D', then num[i] > num[i + 1].
14+
//
15+
// Return the lexicographically smallest possible string num that meets the conditions.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: pattern = "IIIDIDDD"
20+
// Output: "123549876"
21+
// Explanation:
22+
// At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
23+
// At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
24+
// Some possible values of num are "245639871", "135749862", and "123849765".
25+
// It can be proven that "123549876" is the smallest possible num that meets the conditions.
26+
// Note that "123414321" is not possible because the digit '1' is used more than once.
27+
// Example 2:
28+
//
29+
// Input: pattern = "DDD"
30+
// Output: "4321"
31+
// Explanation:
32+
// Some possible values of num are "9876", "7321", and "8742".
33+
// It can be proven that "4321" is the smallest possible num that meets the conditions.
34+
//
35+
//  
36+
// Constraints:
37+
//
38+
// 1 <= pattern.length <= 8
39+
// pattern consists of only the letters 'I' and 'D'.
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
string smallestNumber(string pattern) {
46+
int n = pattern.size();
47+
vector<bool> used(10, false);
48+
vector<int> res(n + 1, 0);
49+
50+
helper(pattern, 0, res, used);
51+
52+
string result;
53+
for (int num : res) {
54+
result += to_string(num);
55+
}
56+
return result;
57+
}
58+
59+
bool helper(const string &pattern, int i, vector<int> &res, vector<bool> &used) {
60+
if (i == res.size()) {
61+
return true;
62+
}
63+
64+
for (int num = 1; num <= 9; num++) {
65+
if (used[num]) continue;
66+
if (i == 0 ||
67+
(pattern[i - 1] == 'I' && num > res[i - 1]) ||
68+
(pattern[i - 1] == 'D' && num < res[i - 1])) {
69+
res[i] = num;
70+
used[num] = true;
71+
if (helper(pattern, i + 1, res, used)) {
72+
return true;
73+
}
74+
res[i] = 0;
75+
used[num] = false;
76+
}
77+
}
78+
79+
return false;
80+
}
81+
};
82+
83+
class Solution {
84+
public:
85+
string smallestNumber(string pattern) {
86+
int n = pattern.size();
87+
string res;
88+
stack<char> st;
89+
for (int i = 0; i <= n; i++) {
90+
st.push('1' + i);
91+
if (i == n || pattern[i] == 'I') {
92+
while (!st.empty()) {
93+
res += st.top();
94+
st.pop();
95+
}
96+
}
97+
}
98+
return res;
99+
}
100+
};
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Tag: String, Backtracking, Stack, Greedy
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/P8i_9XLrj3w
7+
8+
# You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.
9+
# A 0-indexed string num of length n + 1 is created using the following conditions:
10+
#
11+
# num consists of the digits '1' to '9', where each digit is used at most once.
12+
# If pattern[i] == 'I', then num[i] < num[i + 1].
13+
# If pattern[i] == 'D', then num[i] > num[i + 1].
14+
#
15+
# Return the lexicographically smallest possible string num that meets the conditions.
16+
#  
17+
# Example 1:
18+
#
19+
# Input: pattern = "IIIDIDDD"
20+
# Output: "123549876"
21+
# Explanation:
22+
# At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
23+
# At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
24+
# Some possible values of num are "245639871", "135749862", and "123849765".
25+
# It can be proven that "123549876" is the smallest possible num that meets the conditions.
26+
# Note that "123414321" is not possible because the digit '1' is used more than once.
27+
# Example 2:
28+
#
29+
# Input: pattern = "DDD"
30+
# Output: "4321"
31+
# Explanation:
32+
# Some possible values of num are "9876", "7321", and "8742".
33+
# It can be proven that "4321" is the smallest possible num that meets the conditions.
34+
#
35+
#  
36+
# Constraints:
37+
#
38+
# 1 <= pattern.length <= 8
39+
# pattern consists of only the letters 'I' and 'D'.
40+
#
41+
#
42+
43+
class Solution:
44+
def smallestNumber(self, pattern: str) -> str:
45+
n = len(pattern)
46+
res = [0] * (n + 1)
47+
used = [False] * 10
48+
self.helper(pattern, used, res, 0)
49+
return ''.join([str(x) for x in res])
50+
51+
def helper(self, pattern: str, used: list, res: list, i: int) -> bool:
52+
if i == len(res):
53+
return True
54+
55+
for num in range(1, 10):
56+
if used[num]:
57+
continue
58+
59+
if (i == 0) or \
60+
(pattern[i - 1] == 'I' and num > res[i - 1]) or \
61+
(pattern[i - 1] == 'D' and num < res[i - 1]):
62+
63+
used[num] = True
64+
res[i] = num
65+
if self.helper(pattern, used, res, i + 1):
66+
return True
67+
res[i] = 0
68+
used[num] = False
69+
70+
return False
71+
72+
class Solution:
73+
def smallestNumber(self, pattern: str) -> str:
74+
n = len(pattern)
75+
stack = []
76+
res = []
77+
for i in range(0, n + 1):
78+
stack.append(str(i + 1))
79+
if i == n or pattern[i] == 'I':
80+
res += stack[::-1]
81+
stack = []
82+
return ''.join(res)

leetcode/567.permutation-in-string.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,35 @@
2929
class Solution {
3030
public:
3131
bool checkInclusion(string s1, string s2) {
32+
int n = s2.size();
33+
int k = s1.size();
3234
unordered_map<char, int> counter;
3335
for (char x: s1) {
3436
counter[x] += 1;
3537
}
36-
38+
3739
int hit = 0;
38-
int j = 0;
39-
for (int i = 0; i < s2.size(); i++) {
40+
for (int i = 0; i < n; i++) {
41+
if (i >= k) {
42+
if (counter.count(s2[i - k]) > 0) {
43+
counter[s2[i - k]] += 1;
44+
if (counter[s2[i - k]] >= 1) {
45+
hit -= 1;
46+
}
47+
}
48+
}
49+
4050
if (counter.count(s2[i]) > 0) {
4151
counter[s2[i]] -= 1;
4252
if (counter[s2[i]] >= 0) {
4353
hit += 1;
4454
}
4555
}
4656

47-
if (i >= s1.size() - 1) {
48-
if (hit == s1.size()) {
57+
if (i >= k - 1) {
58+
if (hit == k) {
4959
return true;
5060
}
51-
52-
if (counter.count(s2[j]) > 0) {
53-
counter[s2[j]] += 1;
54-
if (counter[s2[j]] > 0) {
55-
hit -= 1;
56-
}
57-
}
58-
59-
j += 1;
6061
}
6162
}
6263

leetcode/567.permutation-in-string.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@
2929
from collections import Counter
3030
class Solution:
3131
def checkInclusion(self, s1: str, s2: str) -> bool:
32+
n = len(s2)
33+
k = len(s1)
3234
counter = Counter(s1)
3335
hit = 0
34-
j = 0
35-
for i in range(len(s2)):
36+
37+
for i in range(n):
38+
if i >= k:
39+
if s2[i - k] in counter:
40+
counter[s2[i - k]] += 1
41+
if counter[s2[i - k]] >= 1:
42+
hit -= 1
43+
3644
if s2[i] in counter:
3745
counter[s2[i]] -= 1
3846
if counter[s2[i]] >= 0:
3947
hit += 1
40-
41-
if i >= len(s1) - 1:
42-
if hit == len(s1):
43-
return True
44-
45-
if s2[j] in counter:
46-
counter[s2[j]] += 1
47-
if counter[s2[j]] > 0:
48-
hit -= 1
49-
50-
j += 1
5148

49+
if i >= k - 1:
50+
if hit == k:
51+
return True
52+
5253
return False

list/灵茶山艾府.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
18. https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/
2626
19. https://leetcode.com/problems/sliding-subarray-beauty/
2727
20. https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/
28-
- https://leetcode.com/problems/permutation-in-string/
28+
21. https://leetcode.com/problems/permutation-in-string/
2929
- https://leetcode.com/problems/find-all-anagrams-in-a-string/
3030
- https://leetcode.com/problems/substring-with-concatenation-of-all-words/
3131
- https://leetcode.com/problems/find-substring-with-given-hash-value/

0 commit comments

Comments
 (0)