Skip to content

Commit 769c8a2

Browse files
committed
49, 380, 931, 1026, 1074, 1207, 1289, 1347, 1657, 1704, 2225.
1 parent 4cbfc76 commit 769c8a2

File tree

13 files changed

+700
-31
lines changed

13 files changed

+700
-31
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LeetCode Problems' Solutions
4141
| 45 | [Jump Game II](//leetcode.com/problems/jump-game-ii/) | [Go](./problems/algorithms/0045.go) [Python](./problems/algorithms/0045.py) | Hard |
4242
| 46 | [Permutations](//leetcode.com/problems/permutations/) | [Python](./problems/algorithms/0046.py) | Medium |
4343
| 48 | [Rotate Image](//leetcode.com/problems/rotate-image/) | [Python](./problems/algorithms/0048.py) | Medium |
44-
| 49 | [Group Anagrams](//leetcode.com/problems/group-anagrams/) | [Python](./problems/algorithms/0049.py) | Medium |
44+
| 49 | [Group Anagrams](//leetcode.com/problems/group-anagrams/) | [Go](./problems/algorithms/0049.go) [Python](./problems/algorithms/0049.py) | Medium |
4545
| 51 | [N-Queens](//leetcode.com/problems/n-queens/) | [Go](./problems/algorithms/0051.go) | Hard |
4646
| 52 | [N-Queens II](//leetcode.com/problems/n-queens-ii/) | [Go](./problems/algorithms/0052.go) | Hard |
4747
| 53 | [Maximum Subarray](//leetcode.com/problems/maximum-subarray/) | [Go](./problems/algorithms/0053.go) [Python](./problems/algorithms/0053.py) | Easy |
@@ -120,6 +120,7 @@ LeetCode Problems' Solutions
120120
| 376 | [Wiggle Subsequence](//leetcode.com/problems/wiggle-subsequence/) | [Go](./problems/algorithms/0376.go) | Medium |
121121
| 377 | [Combination Sum IV](//leetcode.com/problems/combination-sum-iv/) | [Go](./problems/algorithms/0377.go) | Medium |
122122
| 378 | [Kth Smallest Element in a Sorted Matrix](//leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [Go](./problems/algorithms/0378.go) | Medium |
123+
| 380 | [Insert Delete GetRandom O(1)](//leetcode.com/problems/insert-delete-getrandom-o1/) | [Go](./problems/algorithms/0380.go) | Medium |
123124
| 383 | [Ransom Note](//leetcode.com/problems/ransom-note/) | [Go](./problems/algorithms/0383.go) | Easy |
124125
| 387 | [Roman to Integer](//leetcode.com/problems/first-unique-character-in-a-string/) | [Go](./problems/algorithms/0387.go) | Easy |
125126
| 389 | [Find the Difference](//leetcode.com/problems/find-the-difference/) | [Go](./problems/algorithms/0389.go) | Easy |
@@ -163,20 +164,24 @@ LeetCode Problems' Solutions
163164
| 968 | [Binary Tree Cameras](//leetcode.com/problems/binary-tree-cameras/) | [Go](./problems/algorithms/0968.go) | Hard |
164165
| 976 | [Largest Perimeter Triangle](//leetcode.com/problems/largest-perimeter-triangle/) | [Go](./problems/algorithms/0976.go) | Easy |
165166
| 1014 | [Best Sightseeing Pair](//leetcode.com/problems/best-sightseeing-pair/) | [Go](./problems/algorithms/1014.go) | Medium |
167+
| 1026 | [Determine if String Halves Are Alike](//leetcode.com/problems/maximum-difference-between-node-and-ancestor/) | [Go](./problems/algorithms/1026.go) | Medium |
166168
| 1048 | [Longest String Chain](//leetcode.com/problems/longest-string-chain/) | [Go](./problems/algorithms/1048.go) | Medium |
167169
| 1074 | [Number of Submatrices That Sum to Target](//leetcode.com/problems/number-of-submatrices-that-sum-to-target/) | [Go](./problems/algorithms/1074.go) | Hard |
168170
| 1137 | [N-th Tribonacci Number](//leetcode.com/problems/n-th-tribonacci-number/) | [Go](./problems/algorithms/1137.go) | Easy |
169171
| 1143 | [Longest Common Subsequence](//leetcode.com/problems/longest-common-subsequence/) | [Go](./problems/algorithms/1143.go) | Medium |
172+
| 1207 | [Unique Number of Occurrences](//leetcode.com/problems/unique-number-of-occurrences) | [Go](./problems/algorithms/1207.go) | Easy |
170173
| 1220 | [Count Vowels Permutation](//leetcode.com/problems/count-vowels-permutation/) | [Go](./problems/algorithms/1220.go) | Hard |
171174
| 1232 | [Check If It Is a Straight Line](//leetcode.com/problems/check-if-it-is-a-straight-line/) | [Go](./problems/algorithms/1232.go) | Easy |
172175
| 1268 | [Search Suggestions System](//leetcode.com/problems/search-suggestions-system/) | [Go](./problems/algorithms/1268.go) | Medium |
173176
| 1281 | [Subtract the Product and Sum of Digits of an Integer](//leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/) | [Go](./problems/algorithms/1281.go) | Easy |
177+
| 1289 | [Minimum Falling Path Sum II](//leetcode.com/problems/minimum-falling-path-sum-ii) | [Go](./problems/algorithms/1289.go) | Hard |
174178
| 1290 | [Convert Binary Number in a Linked List to Integer](//leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Go](./problems/algorithms/1290.go) | Easy |
175179
| 1309 | [Decrypt String from Alphabet to Integer Mapping](//leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/) | [Go](./problems/algorithms/1309.go) | Easy |
176180
| 1314 | [Matrix Block Sum](//leetcode.com/problems/matrix-block-sum/) | [Go](./problems/algorithms/1314.go) | Medium |
177181
| 1332 | [Remove Palindromic Subsequences](//leetcode.com/problems/remove-palindromic-subsequences/) | [Go](./problems/algorithms/1332.go) | Easy |
178182
| 1338 | [Reduce Array Size to The Half](//leetcode.com/problems/reduce-array-size-to-the-half/) | [Go](./problems/algorithms/1338.go) | Medium |
179183
| 1342 | [Number of Steps to Reduce a Number to Zero](//leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [C++](./problems/algorithms/1342.cpp) [Go](./problems/algorithms/1342.go) | Easy |
184+
| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](//leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | [Go](./problems/algorithms/1347.go) | Medium |
180185
| 1354 | [Construct Target Array With Multiple Sums](//leetcode.com/problems/construct-target-array-with-multiple-sums/) | [Go](./problems/algorithms/1354.go) | Hard |
181186
| 1356 | [Sort Integers by The Number of 1 Bits](//leetcode.com/problems/sort-integers-by-the-number-of-1-bits/) | [Go](./problems/algorithms/1356.go) | Easy |
182187
| 1423 | [Maximum Points You Can Obtain from Cards](//leetcode.com/problems/maximum-points-you-can-obtain-from-cards/) | [Go](./problems/algorithms/1423.go) | Medium |
@@ -193,18 +198,21 @@ LeetCode Problems' Solutions
193198
| 1603 | [Design Parking System](//leetcode.com/problems/design-parking-system/) | [Go](./problems/algorithms/1603.go) | Easy |
194199
| 1642 | [Furthest Building You Can Reach](//leetcode.com/problems/furthest-building-you-can-reach/) | [Go](./problems/algorithms/1642.go) | Medium |
195200
| 1647 | [Minimum Deletions to Make Character Frequencies Unique](//leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) | [Go](./problems/algorithms/1647.go) | Medium |
201+
| 1657 | [Determine if Two Strings Are Close](//leetcode.com/problems/determine-if-two-strings-are-close/) | [Go](./problems/algorithms/1657.go) | Medium |
196202
| 1658 | [Minimum Operations to Reduce X to Zero](//leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | [Go](./problems/algorithms/1658.go) | Medium |
197203
| 1672 | [Richest Customer Wealth](//leetcode.com/problems/richest-customer-wealth/) | [Go](./problems/algorithms/1672.go) | Easy |
198204
| 1678 | [Goal Parser Interpretation](//leetcode.com/problems/goal-parser-interpretation/) | [Go](./problems/algorithms/1678.go) | Easy |
199205
| 1689 | [Partitioning Into Minimum Number Of Deci-Binary Numbers](//leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/) | [Go](./problems/algorithms/1689.go) | Medium |
200206
| 1695 | [Maximum Erasure Value](//leetcode.com/problems/maximum-erasure-value/) | [Go](./problems/algorithms/1695.go) | Medium |
201207
| 1696 | [Jump Game VI](//leetcode.com/problems/jump-game-vi/) | [Go](./problems/algorithms/1696.go) | Medium |
208+
| 1704 | [Determine if String Halves Are Alike](//leetcode.com/problems/determine-if-string-halves-are-alike/) | [Go](./problems/algorithms/1704.go) | Easy |
202209
| 1710 | [Maximum Units on a Truck](//leetcode.com/problems/maximum-units-on-a-truck/) | [Go](./problems/algorithms/1710.go) | Easy |
203210
| 1768 | [Merge Strings Alternately](//leetcode.com/problems/merge-strings-alternately/) | [Go](./problems/algorithms/1768.go) | Easy |
204211
| 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/algorithms/1779.go) | Easy |
205212
| 1790 | [Check if One String Swap Can Make Strings Equal](//leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/) | [Go](./problems/algorithms/1790.go) | Easy |
206213
| 1822 | [Sign of the Product of an Array](//leetcode.com/problems/sign-of-the-product-of-an-array/) | [Go](./problems/algorithms/1822.go) | Easy |
207214
| 2125 | [Number of Laser Beams in a Bank](//leetcode.com/problems/number-of-laser-beams-in-a-bank) | [Go](./problems/algorithms/2125.go) | Medium |
215+
| 2225 | [Find Players With Zero or One Losses](//leetcode.com/problems/number-of-laser-beams-in-a-bank) | [Go](./problems/algorithms/2225.go) | Medium |
208216
| 2319 | [Check if Matrix Is X-Matrix](//leetcode.com/problems/check-if-matrix-is-x-matrix/) | [Go](./problems/algorithms/2319.go) | Easy |
209217
| 2320 | [Count Number of Ways to Place Houses](//leetcode.com/problems/count-number-of-ways-to-place-houses/) | [Go](./problems/algorithms/2320.go) | Medium |
210218
| 2321 | [Maximum Score Of Spliced Array](//leetcode.com/problems/maximum-score-of-spliced-array/) | [Go](./problems/algorithms/2321.go) | Hard |

problems/algorithms/0049.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Source: https://leetcode.com/problems/group-anagrams/
2+
// Title: Group Anagrams
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Given an array of strings strs, group the anagrams together. You can return the answer in any order.
8+
// An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
9+
//
10+
// Example 1:
11+
//
12+
// Input: strs = ["eat","tea","tan","ate","nat","bat"]
13+
// Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
14+
//
15+
// Example 2:
16+
//
17+
// Input: strs = [""]
18+
// Output: [[""]]
19+
//
20+
// Example 3:
21+
//
22+
// Input: strs = ["a"]
23+
// Output: [["a"]]
24+
//
25+
// Constraints:
26+
//
27+
// * 1 <= strs.length <= 10^4
28+
// * 0 <= strs[i].length <= 100
29+
// * strs[i] consists of lowercase English letters.
30+
//
31+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32+
33+
package main
34+
35+
import (
36+
"sort"
37+
)
38+
39+
type RuneSlice []rune
40+
41+
func (x RuneSlice) Len() int { return len(x) }
42+
func (x RuneSlice) Less(i, j int) bool { return x[i] < x[j] }
43+
func (x RuneSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
44+
45+
func groupAnagrams(strs []string) [][]string {
46+
groupMap := make(map[string][]string, len(strs))
47+
for _, str := range strs {
48+
r := RuneSlice(str)
49+
sort.Sort(r)
50+
sorted := string(r)
51+
if _, ok := groupMap[sorted]; !ok {
52+
groupMap[sorted] = make([]string, 0)
53+
}
54+
groupMap[sorted] = append(groupMap[sorted], str)
55+
}
56+
57+
groups := make([][]string, 0, len(groupMap))
58+
for _, group := range groupMap {
59+
groups = append(groups, group)
60+
}
61+
return groups
62+
}

problems/algorithms/0380.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Source: https://leetcode.com/problems/insert-delete-getrandom-o1/
2+
// Title: Insert Delete GetRandom O(1)
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Implement the RandomizedSet class:
8+
//
9+
// * RandomizedSet() Initializes the RandomizedSet object.
10+
// * bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
11+
// * bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
12+
// * int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
13+
//
14+
// You must implement the functions of the class such that each function works in average O(1) time complexity.
15+
//
16+
// Example 1:
17+
//
18+
// Input
19+
// ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
20+
// [[], [1], [2], [2], [], [1], [2], []]
21+
// Output
22+
// [null, true, false, true, 2, true, false, 2]
23+
// Explanation
24+
// RandomizedSet randomizedSet = new RandomizedSet();
25+
// randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
26+
// randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
27+
// randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
28+
// randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
29+
// randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
30+
// randomizedSet.insert(2); // 2 was already in the set, so return false.
31+
// randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.
32+
//
33+
// Constraints:
34+
//
35+
// * -2^31 <= val <= 2^31-1
36+
// * At most 2 * 10^5 calls will be made to insert, remove, and getRandom.s
37+
// * There will be at least one element in the data structure when getRandom is called.
38+
//
39+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40+
41+
package main
42+
43+
import (
44+
"math/rand"
45+
)
46+
47+
/**
48+
* Your RandomizedSet object will be instantiated and called as such:
49+
* obj := Constructor();
50+
* param_1 := obj.Insert(val);
51+
* param_2 := obj.Remove(val);
52+
* param_3 := obj.GetRandom();
53+
*/
54+
55+
type RandomizedSet struct {
56+
keys []int
57+
idxs map[int]int // map key to its index
58+
}
59+
60+
func Constructor() RandomizedSet {
61+
return RandomizedSet{
62+
keys: make([]int, 0),
63+
idxs: make(map[int]int),
64+
}
65+
}
66+
67+
func (this *RandomizedSet) Insert(val int) bool {
68+
_, exist := this.idxs[val]
69+
if exist {
70+
return false
71+
}
72+
73+
this.keys = append(this.keys, val)
74+
this.idxs[val] = len(this.keys) - 1
75+
return true
76+
}
77+
78+
func (this *RandomizedSet) Remove(val int) bool {
79+
idx, exist := this.idxs[val]
80+
if !exist {
81+
return false
82+
}
83+
84+
// Swap with last element
85+
lastVal := this.keys[len(this.keys)-1]
86+
this.keys[idx] = lastVal
87+
this.idxs[lastVal] = idx
88+
delete(this.idxs, val)
89+
this.keys = this.keys[:len(this.keys)-1]
90+
return true
91+
}
92+
93+
func (this *RandomizedSet) GetRandom() int {
94+
idx := rand.Intn(len(this.keys))
95+
return this.keys[idx]
96+
}

problems/algorithms/0931.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,19 @@ func minFallingPathSum(matrix [][]int) int {
5151
}
5252

5353
for _, row := range matrix[1:] {
54-
next[0] = _min(prev[0], prev[1]) + row[0]
54+
next[0] = min(prev[0], prev[1]) + row[0]
5555
for i := 1; i < n-1; i++ {
56-
next[i] = _min3(prev[i-1], prev[i], prev[i+1]) + row[i]
56+
next[i] = min(prev[i-1], prev[i], prev[i+1]) + row[i]
5757
}
58-
next[n-1] = _min(prev[n-2], prev[n-1]) + row[n-1]
58+
next[n-1] = min(prev[n-2], prev[n-1]) + row[n-1]
5959

6060
prev, next = next, prev
6161
}
6262

6363
res := math.MaxInt32
6464
for _, v := range prev {
65-
res = _min(res, v)
65+
res = min(res, v)
6666
}
6767

6868
return res
6969
}
70-
71-
func _min3(a, b, c int) int {
72-
return _min(_min(a, b), c)
73-
}
74-
75-
func _min(a, b int) int {
76-
if a < b {
77-
return a
78-
}
79-
return b
80-
}

problems/algorithms/1026.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Source: https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/
2+
// Title: Determine if String Halves Are Alike
3+
// Difficulty: Medium
4+
// Author: Mu Yang <http://muyang.pro>
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.
8+
//
9+
// A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b.
10+
//
11+
// Example 1:
12+
//
13+
// https://assets.leetcode.com/uploads/2020/11/09/tmp-tree.jpg
14+
//
15+
// Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
16+
// Output: 7
17+
// Explanation:
18+
// We have various ancestor-node differences, some of which are given below :
19+
// |8 - 3| = 5
20+
// |3 - 7| = 4
21+
// |8 - 1| = 7
22+
// |10 - 13| = 3
23+
// Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
24+
//
25+
// Example 2:
26+
//
27+
// https://assets.leetcode.com/uploads/2020/11/09/tmp-tree-1.jpg
28+
//
29+
// Input: root = [1,null,2,null,0,3]
30+
// Output: 3
31+
//
32+
// Constraints:
33+
//
34+
// The number of nodes in the tree is in the range [2, 5000].
35+
// 0 <= Node.val <= 10^5
36+
//
37+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38+
39+
package main
40+
41+
type TreeNode struct {
42+
Val int
43+
Left *TreeNode
44+
Right *TreeNode
45+
}
46+
47+
func maxAncestorDiff(root *TreeNode) int {
48+
return _maxAncestorDiff(root, root.Val, root.Val)
49+
}
50+
51+
func _maxAncestorDiff(root *TreeNode, min, max int) int {
52+
if root == nil {
53+
return 0
54+
}
55+
56+
min = _min(min, root.Val)
57+
max = _max(max, root.Val)
58+
res := _max(root.Val-min, max-root.Val)
59+
60+
// Left
61+
res = _max(res, _maxAncestorDiff(root.Left, min, max))
62+
63+
// Right
64+
res = _max(res, _maxAncestorDiff(root.Right, min, max))
65+
66+
return res
67+
}
68+
69+
func _max(a, b int) int {
70+
if a > b {
71+
return a
72+
}
73+
return b
74+
}
75+
76+
func _min(a, b int) int {
77+
if a < b {
78+
return a
79+
}
80+
return b
81+
}

problems/algorithms/1074.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ func numSubmatrixSumTarget(matrix [][]int, target int) int {
4343
n := len(matrix[0])
4444

4545
// Compute prefix sums
46-
sum := make([][]int, m+1)
46+
presum := make([][]int, m+1)
4747
for i := 0; i < m+1; i++ {
48-
sum[i] = make([]int, n+1)
48+
presum[i] = make([]int, n+1)
4949
}
5050
for i := 0; i < m; i++ {
5151
for j := 0; j < n; j++ {
52-
sum[i+1][j+1] = matrix[i][j] + sum[i][j+1] + sum[i+1][j] - sum[i][j]
52+
presum[i+1][j+1] = matrix[i][j] + presum[i][j+1] + presum[i+1][j] - presum[i][j]
5353
}
5454
}
5555

@@ -59,7 +59,7 @@ func numSubmatrixSumTarget(matrix [][]int, target int) int {
5959
for x2 := x1 + 1; x2 <= m; x2++ {
6060
for y1 := 0; y1 < n; y1++ {
6161
for y2 := y1 + 1; y2 <= n; y2++ {
62-
subsum := sum[x2][y2] - sum[x2][y1] - sum[x1][y2] + sum[x1][y1]
62+
subsum := presum[x2][y2] - presum[x2][y1] - presum[x1][y2] + presum[x1][y1]
6363
if subsum == target {
6464
res++
6565
}

0 commit comments

Comments
 (0)