Skip to content

Commit 92a68f4

Browse files
author
openset
committed
Add: new
1 parent 170122a commit 92a68f4

File tree

7 files changed

+336
-1
lines changed

7 files changed

+336
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1210">1210</span> | [Minimum Moves to Reach Target with Rotations](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations "穿过迷宫的最少移动次数") | [Go](https://github.com/openset/leetcode/tree/master/problems/minimum-moves-to-reach-target-with-rotations) | Hard |
66+
| <span id="1209">1209</span> | [Remove All Adjacent Duplicates in String II](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii "删除字符串中的所有相邻重复项 II") | [Go](https://github.com/openset/leetcode/tree/master/problems/remove-all-adjacent-duplicates-in-string-ii) | Medium |
67+
| <span id="1208">1208</span> | [Get Equal Substrings Within Budget](https://leetcode.com/problems/get-equal-substrings-within-budget "尽可能使字符串相等") | [Go](https://github.com/openset/leetcode/tree/master/problems/get-equal-substrings-within-budget) | Medium |
68+
| <span id="1207">1207</span> | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences "独一无二的出现次数") | [Go](https://github.com/openset/leetcode/tree/master/problems/unique-number-of-occurrences) | Easy |
69+
| <span id="1206">1206</span> | [Design Skiplist](https://leetcode.com/problems/design-skiplist) | [Go](https://github.com/openset/leetcode/tree/master/problems/design-skiplist) | Hard |
6570
| <span id="1205">1205</span> | [Monthly Transactions II](https://leetcode.com/problems/monthly-transactions-ii) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/monthly-transactions-ii) | Medium |
6671
| <span id="1204">1204</span> | [Last Person to Fit in the Elevator](https://leetcode.com/problems/last-person-to-fit-in-the-elevator) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/last-person-to-fit-in-the-elevator) | Medium |
6772
| <span id="1203">1203</span> | [Sort Items by Groups Respecting Dependencies](https://leetcode.com/problems/sort-items-by-groups-respecting-dependencies "项目管理") | [Go](https://github.com/openset/leetcode/tree/master/problems/sort-items-by-groups-respecting-dependencies) | Hard |

problems/design-skiplist/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/monthly-transactions-ii "Monthly Transactions II")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/unique-number-of-occurrences "Unique Number of Occurrences")
11+
12+
## [1206. Design Skiplist (Hard)](https://leetcode.com/problems/design-skiplist "")
13+
14+
<p>Design a Skiplist without using any built-in libraries.</p>
15+
16+
<p><em>A Skiplist is a data structure that takes&nbsp;O(log(n)) time&nbsp;to <code>add</code>, <code>erase</code> and <code>search</code>. Comparing with treap and red-black tree which has the same function and performance, the code length of Skiplist can be&nbsp;comparatively short and the idea behind Skiplists are just simple linked lists.</em></p>
17+
18+
<p><em>For example:&nbsp;we have a Skiplist containing <code>[30,40,50,60,70,90]</code> and we want to add <code>80</code> and <code>45</code> into it. The&nbsp;Skiplist works this way:</em></p>
19+
20+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/27/1506_skiplist.gif" style="width: 960px; height: 332px;" /><br />
21+
<small>Artyom Kalinin [CC BY-SA 3.0], via <a href="https://commons.wikimedia.org/wiki/File:Skip_list_add_element-en.gif" target="_blank" title="Artyom Kalinin [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons">Wikimedia Commons</a></small></p>
22+
23+
<p><em>You can see there are many layers in the Skiplist. Each layer is a sorted linked list. With the help of the top layers, <code>add</code>&nbsp;,&nbsp;<code>erase</code>&nbsp;and <code>search&nbsp;</code>can be faster than O(n).&nbsp;It can be proven&nbsp;that the average time complexity for each operation is O(log(n)) and space complexity is O(n).</em></p>
24+
25+
<p>To be specific, your design should include these functions:</p>
26+
27+
<ul>
28+
<li><code>bool search(int target)</code> : Return whether&nbsp;the <code>target</code> exists in the Skiplist&nbsp;or not.</li>
29+
<li><code>void add(int num)</code>:&nbsp;Insert a value into the SkipList.&nbsp;</li>
30+
<li><code>bool erase(int num)</code>: Remove a value in&nbsp;the Skiplist.&nbsp;If <code>num</code>&nbsp;does not exist in the Skiplist, do nothing and return false. If there exists multiple <code>num</code> values, removing&nbsp;any one of them is fine.</li>
31+
</ul>
32+
33+
<p>See more about Skiplist :&nbsp;<a href="https://en.wikipedia.org/wiki/Skip_list" target="_blank">https://en.wikipedia.org/wiki/Skip_list</a></p>
34+
35+
<p>Note that duplicates may exist in the Skiplist, your code needs to handle this situation.</p>
36+
37+
<p>&nbsp;</p>
38+
39+
<p><b>Example:</b></p>
40+
41+
<pre>
42+
Skiplist skiplist = new Skiplist();
43+
44+
skiplist.add(1);
45+
skiplist.add(2);
46+
skiplist.add(3);
47+
skiplist.search(0); // return false.
48+
skiplist.add(4);
49+
skiplist.search(1); // return true.
50+
skiplist.erase(0); // return false, 0 is not in skiplist.
51+
skiplist.erase(1); // return true.
52+
skiplist.search(1); // return false, 1 has already been erased.</pre>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>0 &lt;= num, target&nbsp;&lt;= 20000</code></li>
59+
<li>At most <code>50000</code>&nbsp;calls will be made to <code>search</code>, <code>add</code>, and <code>erase</code>.</li>
60+
</ul>
61+
62+
### Related Topics
63+
[[Design](https://github.com/openset/leetcode/tree/master/tag/design/README.md)]
64+
65+
### Similar Questions
66+
1. [Design HashSet](https://github.com/openset/leetcode/tree/master/problems/design-hashset) (Easy)
67+
1. [Design HashMap](https://github.com/openset/leetcode/tree/master/problems/design-hashmap) (Easy)
68+
1. [Design Linked List](https://github.com/openset/leetcode/tree/master/problems/design-linked-list) (Easy)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/unique-number-of-occurrences "Unique Number of Occurrences")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/remove-all-adjacent-duplicates-in-string-ii "Remove All Adjacent Duplicates in String II")
11+
12+
## [5207. Get Equal Substrings Within Budget (Medium)](https://leetcode.com/problems/get-equal-substrings-within-budget "尽可能使字符串相等")
13+
14+
<p>You are given two strings <code>s</code> and <code>t</code> of the same length. You want to change <code>s</code> to <code>t</code>. Changing the <code>i</code>-th character of <code>s</code> to <code>i</code>-th character of <code>t</code> costs <code>|s[i] - t[i]|</code> that is, the absolute difference between the ASCII values of the characters.</p>
15+
16+
<p>You are also given an integer <code>maxCost</code>.</p>
17+
18+
<p>Return the maximum length of a substring of <code>s</code> that can be changed to be the same as the corresponding substring of <code>t</code>with a cost less than or equal to <code>maxCost</code>.</p>
19+
20+
<p>If there is no substring from&nbsp;<code>s</code> that can be changed to its corresponding substring from <code>t</code>, return <code>0</code>.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> s = &quot;abcd&quot;, t = &quot;bcdf&quot;, cost = 3
27+
<strong>Output:</strong> 3
28+
<strong>Explanation: </strong>&quot;abc&quot; of s can change to &quot;bcd&quot;. That costs 3, so the maximum length is 3.</pre>
29+
30+
<p><strong>Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> s = &quot;abcd&quot;, t = &quot;cdef&quot;, cost = 3
34+
<strong>Output:</strong> 1
35+
<strong>Explanation: </strong>Each charactor in s costs 2 to change to charactor in <code>t, so the maximum length is 1.</code>
36+
</pre>
37+
38+
<p><strong>Example 3:</strong></p>
39+
40+
<pre>
41+
<strong>Input:</strong> s = &quot;abcd&quot;, t = &quot;acde&quot;, cost = 0
42+
<strong>Output:</strong> 1
43+
<strong>Explanation: </strong>You can&#39;t make any change, so the maximum length is 1.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= s.length, t.length &lt;= 10^5</code></li>
51+
<li><code>0 &lt;= maxCost &lt;= 10^6</code></li>
52+
<li><code>s</code> and&nbsp;<code>t</code> only contain lower case English letters.</li>
53+
</ul>
54+
55+
### Hints
56+
<details>
57+
<summary>Hint 1</summary>
58+
Calculate the differences between a[i] and b[i].
59+
</details>
60+
61+
<details>
62+
<summary>Hint 2</summary>
63+
Use a sliding window to track the longest valid substring.
64+
</details>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/remove-all-adjacent-duplicates-in-string-ii "Remove All Adjacent Duplicates in String II")
9+
                
10+
Next >
11+
12+
## [5208. Minimum Moves to Reach Target with Rotations (Hard)](https://leetcode.com/problems/minimum-moves-to-reach-target-with-rotations "穿过迷宫的最少移动次数")
13+
14+
<p>In an&nbsp;<code>n*n</code>&nbsp;grid, there is a snake that spans 2 cells and starts moving from the top left corner at <code>(0, 0)</code> and <code>(0, 1)</code>. The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at&nbsp;<code>(n-1, n-2)</code>&nbsp;and&nbsp;<code>(n-1, n-1)</code>.</p>
15+
16+
<p>In one move the snake can:</p>
17+
18+
<ul>
19+
<li>Move one cell to the right&nbsp;if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li>
20+
<li>Move down one cell&nbsp;if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li>
21+
<li>Rotate clockwise if it&#39;s in a horizontal position and the two cells under it are both empty. In that case the snake moves from&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r, c+1)</code>&nbsp;to&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r+1, c)</code>.<br />
22+
<img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-2.png" style="width: 300px; height: 134px;" /></li>
23+
<li>Rotate counterclockwise&nbsp;if it&#39;s in a vertical position and the two cells to its right are both empty. In that case the snake moves from&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r+1, c)</code>&nbsp;to&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r, c+1)</code>.<br />
24+
<img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-1.png" style="width: 300px; height: 121px;" /></li>
25+
</ul>
26+
27+
<p>Return the minimum number of moves to reach the target.</p>
28+
29+
<p>If there is no way to reach the target, return&nbsp;<code>-1</code>.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Example 1:</strong></p>
33+
34+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image.png" style="width: 400px; height: 439px;" /></strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> grid = [[0,0,0,0,0,1],
38+
[1,1,0,0,1,0],
39+
&nbsp; [0,0,0,0,1,1],
40+
&nbsp; [0,0,1,0,1,0],
41+
&nbsp; [0,1,1,0,0,0],
42+
&nbsp; [0,1,1,0,0,0]]
43+
<strong>Output:</strong> 11
44+
<strong>Explanation:
45+
</strong>One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].
46+
</pre>
47+
48+
<p><strong>Example 2:</strong></p>
49+
50+
<pre>
51+
<strong>Input:</strong> grid = [[0,0,1,1,1,1],
52+
&nbsp; [0,0,0,0,1,1],
53+
&nbsp; [1,1,0,0,0,1],
54+
&nbsp; [1,1,1,0,0,1],
55+
&nbsp; [1,1,1,0,0,1],
56+
&nbsp; [1,1,1,0,0,0]]
57+
<strong>Output:</strong> 9
58+
</pre>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>2 &lt;= n &lt;= 100</code></li>
65+
<li><code>0 &lt;= grid[i][j] &lt;= 1</code></li>
66+
<li>It is guaranteed that the snake starts at empty cells.</li>
67+
</ul>
68+
69+
### Hints
70+
<details>
71+
<summary>Hint 1</summary>
72+
Use BFS to find the answer.
73+
</details>
74+
75+
<details>
76+
<summary>Hint 2</summary>
77+
The state of the BFS is the position (x, y) along with a binary value that specifies if the position is horizontal or vertical.
78+
</details>

problems/monthly-transactions-ii/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[< Previous](https://github.com/openset/leetcode/tree/master/problems/last-person-to-fit-in-the-elevator "Last Person to Fit in the Elevator")
99

10-
Next >
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/design-skiplist "Design Skiplist")
1111

1212
## [1205. Monthly Transactions II (Medium)](https://leetcode.com/problems/monthly-transactions-ii "")
1313

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/get-equal-substrings-within-budget "Get Equal Substrings Within Budget")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/minimum-moves-to-reach-target-with-rotations "Minimum Moves to Reach Target with Rotations")
11+
12+
## [5206. Remove All Adjacent Duplicates in String II (Medium)](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii "删除字符串中的所有相邻重复项 II")
13+
14+
<p>Given a string&nbsp;<code>s</code>, a <em>k</em>&nbsp;<em>duplicate removal</em>&nbsp;consists of choosing <code>k</code>&nbsp;adjacent and equal letters from&nbsp;<code>s</code> and removing&nbsp;them causing the left and the right side of the deleted substring to concatenate together.</p>
15+
16+
<p>We repeatedly make <code>k</code> duplicate removals on <code>s</code> until we no longer can.</p>
17+
18+
<p>Return the final string after all such duplicate removals have been made.</p>
19+
20+
<p>It is guaranteed that the answer is unique.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> s = &quot;abcd&quot;, k = 2
27+
<strong>Output:</strong> &quot;abcd&quot;
28+
<strong>Explanation: </strong>There&#39;s nothing to delete.</pre>
29+
30+
<p><strong>Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> s = &quot;deeedbbcccbdaa&quot;, k = 3
34+
<strong>Output:</strong> &quot;aa&quot;
35+
<strong>Explanation:
36+
</strong>First delete &quot;eee&quot; and &quot;ccc&quot;, get &quot;ddbbbdaa&quot;
37+
Then delete &quot;bbb&quot;, get &quot;dddaa&quot;
38+
Finally delete &quot;ddd&quot;, get &quot;aa&quot;</pre>
39+
40+
<p><strong>Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> s = &quot;pbbcggttciiippooaais&quot;, k = 2
44+
<strong>Output:</strong> &quot;ps&quot;
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
52+
<li><code>2 &lt;= k &lt;= 10^4</code></li>
53+
<li><code>s</code> only contains lower case English letters.</li>
54+
</ul>
55+
56+
### Hints
57+
<details>
58+
<summary>Hint 1</summary>
59+
Use a stack to store the characters, when there are k same characters, delete them.
60+
</details>
61+
62+
<details>
63+
<summary>Hint 2</summary>
64+
To make it more efficient, use a pair to store the value and the count of each character.
65+
</details>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](https://github.com/openset/leetcode/tree/master/problems/design-skiplist "Design Skiplist")
9+
                
10+
[Next >](https://github.com/openset/leetcode/tree/master/problems/get-equal-substrings-within-budget "Get Equal Substrings Within Budget")
11+
12+
## [5205. Unique Number of Occurrences (Easy)](https://leetcode.com/problems/unique-number-of-occurrences "独一无二的出现次数")
13+
14+
<p>Given an array of integers <code>arr</code>,&nbsp;write a function that returns <code>true</code> if and only if the number of occurrences of each value in the array is unique.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong>Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> arr = [1,2,2,1,1,3]
21+
<strong>Output:</strong> true
22+
<strong>Explanation:</strong>&nbsp;The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.</pre>
23+
24+
<p><strong>Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> arr = [1,2]
28+
<strong>Output:</strong> false
29+
</pre>
30+
31+
<p><strong>Example 3:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> arr = [-3,0,1,-3,1,1,1,-3,10,0]
35+
<strong>Output:</strong> true
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= arr.length&nbsp;&lt;= 1000</code></li>
43+
<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>
44+
</ul>
45+
46+
### Hints
47+
<details>
48+
<summary>Hint 1</summary>
49+
Find the number of occurrences of each element in the array using a hash map.
50+
</details>
51+
52+
<details>
53+
<summary>Hint 2</summary>
54+
Iterate through the hash map and check if there is a repeated value.
55+
</details>

0 commit comments

Comments
 (0)