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
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+
};
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+39
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>
+28
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

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

leetcode/0403-frog-jump/README.md

+31
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+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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>
+34
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+
};

leetcode/0454-4sum-ii/NOTES.md

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

leetcode/0454-4sum-ii/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<h2><a href="https://leetcode.com/problems/4sum-ii/">454. 4Sum II</a></h2><h3>Medium</h3><hr><div><p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
2+
3+
<ul>
4+
<li><code>0 &lt;= i, j, k, l &lt; n</code></li>
5+
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
6+
</ul>
7+
8+
<p>&nbsp;</p>
9+
<p><strong class="example">Example 1:</strong></p>
10+
11+
<pre><strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong>
14+
The two tuples are:
15+
1. (0, 0, 0, 1) -&gt; nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
16+
2. (1, 1, 0, 0) -&gt; nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
22+
<strong>Output:</strong> 1
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>n == nums1.length</code></li>
30+
<li><code>n == nums2.length</code></li>
31+
<li><code>n == nums3.length</code></li>
32+
<li><code>n == nums4.length</code></li>
33+
<li><code>1 &lt;= n &lt;= 200</code></li>
34+
<li><code>-2<sup>28</sup> &lt;= nums1[i], nums2[i], nums3[i], nums4[i] &lt;= 2<sup>28</sup></code></li>
35+
</ul>
36+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool PredictTheWinner(vector<int>& nums) {
4+
5+
auto gen_key = [](auto l, auto ...r) {
6+
return (to_string(l) + ... + ("-" + to_string(r)));
7+
};
8+
unordered_map<string, int> c;
9+
10+
auto sum = accumulate(nums.begin(), nums.end(), 0);
11+
function<int(int, int, bool)> go = [&](auto l, auto r, auto is_a) {
12+
if (l > r) return 0;
13+
auto key = gen_key(l, r, is_a);
14+
if (c.count(key)) return c[key];
15+
auto l_taken = (is_a ? nums[l]: 0) + go(l + 1, r, !is_a),
16+
r_taken = (is_a ? nums[r]: 0) + go(l, r - 1, !is_a);
17+
return c[key] = is_a ? max(l_taken, r_taken): min(l_taken, r_taken);
18+
};
19+
auto a_score = go(0, nums.size() - 1, true);
20+
return 2 * a_score >= sum;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<h2><a href="https://leetcode.com/problems/predict-the-winner/">486. Predict the Winner</a></h2><h3>Medium</h3><hr><div><p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
2+
3+
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
4+
5+
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> nums = [1,5,2]
11+
<strong>Output:</strong> false
12+
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
13+
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
14+
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
15+
Hence, player 1 will never be the winner and you need to return false.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
<pre><strong>Input:</strong> nums = [1,5,233,7]
21+
<strong>Output:</strong> true
22+
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
23+
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
24+
</pre>
25+
26+
<p>&nbsp;</p>
27+
<p><strong>Constraints:</strong></p>
28+
29+
<ul>
30+
<li><code>1 &lt;= nums.length &lt;= 20</code></li>
31+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
32+
</ul>
33+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
3+
select Employee.name, Bonus.bonus from Employee
4+
left join Bonus on Employee.empId = Bonus.empId
5+
where Bonus.bonus < 1000 or Bonus.bonus is NULL;

leetcode/0577-employee-bonus/NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<h2><a href="https://leetcode.com/problems/employee-bonus/">577. Employee Bonus</a></h2><h3>Easy</h3><hr><div class="sql-schema-wrapper__3VBi"><a class="sql-schema-link__3cEg">SQL Schema<svg viewBox="0 0 24 24" width="1em" height="1em" class="icon__1Md2"><path fill-rule="evenodd" d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"></path></svg></a></div><div><p>Table: <code>Employee</code></p>
2+
3+
<pre>+-------------+---------+
4+
| Column Name | Type |
5+
+-------------+---------+
6+
| empId | int |
7+
| name | varchar |
8+
| supervisor | int |
9+
| salary | int |
10+
+-------------+---------+
11+
empId is the primary key column for this table.
12+
Each row of this table indicates the name and the ID of an employee in addition to their salary and the id of their manager.
13+
</pre>
14+
15+
<p>&nbsp;</p>
16+
17+
<p>Table: <code>Bonus</code></p>
18+
19+
<pre>+-------------+------+
20+
| Column Name | Type |
21+
+-------------+------+
22+
| empId | int |
23+
| bonus | int |
24+
+-------------+------+
25+
empId is the primary key column for this table.
26+
empId is a foreign key to empId from the Employee table.
27+
Each row of this table contains the id of an employee and their respective bonus.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p>Write an SQL query to report the name and bonus amount of each employee with a bonus <strong>less than</strong> <code>1000</code>.</p>
33+
34+
<p>Return the result table in <strong>any order</strong>.</p>
35+
36+
<p>The query result format is in the following example.</p>
37+
38+
<p>&nbsp;</p>
39+
<p><strong class="example">Example 1:</strong></p>
40+
41+
<pre><strong>Input:</strong>
42+
Employee table:
43+
+-------+--------+------------+--------+
44+
| empId | name | supervisor | salary |
45+
+-------+--------+------------+--------+
46+
| 3 | Brad | null | 4000 |
47+
| 1 | John | 3 | 1000 |
48+
| 2 | Dan | 3 | 2000 |
49+
| 4 | Thomas | 3 | 4000 |
50+
+-------+--------+------------+--------+
51+
Bonus table:
52+
+-------+-------+
53+
| empId | bonus |
54+
+-------+-------+
55+
| 2 | 500 |
56+
| 4 | 2000 |
57+
+-------+-------+
58+
<strong>Output:</strong>
59+
+------+-------+
60+
| name | bonus |
61+
+------+-------+
62+
| Brad | null |
63+
| John | null |
64+
| Dan | 500 |
65+
+------+-------+
66+
</pre>
67+
</div>

0 commit comments

Comments
 (0)