diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md index 43c01e207b7c6..e1e44eb2e0170 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README.md @@ -206,14 +206,14 @@ func (h *hp) Pop() any { ```ts function minRefuelStops(target: number, startFuel: number, stations: number[][]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let [ans, pre] = [0, 0]; stations.push([target, 0]); for (const [pos, fuel] of stations) { const dist = pos - pre; startFuel -= dist; while (startFuel < 0 && !pq.isEmpty()) { - startFuel += pq.dequeue().element; + startFuel += pq.dequeue(); ans++; } if (startFuel < 0) { diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md index 630e93b48caff..178656648518c 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/README_EN.md @@ -203,14 +203,14 @@ func (h *hp) Pop() any { ```ts function minRefuelStops(target: number, startFuel: number, stations: number[][]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let [ans, pre] = [0, 0]; stations.push([target, 0]); for (const [pos, fuel] of stations) { const dist = pos - pre; startFuel -= dist; while (startFuel < 0 && !pq.isEmpty()) { - startFuel += pq.dequeue().element; + startFuel += pq.dequeue(); ans++; } if (startFuel < 0) { diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.ts b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.ts index e4a782ed55c25..0ecdf32837785 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.ts +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.ts @@ -1,12 +1,12 @@ function minRefuelStops(target: number, startFuel: number, stations: number[][]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let [ans, pre] = [0, 0]; stations.push([target, 0]); for (const [pos, fuel] of stations) { const dist = pos - pre; startFuel -= dist; while (startFuel < 0 && !pq.isEmpty()) { - startFuel += pq.dequeue().element; + startFuel += pq.dequeue(); ans++; } if (startFuel < 0) { diff --git a/solution/1000-1099/1046.Last Stone Weight/README.md b/solution/1000-1099/1046.Last Stone Weight/README.md index 805d5787cb803..987404398de4f 100644 --- a/solution/1000-1099/1046.Last Stone Weight/README.md +++ b/solution/1000-1099/1046.Last Stone Weight/README.md @@ -163,18 +163,18 @@ func (h *hp) pop() int { return heap.Pop(h).(int) } ```ts function lastStoneWeight(stones: number[]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); for (const x of stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue().element; - const x = pq.dequeue().element; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x !== y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue().element; + return pq.isEmpty() ? 0 : pq.dequeue(); } ``` @@ -191,13 +191,13 @@ var lastStoneWeight = function (stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue()['priority']; - const x = pq.dequeue()['priority']; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x != y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue()['priority']; + return pq.isEmpty() ? 0 : pq.dequeue(); }; ``` diff --git a/solution/1000-1099/1046.Last Stone Weight/README_EN.md b/solution/1000-1099/1046.Last Stone Weight/README_EN.md index 4cfa4d01b3945..4c1f07348f415 100644 --- a/solution/1000-1099/1046.Last Stone Weight/README_EN.md +++ b/solution/1000-1099/1046.Last Stone Weight/README_EN.md @@ -165,18 +165,18 @@ func (h *hp) pop() int { return heap.Pop(h).(int) } ```ts function lastStoneWeight(stones: number[]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); for (const x of stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue().element; - const x = pq.dequeue().element; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x !== y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue().element; + return pq.isEmpty() ? 0 : pq.dequeue(); } ``` @@ -193,13 +193,13 @@ var lastStoneWeight = function (stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue()['priority']; - const x = pq.dequeue()['priority']; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x != y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue()['priority']; + return pq.isEmpty() ? 0 : pq.dequeue(); }; ``` diff --git a/solution/1000-1099/1046.Last Stone Weight/Solution.js b/solution/1000-1099/1046.Last Stone Weight/Solution.js index 2452c8e41cee7..f015b347d6ce9 100644 --- a/solution/1000-1099/1046.Last Stone Weight/Solution.js +++ b/solution/1000-1099/1046.Last Stone Weight/Solution.js @@ -8,11 +8,11 @@ var lastStoneWeight = function (stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue()['priority']; - const x = pq.dequeue()['priority']; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x != y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue()['priority']; + return pq.isEmpty() ? 0 : pq.dequeue(); }; diff --git a/solution/1000-1099/1046.Last Stone Weight/Solution.ts b/solution/1000-1099/1046.Last Stone Weight/Solution.ts index 4224189ffb4a8..07ed9d5d66241 100644 --- a/solution/1000-1099/1046.Last Stone Weight/Solution.ts +++ b/solution/1000-1099/1046.Last Stone Weight/Solution.ts @@ -1,14 +1,14 @@ function lastStoneWeight(stones: number[]): number { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); for (const x of stones) { pq.enqueue(x); } while (pq.size() > 1) { - const y = pq.dequeue().element; - const x = pq.dequeue().element; + const y = pq.dequeue(); + const x = pq.dequeue(); if (x !== y) { pq.enqueue(y - x); } } - return pq.isEmpty() ? 0 : pq.dequeue().element; + return pq.isEmpty() ? 0 : pq.dequeue(); } diff --git a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README.md b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README.md index 67cd7338f7ab9..92d70c41f8ff3 100644 --- a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README.md +++ b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README.md @@ -198,14 +198,14 @@ func (hp) Push(any) {} ```ts function isPossible(target: number[]): boolean { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let s = 0; for (const x of target) { s += x; pq.enqueue(x); } - while (pq.front().element > 1) { - const mx = pq.dequeue().element; + while (pq.front() > 1) { + const mx = pq.dequeue(); const t = s - mx; if (t < 1 || mx - t < 1) { return false; diff --git a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README_EN.md b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README_EN.md index 46c3cefe4351d..1add677cba9f6 100644 --- a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README_EN.md +++ b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/README_EN.md @@ -199,14 +199,14 @@ func (hp) Push(any) {} ```ts function isPossible(target: number[]): boolean { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let s = 0; for (const x of target) { s += x; pq.enqueue(x); } - while (pq.front().element > 1) { - const mx = pq.dequeue().element; + while (pq.front() > 1) { + const mx = pq.dequeue(); const t = s - mx; if (t < 1 || mx - t < 1) { return false; diff --git a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/Solution.ts b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/Solution.ts index 144be716e511b..30f3e9d27db06 100644 --- a/solution/1300-1399/1354.Construct Target Array With Multiple Sums/Solution.ts +++ b/solution/1300-1399/1354.Construct Target Array With Multiple Sums/Solution.ts @@ -1,12 +1,12 @@ function isPossible(target: number[]): boolean { - const pq = new MaxPriorityQueue(); + const pq = new MaxPriorityQueue(); let s = 0; for (const x of target) { s += x; pq.enqueue(x); } - while (pq.front().element > 1) { - const mx = pq.dequeue().element; + while (pq.front() > 1) { + const mx = pq.dequeue(); const t = s - mx; if (t < 1 || mx - t < 1) { return false; diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md index fc6d85ba96c64..f3b21d53007b9 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README.md @@ -264,25 +264,25 @@ function minimumDifference(nums: number[]): number { const n = Math.floor(m / 3); let s = 0; const pre: number[] = Array(m + 1); - const q1 = new MaxPriorityQueue(); + const q1 = new MaxPriorityQueue(); for (let i = 1; i <= n * 2; ++i) { const x = nums[i - 1]; s += x; - q1.enqueue(x, x); + q1.enqueue(x); if (q1.size() > n) { - s -= q1.dequeue().element; + s -= q1.dequeue(); } pre[i] = s; } s = 0; const suf: number[] = Array(m + 1); - const q2 = new MinPriorityQueue(); + const q2 = new MinPriorityQueue(); for (let i = m; i > n; --i) { const x = nums[i - 1]; s += x; - q2.enqueue(x, x); + q2.enqueue(x); if (q2.size() > n) { - s -= q2.dequeue().element; + s -= q2.dequeue(); } suf[i] = s; } @@ -294,6 +294,58 @@ function minimumDifference(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::BinaryHeap; +use std::cmp::Reverse; + +impl Solution { + pub fn minimum_difference(nums: Vec) -> i64 { + let m = nums.len(); + let n = m / 3; + let mut s = 0i64; + let mut pre = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); // max-heap + + for i in 1..=2 * n { + let x = nums[i - 1] as i64; + s += x; + pq.push(x); + if pq.len() > n { + if let Some(top) = pq.pop() { + s -= top; + } + } + pre[i] = s; + } + + s = 0; + let mut suf = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); + + for i in (n + 1..=m).rev() { + let x = nums[i - 1] as i64; + s += x; + pq.push(Reverse(x)); + if pq.len() > n { + if let Some(Reverse(top)) = pq.pop() { + s -= top; + } + } + suf[i] = s; + } + + let mut ans = i64::MAX; + for i in n..=2 * n { + ans = ans.min(pre[i] - suf[i + 1]); + } + + ans + } +} +``` + diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md index a3219e509b366..141608a7e8fcc 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md @@ -264,25 +264,25 @@ function minimumDifference(nums: number[]): number { const n = Math.floor(m / 3); let s = 0; const pre: number[] = Array(m + 1); - const q1 = new MaxPriorityQueue(); + const q1 = new MaxPriorityQueue(); for (let i = 1; i <= n * 2; ++i) { const x = nums[i - 1]; s += x; - q1.enqueue(x, x); + q1.enqueue(x); if (q1.size() > n) { - s -= q1.dequeue().element; + s -= q1.dequeue(); } pre[i] = s; } s = 0; const suf: number[] = Array(m + 1); - const q2 = new MinPriorityQueue(); + const q2 = new MinPriorityQueue(); for (let i = m; i > n; --i) { const x = nums[i - 1]; s += x; - q2.enqueue(x, x); + q2.enqueue(x); if (q2.size() > n) { - s -= q2.dequeue().element; + s -= q2.dequeue(); } suf[i] = s; } @@ -294,6 +294,58 @@ function minimumDifference(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::BinaryHeap; +use std::cmp::Reverse; + +impl Solution { + pub fn minimum_difference(nums: Vec) -> i64 { + let m = nums.len(); + let n = m / 3; + let mut s = 0i64; + let mut pre = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); // max-heap + + for i in 1..=2 * n { + let x = nums[i - 1] as i64; + s += x; + pq.push(x); + if pq.len() > n { + if let Some(top) = pq.pop() { + s -= top; + } + } + pre[i] = s; + } + + s = 0; + let mut suf = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); + + for i in (n + 1..=m).rev() { + let x = nums[i - 1] as i64; + s += x; + pq.push(Reverse(x)); + if pq.len() > n { + if let Some(Reverse(top)) = pq.pop() { + s -= top; + } + } + suf[i] = s; + } + + let mut ans = i64::MAX; + for i in n..=2 * n { + ans = ans.min(pre[i] - suf[i + 1]); + } + + ans + } +} +``` + diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.rs b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.rs new file mode 100644 index 0000000000000..c0081d3a69131 --- /dev/null +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.rs @@ -0,0 +1,47 @@ +use std::cmp::Reverse; +use std::collections::BinaryHeap; + +impl Solution { + pub fn minimum_difference(nums: Vec) -> i64 { + let m = nums.len(); + let n = m / 3; + let mut s = 0i64; + let mut pre = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); // max-heap + + for i in 1..=2 * n { + let x = nums[i - 1] as i64; + s += x; + pq.push(x); + if pq.len() > n { + if let Some(top) = pq.pop() { + s -= top; + } + } + pre[i] = s; + } + + s = 0; + let mut suf = vec![0i64; m + 1]; + let mut pq = BinaryHeap::new(); + + for i in (n + 1..=m).rev() { + let x = nums[i - 1] as i64; + s += x; + pq.push(Reverse(x)); + if pq.len() > n { + if let Some(Reverse(top)) = pq.pop() { + s -= top; + } + } + suf[i] = s; + } + + let mut ans = i64::MAX; + for i in n..=2 * n { + ans = ans.min(pre[i] - suf[i + 1]); + } + + ans + } +} diff --git a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.ts b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.ts index 2ae41810bf305..d407e351f40a5 100644 --- a/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.ts +++ b/solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/Solution.ts @@ -3,25 +3,25 @@ function minimumDifference(nums: number[]): number { const n = Math.floor(m / 3); let s = 0; const pre: number[] = Array(m + 1); - const q1 = new MaxPriorityQueue(); + const q1 = new MaxPriorityQueue(); for (let i = 1; i <= n * 2; ++i) { const x = nums[i - 1]; s += x; - q1.enqueue(x, x); + q1.enqueue(x); if (q1.size() > n) { - s -= q1.dequeue().element; + s -= q1.dequeue(); } pre[i] = s; } s = 0; const suf: number[] = Array(m + 1); - const q2 = new MinPriorityQueue(); + const q2 = new MinPriorityQueue(); for (let i = m; i > n; --i) { const x = nums[i - 1]; s += x; - q2.enqueue(x, x); + q2.enqueue(x); if (q2.size() > n) { - s -= q2.dequeue().element; + s -= q2.dequeue(); } suf[i] = s; }