diff --git a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/README.md b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/README.md index 49a4f32f6..aae2aed50 100755 --- a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/README.md +++ b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/README.md @@ -1,28 +1,46 @@ # [3264.Final Array State After K Multiplication Operations I][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given an integer array `nums`, an integer `k`, and an integer `multiplier`. + +You need to perform `k` operations on `nums`. In each operation: + +- Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**. +- Replace the selected minimum value `x` with `x * multiplier`. + +Return an integer array denoting the final state of `nums` after performing all `k` operations. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [2,1,3,5,6], k = 5, multiplier = 2 -## 题意 -> ... +Output: [8,4,6,5,6] -## 题解 +Explanation: -### 思路1 -> ... -Final Array State After K Multiplication Operations I -```go +Operation Result +After operation 1 [2, 2, 3, 5, 6] +After operation 2 [4, 2, 3, 5, 6] +After operation 3 [4, 4, 3, 5, 6] +After operation 4 [4, 4, 6, 5, 6] +After operation 5 [8, 4, 6, 5, 6] ``` +**Example 2:** + +``` +Input: nums = [1,2], k = 3, multiplier = 4 + +Output: [16,8] + +Explanation: + +Operation Result +After operation 1 [4, 2] +After operation 2 [4, 8] +After operation 3 [16, 8] +``` ## 结语 diff --git a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution.go b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution.go index d115ccf5e..f62ca7eea 100644 --- a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution.go +++ b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution.go @@ -1,5 +1,50 @@ package Solution -func Solution(x bool) bool { +import "container/heap" + +type heap3264 [][2]int + +func (h *heap3264) Len() int { + return len(*h) +} + +func (h *heap3264) Swap(i, j int) { + (*h)[i], (*h)[j] = (*h)[j], (*h)[i] +} + +func (h *heap3264) Less(i, j int) bool { + a, b := (*h)[i], (*h)[j] + if a[1] == b[1] { + return a[0] < b[0] + } + return a[1] < b[1] +} + +func (h *heap3264) Push(x any) { + *h = append(*h, x.([2]int)) +} + +func (h *heap3264) Pop() any { + old := *h + l := len(old) + x := old[l-1] + *h = old[:l-1] return x } + +func Solution(nums []int, k int, multiplier int) []int { + h := &heap3264{} + for i := range nums { + heap.Push(h, [2]int{i, nums[i]}) + } + for ; k > 0; k-- { + top := heap.Pop(h).([2]int) + top[1] *= multiplier + heap.Push(h, top) + } + for h.Len() > 0 { + top := heap.Pop(h).([2]int) + nums[top[0]] = top[1] + } + return nums +} diff --git a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution_test.go b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution_test.go index 14ff50eb4..1d68a35d2 100644 --- a/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution_test.go +++ b/leetcode/3201-3300/3264.Final-Array-State-After-K-Multiplication-Operations-I/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums []int + k, multiplier int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 1, 3, 5, 6}, 5, 2, []int{8, 4, 6, 5, 6}}, + {"TestCase2", []int{1, 2}, 3, 4, []int{16, 8}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k, c.multiplier) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.nums, c.k, c.multiplier) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }