Skip to content

Commit d89865f

Browse files
authored
feat: add solutions to lc problems: No.0846,1296 (#4282)
1 parent a76908b commit d89865f

24 files changed

+3684
-581
lines changed

solution/0800-0899/0846.Hand of Straights/README.md

+604-114
Large diffs are not rendered by default.

solution/0800-0899/0846.Hand of Straights/README_EN.md

+612-110
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
class Solution {
22
public:
33
bool isNStraightHand(vector<int>& hand, int groupSize) {
4+
if (hand.size() % groupSize) {
5+
return false;
6+
}
7+
ranges::sort(hand);
48
unordered_map<int, int> cnt;
5-
for (int& v : hand) ++cnt[v];
6-
sort(hand.begin(), hand.end());
7-
for (int& v : hand) {
8-
if (cnt.count(v)) {
9-
for (int x = v; x < v + groupSize; ++x) {
10-
if (!cnt.count(x)) {
9+
for (int x : hand) {
10+
++cnt[x];
11+
}
12+
for (int x : hand) {
13+
if (cnt.contains(x)) {
14+
for (int y = x; y < x + groupSize; ++y) {
15+
if (!cnt.contains(y)) {
1116
return false;
1217
}
13-
if (--cnt[x] == 0) {
14-
cnt.erase(x);
18+
if (--cnt[y] == 0) {
19+
cnt.erase(y);
1520
}
1621
}
1722
}
1823
}
1924
return true;
2025
}
21-
};
26+
};
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
func isNStraightHand(hand []int, groupSize int) bool {
2-
cnt := map[int]int{}
3-
for _, v := range hand {
4-
cnt[v]++
2+
if len(hand)%groupSize != 0 {
3+
return false
54
}
65
sort.Ints(hand)
7-
for _, v := range hand {
8-
if _, ok := cnt[v]; ok {
9-
for x := v; x < v+groupSize; x++ {
10-
if _, ok := cnt[x]; !ok {
6+
cnt := map[int]int{}
7+
for _, x := range hand {
8+
cnt[x]++
9+
}
10+
for _, x := range hand {
11+
if cnt[x] > 0 {
12+
for y := x; y < x+groupSize; y++ {
13+
if cnt[y] == 0 {
1114
return false
1215
}
13-
cnt[x]--
14-
if cnt[x] == 0 {
15-
delete(cnt, x)
16-
}
16+
cnt[y]--
1717
}
1818
}
1919
}
2020
return true
21-
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
class Solution {
22
public boolean isNStraightHand(int[] hand, int groupSize) {
3-
Map<Integer, Integer> cnt = new HashMap<>();
4-
for (int v : hand) {
5-
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
3+
if (hand.length % groupSize != 0) {
4+
return false;
65
}
76
Arrays.sort(hand);
8-
for (int v : hand) {
9-
if (cnt.containsKey(v)) {
10-
for (int x = v; x < v + groupSize; ++x) {
11-
if (!cnt.containsKey(x)) {
7+
Map<Integer, Integer> cnt = new HashMap<>();
8+
for (int x : hand) {
9+
cnt.merge(x, 1, Integer::sum);
10+
}
11+
for (int x : hand) {
12+
if (cnt.getOrDefault(x, 0) > 0) {
13+
for (int y = x; y < x + groupSize; ++y) {
14+
if (cnt.merge(y, -1, Integer::sum) < 0) {
1215
return false;
1316
}
14-
cnt.put(x, cnt.get(x) - 1);
15-
if (cnt.get(x) == 0) {
16-
cnt.remove(x);
17-
}
1817
}
1918
}
2019
}
2120
return true;
2221
}
23-
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
3+
if len(hand) % groupSize:
4+
return False
35
cnt = Counter(hand)
4-
for v in sorted(hand):
5-
if cnt[v]:
6-
for x in range(v, v + groupSize):
7-
if cnt[x] == 0:
6+
for x in sorted(hand):
7+
if cnt[x]:
8+
for y in range(x, x + groupSize):
9+
if cnt[y] == 0:
810
return False
9-
cnt[x] -= 1
10-
if cnt[x] == 0:
11-
cnt.pop(x)
11+
cnt[y] -= 1
1212
return True
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
function isNStraightHand(hand: number[], groupSize: number) {
2-
const cnt: Record<number, number> = {};
3-
for (const i of hand) {
4-
cnt[i] = (cnt[i] ?? 0) + 1;
1+
function isNStraightHand(hand: number[], groupSize: number): boolean {
2+
if (hand.length % groupSize !== 0) {
3+
return false;
54
}
6-
7-
const keys = Object.keys(cnt).map(Number);
8-
for (const i of keys) {
9-
while (cnt[i]) {
10-
for (let j = i; j < groupSize + i; j++) {
11-
if (!cnt[j]) {
5+
const cnt = new Map<number, number>();
6+
for (const x of hand) {
7+
cnt.set(x, (cnt.get(x) || 0) + 1);
8+
}
9+
hand.sort((a, b) => a - b);
10+
for (const x of hand) {
11+
if (cnt.get(x)! > 0) {
12+
for (let y = x; y < x + groupSize; y++) {
13+
if ((cnt.get(y) || 0) === 0) {
1214
return false;
1315
}
14-
cnt[j]--;
16+
cnt.set(y, cnt.get(y)! - 1);
1517
}
1618
}
1719
}
18-
1920
return true;
2021
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
class Solution {
22
public:
33
bool isNStraightHand(vector<int>& hand, int groupSize) {
4-
if (hand.size() % groupSize != 0) return false;
4+
if (hand.size() % groupSize) {
5+
return false;
6+
}
57
map<int, int> mp;
6-
for (int& h : hand) mp[h] += 1;
8+
for (int x : hand) {
9+
++mp[x];
10+
}
711
while (!mp.empty()) {
8-
int v = mp.begin()->first;
9-
for (int i = v; i < v + groupSize; ++i) {
10-
if (!mp.count(i)) return false;
11-
if (mp[i] == 1)
12-
mp.erase(i);
13-
else
14-
mp[i] -= 1;
12+
int x = mp.begin()->first;
13+
for (int y = x; y < x + groupSize; ++y) {
14+
if (!mp.contains(y)) {
15+
return false;
16+
}
17+
if (--mp[y] == 0) {
18+
mp.erase(y);
19+
}
1520
}
1621
}
1722
return true;
1823
}
19-
};
24+
};

solution/0800-0899/0846.Hand of Straights/Solution2.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ func isNStraightHand(hand []int, groupSize int) bool {
22
if len(hand)%groupSize != 0 {
33
return false
44
}
5-
m := treemap.NewWithIntComparator()
6-
for _, h := range hand {
7-
if v, ok := m.Get(h); ok {
8-
m.Put(h, v.(int)+1)
5+
tm := treemap.NewWithIntComparator()
6+
for _, x := range hand {
7+
if v, ok := tm.Get(x); ok {
8+
tm.Put(x, v.(int)+1)
99
} else {
10-
m.Put(h, 1)
10+
tm.Put(x, 1)
1111
}
1212
}
13-
for !m.Empty() {
14-
v, _ := m.Min()
15-
for i := v.(int); i < v.(int)+groupSize; i++ {
16-
if _, ok := m.Get(i); !ok {
17-
return false
18-
}
19-
if v, _ := m.Get(i); v.(int) == 1 {
20-
m.Remove(i)
13+
for !tm.Empty() {
14+
x, _ := tm.Min()
15+
for y := x.(int); y < x.(int)+groupSize; y++ {
16+
if v, ok := tm.Get(y); ok {
17+
if v.(int) == 1 {
18+
tm.Remove(y)
19+
} else {
20+
tm.Put(y, v.(int)-1)
21+
}
2122
} else {
22-
m.Put(i, v.(int)-1)
23+
return false
2324
}
2425
}
2526
}
2627
return true
27-
}
28+
}

solution/0800-0899/0846.Hand of Straights/Solution2.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ public boolean isNStraightHand(int[] hand, int groupSize) {
44
return false;
55
}
66
TreeMap<Integer, Integer> tm = new TreeMap<>();
7-
for (int h : hand) {
8-
tm.put(h, tm.getOrDefault(h, 0) + 1);
7+
for (int x : hand) {
8+
tm.merge(x, 1, Integer::sum);
99
}
1010
while (!tm.isEmpty()) {
11-
int v = tm.firstKey();
12-
for (int i = v; i < v + groupSize; ++i) {
13-
if (!tm.containsKey(i)) {
11+
int x = tm.firstKey();
12+
for (int y = x; y < x + groupSize; ++y) {
13+
int t = tm.merge(y, -1, Integer::sum);
14+
if (t < 0) {
1415
return false;
1516
}
16-
if (tm.get(i) == 1) {
17-
tm.remove(i);
18-
} else {
19-
tm.put(i, tm.get(i) - 1);
17+
if (t == 0) {
18+
tm.remove(y);
2019
}
2120
}
2221
}
2322
return true;
2423
}
25-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
class Solution:
22
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
3-
if len(hand) % groupSize != 0:
3+
if len(hand) % groupSize:
44
return False
5-
sd = SortedDict()
6-
for h in hand:
7-
if h in sd:
8-
sd[h] += 1
9-
else:
10-
sd[h] = 1
5+
cnt = Counter(hand)
6+
sd = SortedDict(cnt)
117
while sd:
12-
v = sd.peekitem(0)[0]
13-
for i in range(v, v + groupSize):
14-
if i not in sd:
8+
x = next(iter(sd))
9+
for y in range(x, x + groupSize):
10+
if y not in sd:
1511
return False
16-
if sd[i] == 1:
17-
sd.pop(i)
12+
if sd[y] == 1:
13+
del sd[y]
1814
else:
19-
sd[i] -= 1
15+
sd[y] -= 1
2016
return True

0 commit comments

Comments
 (0)