Skip to content

Commit 5c97dac

Browse files
authored
feat: add rust solution to lc problem: No.2558 (doocs#1224)
1 parent d30b6bd commit 5c97dac

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,30 @@ func (hp) Pop() (_ interface{}) { return }
144144
func (hp) Push(interface{}) {}
145145
```
146146

147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn pick_gifts(gifts: Vec<i32>, k: i32) -> i64 {
152+
let mut h = std::collections::BinaryHeap::from(gifts);
153+
let mut ans = 0;
154+
155+
for _ in 0..k {
156+
if let Some(mut max_gift) = h.pop() {
157+
max_gift = (max_gift as f64).sqrt().floor() as i32;
158+
h.push(max_gift);
159+
}
160+
}
161+
162+
for x in h {
163+
ans += x as i64;
164+
}
165+
166+
ans
167+
}
168+
}
169+
```
170+
147171
### **...**
148172

149173
```

solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ func (hp) Pop() (_ interface{}) { return }
126126
func (hp) Push(interface{}) {}
127127
```
128128

129+
### **Rust**
130+
131+
```rust
132+
impl Solution {
133+
pub fn pick_gifts(gifts: Vec<i32>, k: i32) -> i64 {
134+
let mut h = std::collections::BinaryHeap::from(gifts);
135+
let mut ans = 0;
136+
137+
for _ in 0..k {
138+
if let Some(mut max_gift) = h.pop() {
139+
max_gift = (max_gift as f64).sqrt().floor() as i32;
140+
h.push(max_gift);
141+
}
142+
}
143+
144+
for x in h {
145+
ans += x as i64;
146+
}
147+
148+
ans
149+
}
150+
}
151+
```
152+
129153
### **...**
130154

131155
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn pick_gifts(gifts: Vec<i32>, k: i32) -> i64 {
3+
let mut h = std::collections::BinaryHeap::from(gifts);
4+
let mut ans = 0;
5+
6+
for _ in 0..k {
7+
if let Some(mut max_gift) = h.pop() {
8+
max_gift = (max_gift as f64).sqrt().floor() as i32;
9+
h.push(max_gift);
10+
}
11+
}
12+
13+
for x in h {
14+
ans += x as i64;
15+
}
16+
17+
ans
18+
}
19+
}

0 commit comments

Comments
 (0)