Skip to content

Commit f58edb8

Browse files
committed
Add solution and test-cases for problem 3264
1 parent dbd4dda commit f58edb8

File tree

3 files changed

+88
-25
lines changed

3 files changed

+88
-25
lines changed

leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [3264.Final Array State After K Multiplication Operations I][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+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
5+
6+
You need to perform `k` operations on `nums`. In each operation:
7+
8+
- Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
9+
- Replace the selected minimum value `x` with `x * multiplier`.
10+
11+
Return an integer array denoting the final state of `nums` after performing all `k` operations.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
16+
Input: nums = [2,1,3,5,6], k = 5, multiplier = 2
1417
15-
## 题意
16-
> ...
18+
Output: [8,4,6,5,6]
1719
18-
## 题解
20+
Explanation:
1921
20-
### 思路1
21-
> ...
22-
Final Array State After K Multiplication Operations I
23-
```go
22+
Operation Result
23+
After operation 1 [2, 2, 3, 5, 6]
24+
After operation 2 [4, 2, 3, 5, 6]
25+
After operation 3 [4, 4, 3, 5, 6]
26+
After operation 4 [4, 4, 6, 5, 6]
27+
After operation 5 [8, 4, 6, 5, 6]
2428
```
2529

30+
**Example 2:**
31+
32+
```
33+
Input: nums = [1,2], k = 3, multiplier = 4
34+
35+
Output: [16,8]
36+
37+
Explanation:
38+
39+
Operation Result
40+
After operation 1 [4, 2]
41+
After operation 2 [4, 8]
42+
After operation 3 [16, 8]
43+
```
2644

2745
## 结语
2846

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package Solution
22

3-
func Solution(x bool) bool {
3+
import "container/heap"
4+
5+
type heap3264 [][2]int
6+
7+
func (h *heap3264) Len() int {
8+
return len(*h)
9+
}
10+
11+
func (h *heap3264) Swap(i, j int) {
12+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
13+
}
14+
15+
func (h *heap3264) Less(i, j int) bool {
16+
a, b := (*h)[i], (*h)[j]
17+
if a[1] == b[1] {
18+
return a[0] < b[0]
19+
}
20+
return a[1] < b[1]
21+
}
22+
23+
func (h *heap3264) Push(x any) {
24+
*h = append(*h, x.([2]int))
25+
}
26+
27+
func (h *heap3264) Pop() any {
28+
old := *h
29+
l := len(old)
30+
x := old[l-1]
31+
*h = old[:l-1]
432
return x
533
}
34+
35+
func Solution(nums []int, k int, multiplier int) []int {
36+
h := &heap3264{}
37+
for i := range nums {
38+
heap.Push(h, [2]int{i, nums[i]})
39+
}
40+
for ; k > 0; k-- {
41+
top := heap.Pop(h).([2]int)
42+
top[1] *= multiplier
43+
heap.Push(h, top)
44+
}
45+
for h.Len() > 0 {
46+
top := heap.Pop(h).([2]int)
47+
nums[top[0]] = top[1]
48+
}
49+
return nums
50+
}

leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution_test.go

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

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k, c.multiplier)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.nums, c.k, c.multiplier)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)