Skip to content

Commit 066b51b

Browse files
authored
Merge pull request #834 from 0xff-dev/1296
Add solution and test-cases for problem 1296
2 parents c6f5d34 + 0253ea2 commit 066b51b

File tree

3 files changed

+96
-24
lines changed

3 files changed

+96
-24
lines changed

leetcode/1201-1300/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [1296.Divide Array in Sets of K Consecutive Numbers][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array of integers `nums` and a positive integer `k`, check whether it is possible to divide this array into sets of `k` consecutive numbers.
5+
6+
Return `true` if it is possible. Otherwise, return `false`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [1,2,3,3,4,4,5,6], k = 4
12+
Output: true
13+
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Divide Array in Sets of K Consecutive Numbers
23-
```go
18+
```
19+
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
20+
Output: true
21+
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
2422
```
2523

24+
**Example 3:**
25+
26+
```
27+
Input: nums = [1,2,3,4], k = 3
28+
Output: false
29+
Explanation: Each array should be divided in subarrays of size 3.
30+
```
2631

2732
## 结语
2833

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int) bool {
6+
l := len(nums)
7+
if l%k != 0 {
8+
return false
9+
}
10+
if k == 1 {
11+
return true
12+
}
13+
14+
keys := make([]int, 0)
15+
keyCount := make(map[int]int)
16+
for _, n := range nums {
17+
keyCount[n]++
18+
if keyCount[n] == 1 {
19+
keys = append(keys, n)
20+
}
21+
}
22+
sort.Ints(keys)
23+
24+
idx := 1
25+
count := keyCount[keys[0]]
26+
nextIdx := -1
27+
keyCount[keys[0]] = 0
28+
used := 1
29+
l -= count
30+
31+
for idx < len(keys) {
32+
if keys[idx]-keys[idx-1] != 1 {
33+
return false
34+
}
35+
keyCount[keys[idx]] -= count
36+
l -= count
37+
used++
38+
if keyCount[keys[idx]] < 0 {
39+
return false
40+
}
41+
if keyCount[keys[idx]] > 0 && nextIdx == -1 {
42+
nextIdx = idx
43+
}
44+
45+
if used != k {
46+
idx++
47+
continue
48+
}
49+
if nextIdx == -1 {
50+
if idx == len(keys)-1 {
51+
break
52+
}
53+
idx++
54+
count = keyCount[keys[idx]]
55+
nextIdx = -1
56+
keyCount[keys[idx]] = 0
57+
l -= count
58+
used = 1
59+
idx++
60+
continue
61+
}
62+
63+
idx = nextIdx + 1
64+
count = keyCount[keys[nextIdx]]
65+
keyCount[keys[nextIdx]] = 0
66+
nextIdx = -1
67+
l -= count
68+
used = 1
69+
}
70+
return l == 0 && used == k
571
}

leetcode/1201-1300/1296.Divide-Array-in-Sets-of-K-Consecutive-Numbers/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
nums []int
14+
k int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 3, 3, 4, 4, 5, 6}, 4, true},
18+
{"TestCase2", []int{3, 2, 1, 2, 3, 4, 3, 4, 5, 9, 10, 11}, 3, true},
19+
{"TestCase3", []int{1, 2, 3, 4}, 3, false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)