Skip to content

Commit 8eaf66b

Browse files
committed
feat: add solutions to lc problems: No.0846,1296
* No.0846.Hand of Straights * No.1296.Divide Array in Sets of K Consecutive Numbers
1 parent 9ff52c2 commit 8eaf66b

File tree

12 files changed

+486
-136
lines changed

12 files changed

+486
-136
lines changed

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

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,43 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
TreeMap 实现。
50+
**方法一:哈希表 + 排序**
51+
52+
我们先用哈希表 `cnt` 统计数组 `hand` 中每个数字出现的次数,然后对数组 `hand` 进行排序。
53+
54+
接下来,我们遍历数组 `hand`,对于数组中的每个数字 $v$,如果 $v$ 在哈希表 `cnt` 中出现的次数不为 $0$,则我们枚举 $v$ 到 $v+groupSize-1$ 的每个数字,如果这些数字在哈希表 `cnt` 中出现的次数都不为 $0$,则我们将这些数字的出现次数减 $1$,如果减 $1$ 后这些数字的出现次数为 $0$,则我们在哈希表 `cnt` 中删除这些数字。否则说明无法将数组划分成若干个长度为 $groupSize$ 的子数组,返回 `false`。如果可以将数组划分成若干个长度为 $groupSize$ 的子数组,则遍历结束后返回 `true`
55+
56+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `hand` 的长度。
57+
58+
**方法二:有序集合**
59+
60+
我们也可以使用有序集合统计数组 `hand` 中每个数字出现的次数。
61+
62+
接下来,循环取出有序集合中的最小值 $v$,然后枚举 $v$ 到 $v+groupSize-1$ 的每个数字,如果这些数字在有序集合中出现的次数都不为 $0$,则我们将这些数字的出现次数减 $1$,如果出现次数减 $1$ 后为 $0$,则将该数字从有序集合中删除,否则说明无法将数组划分成若干个长度为 $groupSize$ 的子数组,返回 `false`。如果可以将数组划分成若干个长度为 $groupSize$ 的子数组,则遍历结束后返回 `true`
63+
64+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `hand` 的长度。
5165

5266
<!-- tabs:start -->
5367

5468
### **Python3**
5569

5670
<!-- 这里可写当前语言的特殊实现逻辑 -->
5771

