Skip to content

Commit b4dac32

Browse files
authored
feat: add typescript and rust solutions to lc problem: No.2530 (#1836)
No.2530.Maximal Score After Applying K Operations
1 parent 49ee3ff commit b4dac32

File tree

5 files changed

+129
-14
lines changed

5 files changed

+129
-14
lines changed

solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959

6060
要使得分数最大化,我们需要在每一步操作中,选择元素值最大的元素进行操作。因此,我们可以使用优先队列(大根堆)来维护当前元素值最大的元素。
6161

62-
每次从优先队列中取出元素值最大的元素,将其分数加入答案,然后将其替换为 `ceil(nums[i] / 3)`,并将新的元素值加入优先队列。循环 $k$ 次后,将答案返回即可。
62+
每次从优先队列中取出元素值最大的元素 $v$,将答案加上 $v$,并将 $v$ 替换为 $\lceil \frac{v}{3} \rceil$,加入优先队列。重复 $k$ 次后,将答案返回即可。
6363

64-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
64+
时间复杂度 $O(n + k \times \log n)$,空间复杂度 $O(n)$ 或 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。
6565

6666
<!-- tabs:start -->
6767

@@ -169,9 +169,9 @@ func maxKelements(nums []int, k int) (ans int64) {
169169

170170
type hp struct{ sort.IntSlice }
171171

172-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
173-
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
174-
func (h *hp) Pop() interface{} {
172+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
173+
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
174+
func (h *hp) Pop() any {
175175
a := h.IntSlice
176176
v := a[len(a)-1]
177177
h.IntSlice = a[:len(a)-1]
@@ -196,8 +196,47 @@ func maxKelements(nums []int, k int) (ans int64) {
196196
type hp struct{ sort.IntSlice }
197197

198198
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
199-
func (hp) Push(interface{}) {}
200-
func (hp) Pop() (_ interface{}) { return }
199+
func (hp) Push(any) {}
200+
func (hp) Pop() (_ any) { return }
201+
```
202+
203+
### **Rust**
204+
205+
```rust
206+
use std::collections::BinaryHeap;
207+
208+
impl Solution {
209+
pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {
210+
let mut pq = BinaryHeap::from(nums);
211+
let mut ans = 0;
212+
let mut k = k;
213+
while k > 0 {
214+
if let Some(v) = pq.pop() {
215+
ans += v as i64;
216+
pq.push((v + 2) / 3);
217+
k -= 1;
218+
}
219+
}
220+
ans
221+
}
222+
}
223+
```
224+
225+
### **TypeScript**
226+
227+
```ts
228+
function maxKelements(nums: number[], k: number): number {
229+
const pq = new MaxPriorityQueue();
230+
nums.forEach(num => pq.enqueue(num));
231+
let ans = 0;
232+
while (k > 0) {
233+
const v = pq.dequeue()!.element;
234+
ans += v;
235+
pq.enqueue(Math.floor((v + 2) / 3));
236+
k--;
237+
}
238+
return ans;
239+
}
201240
```
202241

203242
### **...**

solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md

+52-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ The final score is 10 + 4 + 3 = 17.
4949

5050
## Solutions
5151

52+
**Solution 1: Priority Queue (Max Heap)**
53+
54+
To maximize the sum of scores, we need to select the element with the maximum value at each step. Therefore, we can use a priority queue (max heap) to maintain the element with the maximum value.
55+
56+
At each step, we take out the element with the maximum value $v$ from the priority queue, add $v$ to the answer, and replace $v$ with $\lceil \frac{v}{3} \rceil$, and then add it to the priority queue. After repeating this process $k$ times, we return the answer.
57+
58+
The time complexity is $O(n + k \times \log n)$, and the space complexity is $O(n)$ or $O(1)$. Here, $n$ is the length of the array $nums$.
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**
@@ -151,9 +159,9 @@ func maxKelements(nums []int, k int) (ans int64) {
151159

152160
type hp struct{ sort.IntSlice }
153161

154-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
155-
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
156-
func (h *hp) Pop() interface{} {
162+
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
163+
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
164+
func (h *hp) Pop() any {
157165
a := h.IntSlice
158166
v := a[len(a)-1]
159167
h.IntSlice = a[:len(a)-1]
@@ -178,8 +186,47 @@ func maxKelements(nums []int, k int) (ans int64) {
178186
type hp struct{ sort.IntSlice }
179187

180188
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
181-
func (hp) Push(interface{}) {}
182-
func (hp) Pop() (_ interface{}) { return }
189+
func (hp) Push(any) {}
190+
func (hp) Pop() (_ any) { return }
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
use std::collections::BinaryHeap;
197+
198+
impl Solution {
199+
pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {
200+
let mut pq = BinaryHeap::from(nums);
201+
let mut ans = 0;
202+
let mut k = k;
203+
while k > 0 {
204+
if let Some(v) = pq.pop() {
205+
ans += v as i64;
206+
pq.push((v + 2) / 3);
207+
k -= 1;
208+
}
209+
}
210+
ans
211+
}
212+
}
213+
```
214+
215+
### **TypeScript**
216+
217+
```ts
218+
function maxKelements(nums: number[], k: number): number {
219+
const pq = new MaxPriorityQueue();
220+
nums.forEach(num => pq.enqueue(num));
221+
let ans = 0;
222+
while (k > 0) {
223+
const v = pq.dequeue()!.element;
224+
ans += v;
225+
pq.enqueue(Math.floor((v + 2) / 3));
226+
k--;
227+
}
228+
return ans;
229+
}
183230
```
184231

185232
### **...**

solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ func maxKelements(nums []int, k int) (ans int64) {
1212
type hp struct{ sort.IntSlice }
1313

1414
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
15-
func (hp) Push(interface{}) {}
16-
func (hp) Pop() (_ interface{}) { return }
15+
func (hp) Push(any) {}
16+
func (hp) Pop() (_ any) { return }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::collections::BinaryHeap;
2+
3+
impl Solution {
4+
pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {
5+
let mut pq = BinaryHeap::from(nums);
6+
let mut ans = 0;
7+
let mut k = k;
8+
while k > 0 {
9+
if let Some(v) = pq.pop() {
10+
ans += v as i64;
11+
pq.push((v + 2) / 3);
12+
k -= 1;
13+
}
14+
}
15+
ans
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maxKelements(nums: number[], k: number): number {
2+
const pq = new MaxPriorityQueue();
3+
nums.forEach(num => pq.enqueue(num));
4+
let ans = 0;
5+
while (k > 0) {
6+
const v = pq.dequeue()!.element;
7+
ans += v;
8+
pq.enqueue(Math.floor((v + 2) / 3));
9+
k--;
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)