Skip to content

Commit dc29d15

Browse files
committed
1
1 parent 84e046e commit dc29d15

File tree

18 files changed

+661
-36
lines changed

18 files changed

+661
-36
lines changed

leetcode/1. Two Sum/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [1. Two Sum (Easy)](https://leetcode.com/problems/two-sum/submissions/)
2+
3+
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
4+
5+
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
6+
7+
<p><strong>Example:</strong></p>
8+
9+
<pre>Given nums = [2, 7, 11, 15], target = 9,
10+
11+
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
12+
return [<strong>0</strong>, <strong>1</strong>].
13+
</pre>
14+
15+
16+
**Related Topics**:
17+
[Array](https://leetcode.com/tag/array/), [Hash Table](https://leetcode.com/tag/hash-table/)
18+
19+
**Similar Questions**:
20+
* [3Sum (Medium)](https://leetcode.com/problems/3sum/)
21+
* [4Sum (Medium)](https://leetcode.com/problems/4sum/)
22+
* [Two Sum II - Input array is sorted (Easy)](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)
23+
* [Two Sum III - Data structure design (Easy)](https://leetcode.com/problems/two-sum-iii-data-structure-design/)
24+
* [Subarray Sum Equals K (Medium)](https://leetcode.com/problems/subarray-sum-equals-k/)
25+
* [Two Sum IV - Input is a BST (Easy)](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)
26+
* [Two Sum Less Than K (Easy)](https://leetcode.com/problems/two-sum-less-than-k/)
27+
28+
## Solution 1.
29+
30+
```cpp
31+
// OJ: https://leetcode.com/problems/two-sum/
32+
// Author: github.com/lzl124631x
33+
// Time: O(NlogN)
34+
// Space: O(N)
35+
class Solution {
36+
public:
37+
vector<int> twoSum(vector<int>& A, int target) {
38+
vector<vector<int>> v;
39+
int N = A.size(), L = 0, R = N - 1;
40+
for (int i = 0; i < N; ++i) v.push_back({ A[i], i });
41+
sort(begin(v), end(v), [](const vector<int> &a, const vector<int> &b) { return a[0] < b[0]; });
42+
while (L < R) {
43+
int sum = v[L][0] + v[R][0];
44+
if (sum == target) return { v[L][1], v[R][1] };
45+
if (sum < target) ++L;
46+
else --R;
47+
}
48+
return {};
49+
}
50+
};
51+
```
52+
53+
## Solution 2.
54+
55+
```cpp
56+
// OJ: https://leetcode.com/problems/two-sum/
57+
// Author: github.com/lzl124631x
58+
// Time: O(N)
59+
// Space: O(N)
60+
class Solution {
61+
public:
62+
vector<int> twoSum(vector<int>& A, int target) {
63+
unordered_map<int, int> m;
64+
for (int i = 0; i < A.size(); ++i) {
65+
int t = target - A[i];
66+
if (m.count(t)) return { m[t], i };
67+
m[A[i]] = i;
68+
}
69+
return {};
70+
}
71+
};
72+
```

leetcode/1. Two Sum/s1.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

leetcode/1. Two Sum/s2.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public:
3+
vector<string> braceExpansionII(string s) {
4+
stack<pair<bool, vector<string>>> ans; // bool -> isList
5+
vector<string> init{""};
6+
ans.push(make_pair(false, init));
7+
for (int i = 0; i < s.size(); ++i) {
8+
if (isalpha(s[i])) {
9+
if (ans.top().first) {
10+
vector<string> tmp{ string(1, s[i]) };
11+
ans.push(make_pair(false, tmp));
12+
} else {
13+
for (auto &a : ans.top().second) a += s[i];
14+
}
15+
} else if (s[i] == '{') {
16+
vector<string> tmp;
17+
ans.push(make_pair(true, tmp));
18+
} else { // , or }
19+
auto top = ans.top();
20+
ans.pop();
21+
if (ans.top().first) {
22+
for (auto &a : top.second) ans.top().second.push_back(a);
23+
} else {
24+
vector<string> tmp;
25+
for (auto &a : ans.top().second)
26+
for (auto &b : top.second)
27+
tmp.push_back(a + b);
28+
swap(tmp, ans.top().second);
29+
}
30+
if (s[i] == '}') {
31+
auto top = ans.top();
32+
ans.pop();
33+
if (ans.top().first) {
34+
for (auto &a : top.second) ans.top().second.push_back(a);
35+
} else {
36+
vector<string> tmp;
37+
for (auto &a : ans.top().second)
38+
for (auto &b : top.second)
39+
tmp.push_back(a + b);
40+
swap(tmp, ans.top().second);
41+
}
42+
}
43+
}
44+
cout << i << endl;
45+
}
46+
auto &v = ans.top().second;
47+
sort(v.begin(), v.end());
48+
v.erase(unique(v.begin(), v.end()), v.end());
49+
return v;
50+
}
51+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# [1223. Dice Roll Simulation (Medium)](https://leetcode.com/problems/dice-roll-simulation/)
2+
3+
<p>A die simulator generates a random number from 1 to 6 for each roll.&nbsp;You introduced a constraint to the generator such that it cannot roll the number <code>i</code> more than <code>rollMax[i]</code> (1-indexed) <strong>consecutive</strong> times.&nbsp;</p>
4+
5+
<p>Given an array of integers&nbsp;<code>rollMax</code>&nbsp;and an integer&nbsp;<code>n</code>, return the number of distinct sequences that can be obtained with exact <code>n</code> rolls.</p>
6+
7+
<p>Two sequences are considered different if at least one element differs from each other. Since the answer&nbsp;may be too large,&nbsp;return it modulo <code>10^9 + 7</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong>Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> n = 2, rollMax = [1,1,2,2,2,3]
13+
<strong>Output:</strong> 34
14+
<strong>Explanation:</strong> There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.
15+
</pre>
16+
17+
<p><strong>Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> n = 2, rollMax = [1,1,1,1,1,1]
20+
<strong>Output:</strong> 30
21+
</pre>
22+
23+
<p><strong>Example 3:</strong></p>
24+
25+
<pre><strong>Input:</strong> n = 3, rollMax = [1,1,1,2,2,3]
26+
<strong>Output:</strong> 181
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= n &lt;= 5000</code></li>
34+
<li><code>rollMax.length == 6</code></li>
35+
<li><code>1 &lt;= rollMax[i] &lt;= 15</code></li>
36+
</ul>
37+
38+
39+
**Related Topics**:
40+
[Dynamic Programming](https://leetcode.com/tag/dynamic-programming/)
41+
42+
## Solution 1. DP
43+
44+
Let `dp[i][j]` be the number of distinct sequences ending with `j` obtained using `i` rolles.
45+
46+
```
47+
dp[1][j] = 1 1 <= j <= 6
48+
```
49+
50+
Ignoring the `rollMax`, we have `dp[i][j] = sum( dp[i-1][k] | 1 <= k <= 6 )`.
51+
52+
If we pick `j` at `i` roll, we need to exclude the cases where there are already `rollMax[j]` `j` numbers right in front of `i`th roll.
53+
54+
The number of such cases is `dp[i-rollMax[j]-1][k]` where `1 <= k <= 6` and `k != j`.
55+
56+
```cpp
57+
// OJ: https://leetcode.com/problems/dice-roll-simulation/
58+
// Author: github.com/lzl124631x
59+
// Time: O(N)
60+
// Space: O(N)
61+
// Ref: https://leetcode.com/problems/dice-roll-simulation/discuss/403756/Java-Share-my-DP-solution
62+
class Solution {
63+
public:
64+
int dieSimulator(int n, vector<int>& A) {
65+
long mod = 1e9+7;
66+
vector<vector<long>> dp(n, vector<long>(7));
67+
for (int i = 0; i < 6; ++i) dp[0][i] = 1;
68+
dp[0][6] = 6;
69+
for (int i = 1; i < n; ++i) {
70+
long sum = 0;
71+
for (int j = 0; j < 6; ++j) {
72+
dp[i][j] = dp[i - 1][6];
73+
if (i < A[j]) sum = (sum + dp[i][j]) % mod;
74+
else {
75+
if (i - A[j] - 1 >= 0) dp[i][j] = (dp[i][j] - (dp[i - A[j] - 1][6] - dp[i - A[j] - 1][j]) + mod) % mod;
76+
else dp[i][j] = (dp[i][j] - 1) % mod;
77+
sum = (sum + dp[i][j]) % mod;
78+
}
79+
}
80+
dp[i][6] = sum;
81+
}
82+
return dp[n - 1][6];
83+
}
84+
};
85+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// OJ: https://leetcode.com/problems/dice-roll-simulation/
2+
// Author: github.com/lzl124631x
3+
// Time: O(N)
4+
// Space: O(N)
5+
// Ref: https://leetcode.com/problems/dice-roll-simulation/discuss/403756/Java-Share-my-DP-solution
6+
class Solution {
7+
public:
8+
int dieSimulator(int n, vector<int>& A) {
9+
long mod = 1e9+7;
10+
vector<vector<long>> dp(n, vector<long>(7));
11+
for (int i = 0; i < 6; ++i) dp[0][i] = 1;
12+
dp[0][6] = 6;
13+
for (int i = 1; i < n; ++i) {
14+
long sum = 0;
15+
for (int j = 0; j < 6; ++j) {
16+
dp[i][j] = dp[i - 1][6];
17+
if (i < A[j]) sum = (sum + dp[i][j]) % mod;
18+
else {
19+
if (i - A[j] - 1 >= 0) dp[i][j] = (dp[i][j] - (dp[i - A[j] - 1][6] - dp[i - A[j] - 1][j]) + mod) % mod;
20+
else dp[i][j] = (dp[i][j] - 1) % mod;
21+
sum = (sum + dp[i][j]) % mod;
22+
}
23+
}
24+
dp[i][6] = sum;
25+
}
26+
return dp[n - 1][6];
27+
}
28+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [1320. Minimum Distance to Type a Word Using Two Fingers (Hard)](https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/)
2+
3+
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/01/02/leetcode_keyboard.png" style="width: 417px; height: 250px;"></p>
4+
5+
<p>You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter <strong>A</strong> is located at coordinate <strong>(0,0)</strong>, the letter <strong>B</strong> is located at coordinate <strong>(0,1)</strong>, the letter <strong>P</strong> is located at coordinate <strong>(2,3)</strong> and the letter <strong>Z</strong> is located at coordinate <strong>(4,1)</strong>.</p>
6+
7+
<p>Given the string <code>word</code>, return the minimum total distance to type such string using only two&nbsp;fingers. The distance between coordinates <strong>(x<sub>1</sub>,y<sub>1</sub>)</strong> and <strong>(x<sub>2</sub>,y<sub>2</sub>)</strong> is <strong>|x<sub>1</sub> - x<sub>2</sub>| + |y<sub>1</sub> - y<sub>2</sub>|</strong>.&nbsp;</p>
8+
9+
<p>Note that the initial positions of your two&nbsp;fingers are considered free so don't count towards your total distance, also your two&nbsp;fingers do not have to start at the first letter or the first two&nbsp;letters.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> word = "CAKE"
15+
<strong>Output:</strong> 3
16+
<strong>Explanation:
17+
</strong>Using two fingers, one optimal way to type "CAKE" is:
18+
Finger 1 on letter 'C' -&gt; cost = 0
19+
Finger 1 on letter 'A' -&gt; cost = Distance from letter 'C' to letter 'A' = 2
20+
Finger 2 on letter 'K' -&gt; cost = 0
21+
Finger 2 on letter 'E' -&gt; cost = Distance from letter 'K' to letter 'E' = 1
22+
Total distance = 3
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<pre><strong>Input:</strong> word = "HAPPY"
28+
<strong>Output:</strong> 6
29+
<strong>Explanation: </strong>
30+
Using two fingers, one optimal way to type "HAPPY" is:
31+
Finger 1 on letter 'H' -&gt; cost = 0
32+
Finger 1 on letter 'A' -&gt; cost = Distance from letter 'H' to letter 'A' = 2
33+
Finger 2 on letter 'P' -&gt; cost = 0
34+
Finger 2 on letter 'P' -&gt; cost = Distance from letter 'P' to letter 'P' = 0
35+
Finger 1 on letter 'Y' -&gt; cost = Distance from letter 'A' to letter 'Y' = 4
36+
Total distance = 6
37+
</pre>
38+
39+
<p><strong>Example 3:</strong></p>
40+
41+
<pre><strong>Input:</strong> word = "NEW"
42+
<strong>Output:</strong> 3
43+
</pre>
44+
45+
<p><strong>Example 4:</strong></p>
46+
47+
<pre><strong>Input:</strong> word = "YEAR"
48+
<strong>Output:</strong> 7
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>2 &lt;= word.length &lt;= 300</code></li>
56+
<li>Each <code data-stringify-type="code">word[i]</code>&nbsp;is an English uppercase letter.</li>
57+
</ul>
58+
59+
**Related Topics**:
60+
[Dynamic Programming](https://leetcode.com/tag/dynamic-programming/)
61+
62+
## Solution 1. DP
63+
64+
```cpp
65+
// OJ: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
66+
// Author: github.com/lzl124631x
67+
// Time: O(N)
68+
// Space: O(1)
69+
// Ref: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477652/JavaC%2B%2BPython-1D-DP-O(1)-Space
70+
class Solution {
71+
int d(int a, int b) {
72+
return abs(a / 6 - b / 6) + abs(a % 6 - b % 6);
73+
}
74+
public:
75+
int minimumDistance(string word) {
76+
vector<int> dp(26);
77+
int res = 0, save = 0, n = word.size();
78+
for (int i = 0; i < n - 1; ++i) {
79+
int b = word[i] - 'A', c = word[i + 1] - 'A';
80+
for (int a = 0; a < 26; ++a)
81+
dp[b] = max(dp[b], dp[a] + d(b, c) - d(a, c));
82+
save = max(save, dp[b]);
83+
res += d(b, c);
84+
}
85+
return res - save;
86+
}
87+
};
88+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// OJ: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
2+
// Author: github.com/lzl124631x
3+
// Time: O(N)
4+
// Space: O(1)
5+
// Ref: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477652/JavaC%2B%2BPython-1D-DP-O(1)-Space
6+
class Solution {
7+
int d(int a, int b) {
8+
return abs(a / 6 - b / 6) + abs(a % 6 - b % 6);
9+
}
10+
public:
11+
int minimumDistance(string word) {
12+
vector<int> dp(26);
13+
int res = 0, save = 0, n = word.size();
14+
for (int i = 0; i < n - 1; ++i) {
15+
int b = word[i] - 'A', c = word[i + 1] - 'A';
16+
for (int a = 0; a < 26; ++a)
17+
dp[b] = max(dp[b], dp[a] + d(b, c) - d(a, c));
18+
save = max(save, dp[b]);
19+
res += d(b, c);
20+
}
21+
return res - save;
22+
}
23+
};

0 commit comments

Comments
 (0)