72+
```python
73+
class Solution:
74+
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
75+
cnt = Counter(hand)
76+
for v in sorted(hand):
77+
if cnt[v]:
78+
for x in range(v, v + groupSize):
79+
if cnt[x] == 0:
80+
return False
81+
cnt[x] -= 1
82+
if cnt[x] == 0:
83+
cnt.pop(x)
84+
return True
85+
```
86+
5887
```python
5988
from sortedcontainers import SortedDict
6089

@@ -85,6 +114,32 @@ class Solution:
85114

86115
<!-- 这里可写当前语言的特殊实现逻辑 -->
87116

117+
```java
118+
class Solution {
119+
public boolean isNStraightHand(int[] hand, int groupSize) {
120+
Map<Integer, Integer> cnt = new HashMap<>();
121+
for (int v : hand) {
122+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
123+
}
124+
Arrays.sort(hand);
125+
for (int v : hand) {
126+
if (cnt.containsKey(v)) {
127+
for (int x = v; x < v + groupSize; ++x) {
128+
if (!cnt.containsKey(x)) {
129+
return false;
130+
}
131+
cnt.put(x, cnt.get(x) - 1);
132+
if (cnt.get(x) == 0) {
133+
cnt.remove(x);
134+
}
135+
}
136+
}
137+
}
138+
return true;
139+
}
140+
}
141+
```
142+
88143
```java
89144
class Solution {
90145
public boolean isNStraightHand(int[] hand, int groupSize) {
@@ -115,6 +170,30 @@ class Solution {
115170

116171
### **C++**
117172

173+
```cpp
174+
class Solution {
175+
public:
176+
bool isNStraightHand(vector<int>& hand, int groupSize) {
177+
unordered_map<int, int> cnt;
178+
for (int& v : hand) ++cnt[v];
179+
sort(hand.begin(), hand.end());
180+
for (int& v : hand) {
181+
if (cnt.count(v)) {
182+
for (int x = v; x < v + groupSize; ++x) {
183+
if (!cnt.count(x)) {
184+
return false;
185+
}
186+
if (--cnt[x] == 0) {
187+
cnt.erase(x);
188+
}
189+
}
190+
}
191+
}
192+
return true;
193+
}
194+
};
195+
```
196+
118197
```cpp
119198
class Solution {
120199
public:
@@ -139,6 +218,30 @@ public:
139218

140219
### **Go**
141220

221+
```go
222+
func isNStraightHand(hand []int, groupSize int) bool {
223+
cnt := map[int]int{}
224+
for _, v := range hand {
225+
cnt[v]++
226+
}
227+
sort.Ints(hand)
228+
for _, v := range hand {
229+
if _, ok := cnt[v]; ok {
230+
for x := v; x < v+groupSize; x++ {
231+
if _, ok := cnt[x]; !ok {
232+
return false
233+
}
234+
cnt[x]--
235+
if cnt[x] == 0 {
236+
delete(cnt, x)
237+
}
238+
}
239+
}
240+
}
241+
return true
242+
}
243+
```
244+
142245
```go
143246
func isNStraightHand(hand []int, groupSize int) bool {
144247
if len(hand)%groupSize != 0 {

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@
4444

4545
### **Python3**
4646

47+
```python
48+
class Solution:
49+
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
50+
cnt = Counter(hand)
51+
for v in sorted(hand):
52+
if cnt[v]:
53+
for x in range(v, v + groupSize):
54+
if cnt[x] == 0:
55+
return False
56+
cnt[x] -= 1
57+
if cnt[x] == 0:
58+
cnt.pop(x)
59+
return True
60+
```
61+
4762
```python
4863
from sortedcontainers import SortedDict
4964

@@ -72,6 +87,32 @@ class Solution:
7287

7388
### **Java**
7489

90+
```java
91+
class Solution {
92+
public boolean isNStraightHand(int[] hand, int groupSize) {
93+
Map<Integer, Integer> cnt = new HashMap<>();
94+
for (int v : hand) {
95+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
96+
}
97+
Arrays.sort(hand);
98+
for (int v : hand) {
99+
if (cnt.containsKey(v)) {
100+
for (int x = v; x < v + groupSize; ++x) {
101+
if (!cnt.containsKey(x)) {
102+
return false;
103+
}
104+
cnt.put(x, cnt.get(x) - 1);
105+
if (cnt.get(x) == 0) {
106+
cnt.remove(x);
107+
}
108+
}
109+
}
110+
}
111+
return true;
112+
}
113+
}
114+
```
115+
75116
```java
76117
class Solution {
77118
public boolean isNStraightHand(int[] hand, int groupSize) {
@@ -102,6 +143,30 @@ class Solution {
102143

103144
### **C++**
104145

146+
```cpp
147+
class Solution {
148+
public:
149+
bool isNStraightHand(vector<int>& hand, int groupSize) {
150+
unordered_map<int, int> cnt;
151+
for (int& v : hand) ++cnt[v];
152+
sort(hand.begin(), hand.end());
153+
for (int& v : hand) {
154+
if (cnt.count(v)) {
155+
for (int x = v; x < v + groupSize; ++x) {
156+
if (!cnt.count(x)) {
157+
return false;
158+
}
159+
if (--cnt[x] == 0) {
160+
cnt.erase(x);
161+
}
162+
}
163+
}
164+
}
165+
return true;
166+
}
167+
};
168+
```
169+
105170
```cpp
106171
class Solution {
107172
public:
@@ -126,6 +191,30 @@ public:
126191

127192
### **Go**
128193

194+
```go
195+
func isNStraightHand(hand []int, groupSize int) bool {
196+
cnt := map[int]int{}
197+
for _, v := range hand {
198+
cnt[v]++
199+
}
200+
sort.Ints(hand)
201+
for _, v := range hand {
202+
if _, ok := cnt[v]; ok {
203+
for x := v; x < v+groupSize; x++ {
204+
if _, ok := cnt[x]; !ok {
205+
return false
206+
}
207+
cnt[x]--
208+
if cnt[x] == 0 {
209+
delete(cnt, x)
210+
}
211+
}
212+
}
213+
}
214+
return true
215+
}
216+
```
217+
129218
```go
130219
func isNStraightHand(hand []int, groupSize int) bool {
131220
if len(hand)%groupSize != 0 {

solution/0800-0899/0846.Hand of Straights/Solution.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
class Solution {
22
public:
33
bool isNStraightHand(vector<int>& hand, int groupSize) {
4-
if (hand.size() % groupSize != 0) return false;
5-
map<int, int> mp;
6-
for (int& h : hand) mp[h] += 1;
7-
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;
4+
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)) {
11+
return false;
12+
}
13+
if (--cnt[x] == 0) {
14+
cnt.erase(x);
15+
}
16+
}
1517
}
1618
}
1719
return true;

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
func isNStraightHand(hand []int, groupSize int) bool {
2-
if len(hand)%groupSize != 0 {
3-
return false
2+
cnt := map[int]int{}
3+
for _, v := range hand {
4+
cnt[v]++
45
}
5-
m := treemap.NewWithIntComparator()
6-
for _, h := range hand {
7-
if v, ok := m.Get(h); ok {
8-
m.Put(h, v.(int)+1)
9-
} else {
10-
m.Put(h, 1)
11-
}
12-
}
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)
21-
} else {
22-
m.Put(i, v.(int)-1)
6+
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 {
11+
return false
12+
}
13+
cnt[x]--
14+
if cnt[x] == 0 {
15+
delete(cnt, x)
16+
}
2317
}
2418
}
2519
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
class Solution {
22
public boolean isNStraightHand(int[] hand, int groupSize) {
3-
if (hand.length % groupSize != 0) {
4-
return false;
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int v : hand) {
5+
cnt.put(v, cnt.getOrDefault(v, 0) + 1);
56
}
6-
TreeMap<Integer, Integer> tm = new TreeMap<>();
7-
for (int h : hand) {
8-
tm.put(h, tm.getOrDefault(h, 0) + 1);
9-
}
10-
while (!tm.isEmpty()) {
11-
int v = tm.firstKey();
12-
for (int i = v; i < v + groupSize; ++i) {
13-
if (!tm.containsKey(i)) {
14-
return false;
15-
}
16-
if (tm.get(i) == 1) {
17-
tm.remove(i);
18-
} else {
19-
tm.put(i, tm.get(i) - 1);
7+
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)) {
12+
return false;
13+
}
14+
cnt.put(x, cnt.get(x) - 1);
15+
if (cnt.get(x) == 0) {
16+
cnt.remove(x);
17+
}
2018
}
2119
}
2220
}
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
from sortedcontainers import SortedDict
2-
3-
41
class Solution:
52
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
6-
if len(hand) % groupSize != 0:
7-
return False
8-
sd = SortedDict()
9-
for h in hand:
10-
if h in sd:
11-
sd[h] += 1
12-
else:
13-
sd[h] = 1
14-
while sd:
15-
v = sd.peekitem(0)[0]
16-
for i in range(v, v + groupSize):
17-
if i not in sd:
18-
return False
19-
if sd[i] == 1:
20-
sd.pop(i)
21-
else:
22-
sd[i] -= 1
3+
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:
8+
return False
9+
cnt[x] -= 1
10+
if cnt[x] == 0:
11+
cnt.pop(x)
2312
return True

0 commit comments

Comments
 (0)