Skip to content

Commit f7600b3

Browse files
add leetcode solutions ⚡
1 parent 04dff90 commit f7600b3

File tree

44 files changed

+1109
-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.

44 files changed

+1109
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
vector<int> original;
3+
int n;
4+
public:
5+
Solution(vector<int>& nums) {
6+
original = nums;
7+
n = original.size();
8+
}
9+
10+
vector<int> reset() {
11+
return original;
12+
}
13+
14+
vector<int> shuffle() {
15+
vector<int> shuffled = original;
16+
for (int i = n - 1, leftSize = n; i >= 0; i--, leftSize--) {
17+
int j = rand() % leftSize;
18+
swap(shuffled[i], shuffled[j]);
19+
}
20+
return shuffled;
21+
}
22+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<h2><a href="https://leetcode.com/problems/shuffle-an-array/">384. Shuffle an Array</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p>
2+
3+
<p>Implement the <code>Solution</code> class:</p>
4+
5+
<ul>
6+
<li><code>Solution(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li>
7+
<li><code>int[] reset()</code> Resets the array to its original configuration and returns it.</li>
8+
<li><code>int[] shuffle()</code> Returns a random shuffling of the array.</li>
9+
</ul>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre><strong>Input</strong>
15+
["Solution", "shuffle", "reset", "shuffle"]
16+
[[[1, 2, 3]], [], [], []]
17+
<strong>Output</strong>
18+
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
19+
20+
<strong>Explanation</strong>
21+
Solution solution = new Solution([1, 2, 3]);
22+
solution.shuffle(); // Shuffle the array [1,2,3] and return its result.
23+
// Any permutation of [1,2,3] must be equally likely to be returned.
24+
// Example: return [3, 1, 2]
25+
solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3]
26+
solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
27+
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
35+
<li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
36+
<li>All the elements of <code>nums</code> are <strong>unique</strong>.</li>
37+
<li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li>
38+
</ul>
39+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
bool canCross(vector<int>& s) {
4+
if (s[0] + 1 != s[1]) return false;
5+
6+
int n = s.size() - 1;
7+
unordered_map<string, bool> bag;
8+
auto gen_key = [](auto l, auto ...r) {
9+
return (to_string(l) + ... + ("-" + to_string(r)));
10+
};
11+
12+
function<bool(int, int, int)> go = [&](auto i, auto val, auto offset) {
13+
if (val < s[i]) return false;
14+
if ( i == n ) return val == s[i];
15+
16+
auto key = gen_key(i, val, offset);
17+
if (bag.count(key)) return bag[key];
18+
19+
if (val > s[i]) return bag[key] = go(i + 1, val, offset);
20+
auto paths = go(i + 1, val + offset, offset)
21+
|| go(i + 1, val + offset + 1, offset + 1)
22+
|| go(i + 1, val + offset - 1, offset - 1);
23+
return bag[key] = paths;
24+
};
25+
26+
return go(1, 1, 1);
27+
}
28+
};

leetcode/0403-frog-jump/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

leetcode/0403-frog-jump/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h2><a href="https://leetcode.com/problems/frog-jump/">403. Frog Jump</a></h2><h3>Hard</h3><hr><div><p>A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.</p>
2+
3+
<p>Given a list of <code>stones</code>' positions (in units) in sorted <strong>ascending order</strong>, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be <code>1</code> unit.</p>
4+
5+
<p>If the frog's last jump was <code>k</code> units, its next jump must be either <code>k - 1</code>, <code>k</code>, or <code>k + 1</code> units. The frog can only jump in the forward direction.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> stones = [0,1,3,5,6,8,12,17]
11+
<strong>Output:</strong> true
12+
<strong>Explanation:</strong> The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<pre><strong>Input:</strong> stones = [0,1,2,3,4,8,9,11]
18+
<strong>Output:</strong> false
19+
<strong>Explanation:</strong> There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>2 &lt;= stones.length &lt;= 2000</code></li>
27+
<li><code>0 &lt;= stones[i] &lt;= 2<sup>31</sup> - 1</code></li>
28+
<li><code>stones[0] == 0</code></li>
29+
<li><code>stones</code>&nbsp;is sorted in a strictly increasing order.</li>
30+
</ul>
31+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
string originalDigits(string s) {
4+
vector<string> words = {"zero", "two", "four", "six", "eight",
5+
"one", "three", "five", "seven", "nine"};
6+
vector<int> nums = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
7+
vector<int> distinct_char = {'z', 'w', 'u', 'x', 'g', 'o', 'r', 'f', 'v', 'i'};
8+
vector<int> counts(26, 0);
9+
string result;
10+
for (auto ch : s) counts[ch-'a']++;
11+
for (int i = 0; i < 10; i++) {
12+
int count = counts[distinct_char[i]-'a'];
13+
for (int j = 0; j < words[i].size(); j++)
14+
counts[words[i][j]-'a'] -= count;
15+
while (count--)
16+
result += to_string(nums[i]);
17+
}
18+
sort(result.begin(), result.end());
19+
return result;
20+
}
21+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<h2><a href="https://leetcode.com/problems/reconstruct-original-digits-from-english/">423. Reconstruct Original Digits from English</a></h2><h3>Medium</h3><hr><div><p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
<pre><strong>Input:</strong> s = "owoztneoer"
6+
<strong>Output:</strong> "012"
7+
</pre><p><strong class="example">Example 2:</strong></p>
8+
<pre><strong>Input:</strong> s = "fviefuro"
9+
<strong>Output:</strong> "45"
10+
</pre>
11+
<p>&nbsp;</p>
12+
<p><strong>Constraints:</strong></p>
13+
14+
<ul>
15+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
16+
<li><code>s[i]</code> is one of the characters <code>["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]</code>.</li>
17+
<li><code>s</code> is <strong>guaranteed</strong> to be valid.</li>
18+
</ul>
19+
</div>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Gets TLE
2+
/*
3+
class Solution {
4+
public:
5+
int fourSumCount(vector<int>& one,
6+
vector<int>& two,
7+
vector<int>& three,
8+
vector<int>& four, int res = 0) {
9+
for (auto a: one)
10+
for (auto b: two)
11+
for (auto c: three)
12+
for (auto d: four)
13+
res += (a + b + c + d == 0);
14+
return res;
15+
}
16+
};
17+
*/
18+
19+
class Solution {
20+
public:
21+
int fourSumCount(vector<int>& one,
22+
vector<int>& two,
23+
vector<int>& three,
24+
vector<int>& four, int res = 0) {
25+
unordered_map<int,int> mp;
26+
for (auto a: one)
27+
for (auto b: two)
28+
mp[a + b]++;
29+
for (auto c: three)
30+
for (auto d: four)
31+
if (mp.count(0 - c - d)) res += mp[0 - c - d];
32+
return res;
33+
}
34+
};

0 commit comments

Comments
 (0)