From f546e8484b17811c241f6c89bf68e6ea606eb64d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 22 May 2025 08:45:00 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3362 No.3362.Zero Array Transformation III --- .../README.md | 142 +++++++++++++++++- .../README_EN.md | 140 ++++++++++++++++- .../Solution.cpp | 27 ++++ .../Solution.go | 43 ++++++ .../Solution.java | 24 +++ .../Solution.py | 17 +++ .../Solution.ts | 22 +++ 7 files changed, 408 insertions(+), 7 deletions(-) create mode 100644 solution/3300-3399/3362.Zero Array Transformation III/Solution.cpp create mode 100644 solution/3300-3399/3362.Zero Array Transformation III/Solution.go create mode 100644 solution/3300-3399/3362.Zero Array Transformation III/Solution.java create mode 100644 solution/3300-3399/3362.Zero Array Transformation III/Solution.py create mode 100644 solution/3300-3399/3362.Zero Array Transformation III/Solution.ts diff --git a/solution/3300-3399/3362.Zero Array Transformation III/README.md b/solution/3300-3399/3362.Zero Array Transformation III/README.md index 62decddccab01..303106e143a59 100644 --- a/solution/3300-3399/3362.Zero Array Transformation III/README.md +++ b/solution/3300-3399/3362.Zero Array Transformation III/README.md @@ -97,32 +97,166 @@ tags: -### 方法一 +### 方法一:贪心 + 差分数组 + 优先队列(大根堆) #### Python3 ```python - +class Solution: + def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int: + queries.sort() + pq = [] + d = [0] * (len(nums) + 1) + s = j = 0 + for i, x in enumerate(nums): + s += d[i] + while j < len(queries) and queries[j][0] <= i: + heappush(pq, -queries[j][1]) + j += 1 + while s < x and pq and -pq[0] >= i: + s += 1 + d[-heappop(pq) + 1] -= 1 + if s < x: + return -1 + return len(pq) ``` #### Java ```java - +class Solution { + public int maxRemoval(int[] nums, int[][] queries) { + Arrays.sort(queries, (a, b) -> Integer.compare(a[0], b[0])); + PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); + int n = nums.length; + int[] d = new int[n + 1]; + int s = 0, j = 0; + for (int i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.offer(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.peek() >= i) { + s++; + d[pq.poll() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxRemoval(vector& nums, vector>& queries) { + sort(queries.begin(), queries.end()); + priority_queue pq; + int n = nums.size(); + vector d(n + 1, 0); + int s = 0, j = 0; + for (int i = 0; i < n; ++i) { + s += d[i]; + while (j < queries.size() && queries[j][0] <= i) { + pq.push(queries[j][1]); + ++j; + } + while (s < nums[i] && !pq.empty() && pq.top() >= i) { + ++s; + int end = pq.top(); + pq.pop(); + --d[end + 1]; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +}; ``` #### Go ```go +func maxRemoval(nums []int, queries [][]int) int { + sort.Slice(queries, func(i, j int) bool { + return queries[i][0] < queries[j][0] + }) + + var h hp + heap.Init(&h) + + n := len(nums) + d := make([]int, n+1) + s, j := 0, 0 + + for i := 0; i < n; i++ { + s += d[i] + for j < len(queries) && queries[j][0] <= i { + heap.Push(&h, queries[j][1]) + j++ + } + for s < nums[i] && h.Len() > 0 && h.IntSlice[0] >= i { + s++ + end := heap.Pop(&h).(int) + if end+1 < len(d) { + d[end+1]-- + } + } + if s < nums[i] { + return -1 + } + } + + return h.Len() +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +``` +#### TypeScript + +```ts +function maxRemoval(nums: number[], queries: number[][]): number { + queries.sort((a, b) => a[0] - b[0]); + const pq = new MaxPriorityQueue(); + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + let [s, j] = [0, 0]; + for (let i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.enqueue(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.front() >= i) { + s++; + d[pq.dequeue() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); +} ``` diff --git a/solution/3300-3399/3362.Zero Array Transformation III/README_EN.md b/solution/3300-3399/3362.Zero Array Transformation III/README_EN.md index a5fbf21a2481a..b3b77c4b4fb37 100644 --- a/solution/3300-3399/3362.Zero Array Transformation III/README_EN.md +++ b/solution/3300-3399/3362.Zero Array Transformation III/README_EN.md @@ -101,25 +101,159 @@ tags: #### Python3 ```python - +class Solution: + def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int: + queries.sort() + pq = [] + d = [0] * (len(nums) + 1) + s = j = 0 + for i, x in enumerate(nums): + s += d[i] + while j < len(queries) and queries[j][0] <= i: + heappush(pq, -queries[j][1]) + j += 1 + while s < x and pq and -pq[0] >= i: + s += 1 + d[-heappop(pq) + 1] -= 1 + if s < x: + return -1 + return len(pq) ``` #### Java ```java - +class Solution { + public int maxRemoval(int[] nums, int[][] queries) { + Arrays.sort(queries, (a, b) -> Integer.compare(a[0], b[0])); + PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); + int n = nums.length; + int[] d = new int[n + 1]; + int s = 0, j = 0; + for (int i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.offer(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.peek() >= i) { + s++; + d[pq.poll() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxRemoval(vector& nums, vector>& queries) { + sort(queries.begin(), queries.end()); + priority_queue pq; + int n = nums.size(); + vector d(n + 1, 0); + int s = 0, j = 0; + for (int i = 0; i < n; ++i) { + s += d[i]; + while (j < queries.size() && queries[j][0] <= i) { + pq.push(queries[j][1]); + ++j; + } + while (s < nums[i] && !pq.empty() && pq.top() >= i) { + ++s; + int end = pq.top(); + pq.pop(); + --d[end + 1]; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +}; ``` #### Go ```go +func maxRemoval(nums []int, queries [][]int) int { + sort.Slice(queries, func(i, j int) bool { + return queries[i][0] < queries[j][0] + }) + + var h hp + heap.Init(&h) + + n := len(nums) + d := make([]int, n+1) + s, j := 0, 0 + + for i := 0; i < n; i++ { + s += d[i] + for j < len(queries) && queries[j][0] <= i { + heap.Push(&h, queries[j][1]) + j++ + } + for s < nums[i] && h.Len() > 0 && h.IntSlice[0] >= i { + s++ + end := heap.Pop(&h).(int) + if end+1 < len(d) { + d[end+1]-- + } + } + if s < nums[i] { + return -1 + } + } + + return h.Len() +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} +``` +#### TypeScript + +```ts +function maxRemoval(nums: number[], queries: number[][]): number { + queries.sort((a, b) => a[0] - b[0]); + const pq = new MaxPriorityQueue(); + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + let [s, j] = [0, 0]; + for (let i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.enqueue(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.front() >= i) { + s++; + d[pq.dequeue() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); +} ``` diff --git a/solution/3300-3399/3362.Zero Array Transformation III/Solution.cpp b/solution/3300-3399/3362.Zero Array Transformation III/Solution.cpp new file mode 100644 index 0000000000000..5f2ba9d8f6879 --- /dev/null +++ b/solution/3300-3399/3362.Zero Array Transformation III/Solution.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int maxRemoval(vector& nums, vector>& queries) { + sort(queries.begin(), queries.end()); + priority_queue pq; + int n = nums.size(); + vector d(n + 1, 0); + int s = 0, j = 0; + for (int i = 0; i < n; ++i) { + s += d[i]; + while (j < queries.size() && queries[j][0] <= i) { + pq.push(queries[j][1]); + ++j; + } + while (s < nums[i] && !pq.empty() && pq.top() >= i) { + ++s; + int end = pq.top(); + pq.pop(); + --d[end + 1]; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +}; diff --git a/solution/3300-3399/3362.Zero Array Transformation III/Solution.go b/solution/3300-3399/3362.Zero Array Transformation III/Solution.go new file mode 100644 index 0000000000000..fad7bfad5b00e --- /dev/null +++ b/solution/3300-3399/3362.Zero Array Transformation III/Solution.go @@ -0,0 +1,43 @@ +func maxRemoval(nums []int, queries [][]int) int { + sort.Slice(queries, func(i, j int) bool { + return queries[i][0] < queries[j][0] + }) + + var h hp + heap.Init(&h) + + n := len(nums) + d := make([]int, n+1) + s, j := 0, 0 + + for i := 0; i < n; i++ { + s += d[i] + for j < len(queries) && queries[j][0] <= i { + heap.Push(&h, queries[j][1]) + j++ + } + for s < nums[i] && h.Len() > 0 && h.IntSlice[0] >= i { + s++ + end := heap.Pop(&h).(int) + if end+1 < len(d) { + d[end+1]-- + } + } + if s < nums[i] { + return -1 + } + } + + return h.Len() +} + +type hp struct{ sort.IntSlice } + +func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } +func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } +func (h *hp) Pop() any { + a := h.IntSlice + v := a[len(a)-1] + h.IntSlice = a[:len(a)-1] + return v +} diff --git a/solution/3300-3399/3362.Zero Array Transformation III/Solution.java b/solution/3300-3399/3362.Zero Array Transformation III/Solution.java new file mode 100644 index 0000000000000..22d7868855f56 --- /dev/null +++ b/solution/3300-3399/3362.Zero Array Transformation III/Solution.java @@ -0,0 +1,24 @@ +class Solution { + public int maxRemoval(int[] nums, int[][] queries) { + Arrays.sort(queries, (a, b) -> Integer.compare(a[0], b[0])); + PriorityQueue pq = new PriorityQueue<>((a, b) -> b - a); + int n = nums.length; + int[] d = new int[n + 1]; + int s = 0, j = 0; + for (int i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.offer(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.peek() >= i) { + s++; + d[pq.poll() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); + } +} diff --git a/solution/3300-3399/3362.Zero Array Transformation III/Solution.py b/solution/3300-3399/3362.Zero Array Transformation III/Solution.py new file mode 100644 index 0000000000000..33e462bfc6bb5 --- /dev/null +++ b/solution/3300-3399/3362.Zero Array Transformation III/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def maxRemoval(self, nums: List[int], queries: List[List[int]]) -> int: + queries.sort() + pq = [] + d = [0] * (len(nums) + 1) + s = j = 0 + for i, x in enumerate(nums): + s += d[i] + while j < len(queries) and queries[j][0] <= i: + heappush(pq, -queries[j][1]) + j += 1 + while s < x and pq and -pq[0] >= i: + s += 1 + d[-heappop(pq) + 1] -= 1 + if s < x: + return -1 + return len(pq) diff --git a/solution/3300-3399/3362.Zero Array Transformation III/Solution.ts b/solution/3300-3399/3362.Zero Array Transformation III/Solution.ts new file mode 100644 index 0000000000000..25be843e8adef --- /dev/null +++ b/solution/3300-3399/3362.Zero Array Transformation III/Solution.ts @@ -0,0 +1,22 @@ +function maxRemoval(nums: number[], queries: number[][]): number { + queries.sort((a, b) => a[0] - b[0]); + const pq = new MaxPriorityQueue(); + const n = nums.length; + const d: number[] = Array(n + 1).fill(0); + let [s, j] = [0, 0]; + for (let i = 0; i < n; i++) { + s += d[i]; + while (j < queries.length && queries[j][0] <= i) { + pq.enqueue(queries[j][1]); + j++; + } + while (s < nums[i] && !pq.isEmpty() && pq.front() >= i) { + s++; + d[pq.dequeue() + 1]--; + } + if (s < nums[i]) { + return -1; + } + } + return pq.size(); +}