Skip to content

Commit 402100d

Browse files
emfomy@gmail.comemfomy@gmail.com
emfomy@gmail.com
authored and
emfomy@gmail.com
committed
update
1 parent 9b64c0c commit 402100d

20 files changed

+1077
-98
lines changed

Diff for: README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ LeetCode Problems' Solutions
9595
| 202 | [Happy Number](//leetcode.com/problems/happy-number) | [Go](problems/0202.go) | Easy |
9696
| 206 | [Reverse Linked List](//leetcode.com/problems/reverse-linked-list) | [Go](problems/0206.go) | Easy |
9797
| 207 | [Course Schedule](//leetcode.com/problems/course-schedule) | [Go](problems/0207.go) | Medium |
98+
| 208 | [Implement Trie (Prefix Tree)](//leetcode.com/problems/implement-trie-prefix-tree) | [Go](problems/0208.go) | Medium |
9899
| 210 | [Course Schedule II](//leetcode.com/problems/course-schedule-ii) | [Go](problems/0210.go) | Medium |
99100
| 213 | [House Robber II](//leetcode.com/problems/house-robber-ii) | [Go](problems/0213.go) | Medium |
100101
| 215 | [Kth Largest Element in an Array](//leetcode.com/problems/kth-largest-element-in-an-array) | [C++](problems/0215.cpp) [Go](problems/0215.go) | Medium |
@@ -201,6 +202,7 @@ LeetCode Problems' Solutions
201202
| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](//leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram) | [Go](problems/1347.go) | Medium |
202203
| 1354 | [Construct Target Array With Multiple Sums](//leetcode.com/problems/construct-target-array-with-multiple-sums) | [Go](problems/1354.go) | Hard |
203204
| 1356 | [Sort Integers by The Number of 1 Bits](//leetcode.com/problems/sort-integers-by-the-number-of-1-bits) | [Go](problems/1356.go) | Easy |
205+
| 1405 | [Longest Happy String](//leetcode.com/problems/longest-happy-string) | [Go](problems/1405.go) | Medium |
204206
| 1423 | [Maximum Points You Can Obtain from Cards](//leetcode.com/problems/maximum-points-you-can-obtain-from-cards) | [Go](problems/1423.go) | Medium |
205207
| 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](//leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | [Go](problems/1438.go) | Medium |
206208
| 1460 | [Make Two Arrays Equal by Reversing Subarrays](//leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays) | [Go](problems/1460.go) | Easy |
@@ -216,6 +218,7 @@ LeetCode Problems' Solutions
216218
| 1567 | [Maximum Length of Subarray With Positive Product](//leetcode.com/problems/maximum-length-of-subarray-with-positive-product) | [Go](problems/1567.go) | Medium |
217219
| 1572 | [Matrix Diagonal Sum](//leetcode.com/problems/matrix-diagonal-sum) | [Go](problems/1572.go) | Easy |
218220
| 1588 | [Sum of All Odd Length Subarrays](//leetcode.com/problems/sum-of-all-odd-length-subarrays) | [Go](problems/1588.go) | Easy |
221+
| 1593 | [// Split a String Into the Max Number of Unique Substrings](//leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings) | [Go](problems/1593.go) | Medium |
219222
| 1603 | [Design Parking System](//leetcode.com/problems/design-parking-system) | [Go](problems/1603.go) | Easy |
220223
| 1642 | [Furthest Building You Can Reach](//leetcode.com/problems/furthest-building-you-can-reach) | [Go](problems/1642.go) | Medium |
221224
| 1647 | [Minimum Deletions to Make Character Frequencies Unique](//leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique) | [Go](problems/1647.go) | Medium |
@@ -229,6 +232,7 @@ LeetCode Problems' Solutions
229232
| 1696 | [Jump Game VI](//leetcode.com/problems/jump-game-vi) | [Go](problems/1696.go) | Medium |
230233
| 1704 | [Determine if String Halves Are Alike](//leetcode.com/problems/determine-if-string-halves-are-alike) | [Go](problems/1704.go) | Easy |
231234
| 1710 | [Maximum Units on a Truck](//leetcode.com/problems/maximum-units-on-a-truck) | [Go](problems/1710.go) | Easy |
235+
| 1760 | [Minimum Limit of Balls in a Bag](//leetcode.com/problems/minimum-limit-of-balls-in-a-bag) | [Go](problems/1760.go) | Medium |
232236
| 1768 | [Merge Strings Alternately](//leetcode.com/problems/merge-strings-alternately) | [Go](problems/1768.go) | Easy |
233237
| 1779 | [Find Nearest Point That Has the Same X or Y Coordinate](//leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate) | [Go](problems/1779.go) | Easy |
234238
| 1790 | [Check if One String Swap Can Make Strings Equal](//leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal) | [Go](problems/1790.go) | Easy |
@@ -237,6 +241,7 @@ LeetCode Problems' Solutions
237241
| 1915 | [Number of Wonderful Substrings](//leetcode.com/problems/number-of-wonderful-substrings) | [Go](problems/1915.go) | Medium |
238242
| 2000 | [Reverse Prefix of Word](//leetcode.com/problems/reverse-prefix-of-word) | [Go](problems/2000.go) | Easy |
239243
| 2037 | [Minimum Number of Moves to Seat Everyone](//leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) | [Go](problems/2037.go) | Easy |
244+
| 2054 | [Two Best Non-Overlapping Events](//leetcode.com/problems/two-best-non-overlapping-events) | [Go](problems/2054.go) | Medium |
240245
| 2125 | [Number of Laser Beams in a Bank](//leetcode.com/problems/number-of-laser-beams-in-a-bank) | [Go](problems/2125.go) | Medium |
241246
| 2134 | [Minimum Swaps to Group All 1's Together II](//leetcode.com/problems/minimum-swaps-to-group-all-1s-together-ii) | [Go](problems/2134.go) | Medium |
242247
| 2181 | [Merge Nodes in Between Zeros](//leetcode.com/problems/merge-nodes-in-between-zeros) | [Go](problems/2181.go) | Medium |
@@ -246,12 +251,16 @@ LeetCode Problems' Solutions
246251
| 2321 | [Maximum Score Of Spliced Array](//leetcode.com/problems/maximum-score-of-spliced-array) | [Go](problems/2321.go) | Hard |
247252
| 2326 | [Spiral Matrix IV](//leetcode.com/problems/spiral-matrix-iv) | [Go](problems/2326.go) | Medium |
248253
| 2331 | [Evaluate Boolean Binary Tree](//leetcode.com/problems/evaluate-boolean-binary-tree) | [Go](problems/2331.go) | Easy |
254+
| 2337 | [Move Pieces to Obtain a String](//leetcode.com/problems/move-pieces-to-obtain-a-string) | [Go](problems/2337.go) | Medium |
249255
| 2441 | [Largest Positive Integer That Exists With Its Negative](//leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative) | [Go](problems/2441.go) | Easy |
250256
| 2487 | [Remove Nodes From Linked List](//leetcode.com/problems/remove-nodes-from-linked-list) | [Go](problems/2487.go) | Medium |
251-
| 2597 | [The Number of Beautiful Subsets](//leetcode.com/problems/the-number-of-beautiful-subsets) | [Go](problems/2597.go) | Medium |
257+
| 2554 | [Maximum Number of Integers to Choose From a Range I](//leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i) | [Go](problems/2554.go) | Medium |
252258
| 2816 | [Double a Number Represented as a Linked List](//leetcode.com/problems/double-a-number-represented-as-a-linked-list) | [Go](problems/2816.go) | Medium |
253259
| 2870 | [Minimum Number of Operations to Make Array Empty](//leetcode.com/problems/minimum-number-of-operations-to-make-array-empty) | [Go](problems/2870.go) | Medium |
260+
| 2938 | [Separate Black and White Balls](//leetcode.com/problems/separate-black-and-white-balls) | [Go](problems/2938.go) | Medium |
261+
| 2981 | [Find Longest Special Substring That Occurs Thrice I](//leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i) | [Go](problems/2981.go) | Medium |
254262
| 2997 | [Minimum Number of Operations to Make Array XOR Equal to K](//leetcode.com/problems/minimum-number-of-operations-to-make-array-xor-equal-to-k) | [Go](problems/2997.go) | Medium |
255263
| 3016 | [Minimum Number of Pushes to Type Word II](//leetcode.com/problems/minimum-number-of-pushes-to-type-word-ii) | [Go](problems/3016.go) | Medium |
256264
| 3075 | [Maximize Happiness of Selected Children](//leetcode.com/problems/maximize-happiness-of-selected-children) | [Go](problems/3075.go) | Medium |
265+
| 3152 | [Special Array II](//leetcode.com/problems/special-array-ii) | [Go](problems/3152.go) | Medium |
257266

Diff for: scripts/hooks.sh renamed to hooks.sh

File renamed without changes.

Diff for: problems/0208.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Source: https://leetcode.com/problems/implement-trie-prefix-tree
2+
// Title: Implement Trie (Prefix Tree)
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// A **trie** (https://en.wikipedia.org/wiki/Trie) (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
8+
//
9+
// Implement the Trie class:
10+
//
11+
// - `Trie()` Initializes the trie object.
12+
// - `void insert(String word)` Inserts the string `word` into the trie.
13+
// - `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise.
14+
// - `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise.
15+
//
16+
// **Example 1:**
17+
//
18+
// ```
19+
// Input
20+
//
21+
// ["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
22+
// [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
23+
// Output
24+
//
25+
// [null, null, true, false, true, null, true]
26+
//
27+
// Explanation
28+
//
29+
// Trie trie = new Trie();
30+
// trie.insert("apple");
31+
// trie.search("apple"); // return True
32+
// trie.search("app"); // return False
33+
// trie.startsWith("app"); // return True
34+
// trie.insert("app");
35+
// trie.search("app"); // return True
36+
// ```
37+
//
38+
// **Constraints:**
39+
//
40+
// - `1 <= word.length, prefix.length <= 2000`
41+
// - `word` and `prefix` consist only of lowercase English letters.
42+
// - At most `3 * 10^4` calls **in total** will be made to `insert`, `search`, and `startsWith`.
43+
//
44+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45+
46+
package main
47+
48+
/**
49+
* Your Trie object will be instantiated and called as such:
50+
* obj := Constructor();
51+
* obj.Insert(word);
52+
* param_2 := obj.Search(word);
53+
* param_3 := obj.StartsWith(prefix);
54+
*/
55+
56+
type Trie struct {
57+
children map[rune]*Trie // child nodes
58+
isWord bool // whether this node represent a word
59+
}
60+
61+
func Constructor() Trie {
62+
return Trie{
63+
children: make(map[rune]*Trie),
64+
}
65+
}
66+
67+
func (this *Trie) Insert(word string) {
68+
node := this
69+
for _, ch := range word {
70+
nextNode := node.children[ch]
71+
if nextNode == nil {
72+
newNode := Constructor()
73+
nextNode = &newNode
74+
node.children[ch] = nextNode
75+
}
76+
node = nextNode
77+
}
78+
node.isWord = true
79+
}
80+
81+
func (this *Trie) Search(word string) bool {
82+
node := this
83+
for _, ch := range word {
84+
nextNode := node.children[ch]
85+
if nextNode == nil {
86+
return false
87+
}
88+
node = nextNode
89+
}
90+
return node.isWord
91+
}
92+
93+
func (this *Trie) StartsWith(prefix string) bool {
94+
node := this
95+
for _, ch := range prefix {
96+
nextNode := node.children[ch]
97+
if nextNode == nil {
98+
return false
99+
}
100+
node = nextNode
101+
}
102+
return true
103+
}

Diff for: problems/0378.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import (
4545
type intHeap []int
4646

4747
func (h intHeap) Len() int { return len(h) }
48-
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // we want max-heap
48+
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // max-heap
4949
func (h intHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5050

5151
func newIntHeap(arr []int) *intHeap {

Diff for: problems/0630.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
type intHeap []int
4949

5050
func (h intHeap) Len() int { return len(h) }
51-
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // we want max-heap
51+
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // max-heap
5252
func (h intHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5353

5454
func newIntHeap(arr []int) *intHeap {

Diff for: problems/1354.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import (
5151
type intHeap []int
5252

5353
func (h intHeap) Len() int { return len(h) }
54-
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // we want max-heap
54+
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // max-heap
5555
func (h intHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5656

5757
func newIntHeap(arr []int) *intHeap {

Diff for: problems/1405.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Source: https://leetcode.com/problems/longest-happy-string
2+
// Title: Longest Happy String
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// A string s is called happy if it satisfies the following conditions:
8+
//
9+
// * s only contains the letters 'a', 'b', and 'c'.
10+
// * s does not contain any of "aaa", "bbb", or "ccc" as a substring.
11+
// * s contains at most a occurrences of the letter 'a'.
12+
// * s contains at most b occurrences of the letter 'b'.
13+
// * s contains at most c occurrences of the letter 'c'.
14+
//
15+
// Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "".
16+
//
17+
// A substring is a contiguous sequence of characters within a string.
18+
//
19+
// Example 1:
20+
//
21+
// Input: a = 1, b = 1, c = 7
22+
// Output: "ccaccbcc"
23+
// Explanation: "ccbccacc" would also be a correct answer.
24+
//
25+
// Example 2:
26+
//
27+
// Input: a = 7, b = 1, c = 0
28+
// Output: "aabaa"
29+
// Explanation: It is the only correct answer in this case.
30+
//
31+
// Constraints:
32+
//
33+
// 0 <= a, b, c <= 100
34+
// a + b + c > 0
35+
//
36+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37+
38+
package main
39+
40+
func longestDiverseString(a int, b int, c int) string {
41+
x, y, z := "a", "b", "c"
42+
// Sort
43+
if a < b {
44+
a, b = b, a
45+
x, y = y, x
46+
}
47+
if b < c {
48+
b, c = c, b
49+
y, z = z, y
50+
}
51+
if a < b {
52+
a, b = b, a
53+
x, y = y, x
54+
}
55+
56+
res := ""
57+
for a > 0 {
58+
if a > b && a >= 2 {
59+
res += x + x
60+
a -= 2
61+
} else {
62+
res += x
63+
a -= 1
64+
}
65+
a, b = b, a
66+
x, y = y, x
67+
68+
if b < c {
69+
b, c = c, b
70+
y, z = z, y
71+
}
72+
}
73+
74+
return res
75+
}

Diff for: problems/1593.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Source: https://leetcode.com/problems/split-a-string-into-the-max-number-of-unique-substrings
2+
// Split a String Into the Max Number of Unique Substrings
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Given a string s, return the maximum number of unique substrings that the given string can be split into.
8+
// You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.
9+
// A substring is a contiguous sequence of characters within a string.
10+
//
11+
// Example 1:
12+
//
13+
// Input: s = "ababccc"
14+
// Output: 5
15+
// Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
16+
//
17+
// Example 2:
18+
//
19+
// Input: s = "aba"
20+
// Output: 2
21+
// Explanation: One way to split maximally is ['a', 'ba'].
22+
//
23+
// Example 3:
24+
//
25+
// Input: s = "aa"
26+
// Output: 1
27+
// Explanation: It is impossible to split the string any further.
28+
//
29+
// Constraints:
30+
//
31+
// 1 <= s.length <= 16
32+
// s contains only lower case English letters.
33+
//
34+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
35+
36+
package main
37+
38+
// Loop for all possible splits
39+
func maxUniqueSplit(s string) int {
40+
n := len(s) - 1
41+
m := uint(1) << n
42+
43+
res := 0
44+
45+
OUTER:
46+
for bits := uint(0); bits < m; bits++ {
47+
words := make(map[string]bool, n+1)
48+
49+
// Construct split
50+
substr := ""
51+
for b := 0; b < n; b++ { // loop for bits
52+
substr += string(s[b])
53+
if (bits >> b & 1) == 1 {
54+
if words[substr] {
55+
continue OUTER
56+
}
57+
words[substr] = true
58+
substr = ""
59+
}
60+
}
61+
substr += string(s[n])
62+
if words[substr] {
63+
continue OUTER
64+
}
65+
words[substr] = true
66+
67+
res = max(res, len(words))
68+
}
69+
70+
return res
71+
}
72+
73+
// Backtracking
74+
func maxUniqueSplit2(s string) int {
75+
n := len(s)
76+
seen := make(map[string]bool, 1<<n)
77+
78+
var backtrack func(start int) int
79+
backtrack = func(start int) int {
80+
if start == n {
81+
return 0
82+
}
83+
84+
maxSplits := 0
85+
for end := start + 1; end <= n; end++ {
86+
word := s[start:end]
87+
if seen[word] {
88+
continue
89+
}
90+
91+
seen[word] = true
92+
maxSplits = max(maxSplits, 1+backtrack(end))
93+
seen[word] = false
94+
}
95+
return maxSplits
96+
}
97+
98+
return backtrack(0)
99+
}

Diff for: problems/1642.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import (
5757
type intHeap []int
5858

5959
func (h intHeap) Len() int { return len(h) }
60-
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // we want max-heap
60+
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // max-heap
6161
func (h intHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
6262

6363
func newIntHeap(arr []int) *intHeap {

Diff for: problems/1647.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
type intHeap []int
4949

5050
func (h intHeap) Len() int { return len(h) }
51-
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // we want max-heap
51+
func (h intHeap) Less(i, j int) bool { return h[i] > h[j] } // max-heap
5252
func (h intHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5353

5454
func newIntHeap(arr []int) *intHeap {

Diff for: problems/1696.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type pair struct {
5050
type pairHeap []pair
5151

5252
func (h pairHeap) Len() int { return len(h) }
53-
func (h pairHeap) Less(i, j int) bool { return h[i].val > h[j].val } // we want max-heap
53+
func (h pairHeap) Less(i, j int) bool { return h[i].val > h[j].val } // max-heap
5454
func (h pairHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5555

5656
func newIntHeap(arr []pair) *pairHeap {

0 commit comments

Comments
 (0)