diff --git a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/README.md b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/README.md index fb559540..d0fd88c2 100755 --- a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/README.md +++ b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/README.md @@ -1,28 +1,55 @@ # [3362.Zero Array Transformation III][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` of length `n` and a 2D array `queries` where `queries[i] = [li, ri]`. + +Each `queries[i]` represents the following action on `nums`: + +- Decrement the value at each index in the range `[li, ri]` in nums by **at most** `1`. +- The amount by which the value is decremented can be chosen **independently** for each index. + +A **Zero Array** is an array with all its elements equal to 0. + +Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the remaining queries. If it is not possible to convert `nums` to a **zero array**, return -1. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,0,2], queries = [[0,2],[0,2],[1,1]] + +Output: 1 + +Explanation: + +After removing queries[2], nums can still be converted to a zero array. + +Using queries[0], decrement nums[0] and nums[2] by 1 and nums[1] by 0. +Using queries[1], decrement nums[0] and nums[2] by 1 and nums[1] by 0. +``` + +**Example 2:** + ``` +Input: nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]] -## 题意 -> ... +Output: 2 -## 题解 +Explanation: -### 思路1 -> ... -Zero Array Transformation III -```go +We can remove queries[2] and queries[3]. ``` +**Example 3:** + +``` +Input: nums = [1,2,3,4], queries = [[0,3]] + +Output: -1 + +Explanation: + +nums cannot be converted to a zero array even after using all the queries. +``` ## 结语 diff --git a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution.go b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution.go index d115ccf5..922013be 100644 --- a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution.go +++ b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution.go @@ -1,5 +1,58 @@ package Solution -func Solution(x bool) bool { +import ( + "container/heap" + "sort" +) + +func Solution(nums []int, queries [][]int) int { + sort.Slice(queries, func(i, j int) bool { + return queries[i][0] < queries[j][0] + }) + pq := &Heap{} + heap.Init(pq) + deltaArray := make([]int, len(nums)+1) + operations := 0 + + for i, j := 0, 0; i < len(nums); i++ { + operations += deltaArray[i] + for j < len(queries) && queries[j][0] == i { + heap.Push(pq, queries[j][1]) + j++ + } + for operations < nums[i] && pq.Len() > 0 && (*pq)[0] >= i { + operations += 1 + deltaArray[heap.Pop(pq).(int)+1] -= 1 + } + if operations < nums[i] { + return -1 + } + } + return pq.Len() +} + +type Heap []int + +func (h Heap) Len() int { + return len(h) +} + +func (h Heap) Less(i, j int) bool { + return h[i] > h[j] +} + +func (h Heap) Swap(i, j int) { + h[i], h[j] = h[j], h[i] +} + +func (h *Heap) Push(x interface{}) { + *h = append(*h, x.(int)) +} + +func (h *Heap) Pop() interface{} { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] return x } diff --git a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution_test.go b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution_test.go index 14ff50eb..ee6a24e2 100644 --- a/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution_test.go +++ b/leetcode/3301-3400/3362.Zero-Array-Transformation-III/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + nums []int + queries [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 0, 2}, [][]int{{0, 2}, {0, 2}, {1, 1}}, 1}, + {"TestCase2", []int{1, 1, 1, 1}, [][]int{{1, 3}, {0, 2}, {1, 3}, {1, 2}}, 2}, + {"TestCase3", []int{1, 2, 3, 4}, [][]int{{0, 3}}, -1}, } // 开始测试 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.queries) 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", + c.expect, got, c.nums, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }