Skip to content

Commit 7b14fde

Browse files
committed
feat: add rust solution to lc problems: No.2335,2336
- No.2335.Minimum Amount of Time to Fill Cups - No.2336.Smallest Number in Infinite Set
1 parent 8199832 commit 7b14fde

File tree

7 files changed

+181
-10
lines changed

7 files changed

+181
-10
lines changed

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,26 @@ func fillCups(amount []int) int {
141141
function fillCups(amount: number[]): number {
142142
amount.sort((a, b) => a - b);
143143
let [a, b, c] = amount;
144-
let diff = (a + b) - c;
144+
let diff = a + b - c;
145145
if (diff <= 0) return c;
146-
else return Math.floor((diff + 1) / 2) + c;
147-
};
146+
else return Math.floor((diff + 1) / 2) + c;
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
impl Solution {
154+
pub fn fill_cups(mut amount: Vec<i32>) -> i32 {
155+
amount.sort();
156+
let dif = amount[0] + amount[1] - amount[2];
157+
if dif <= 0 {
158+
return amount[2];
159+
}
160+
(dif + 1) / 2 + amount[2]
161+
}
162+
}
163+
148164
```
149165

150166
### **...**

solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/README_EN.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,31 @@ func fillCups(amount []int) int {
130130
function fillCups(amount: number[]): number {
131131
amount.sort((a, b) => a - b);
132132
let [a, b, c] = amount;
133-
let diff = (a + b) - c;
133+
let diff = a + b - c;
134134
if (diff <= 0) return c;
135-
else return Math.floor((diff + 1) / 2) + c;
136-
};
135+
else return Math.floor((diff + 1) / 2) + c;
136+
}
137137
```
138138

139+
### **Rust**
140+
141+
```rust
142+
impl Solution {
143+
pub fn fill_cups(mut amount: Vec<i32>) -> i32 {
144+
amount.sort();
145+
let dif = amount[0] + amount[1] - amount[2];
146+
if dif <= 0 {
147+
return amount[2];
148+
}
149+
(dif + 1) / 2 + amount[2]
150+
}
151+
}
152+
139153
### **...**
140154

141155
```
142156

143157
```
144158
145159
<!-- tabs:end -->
160+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn fill_cups(mut amount: Vec<i32>) -> i32 {
3+
amount.sort();
4+
let dif = amount[0] + amount[1] - amount[2];
5+
if dif <= 0 {
6+
return amount[2];
7+
}
8+
(dif + 1) / 2 + amount[2]
9+
}
10+
}

solution/2300-2399/2336.Smallest Number in Infinite Set/README.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ func (h *hp) top() int { a := *h; return a[0] }
328328

329329
```ts
330330
class SmallestInfiniteSet {
331-
hashMap;
331+
private hashMap: boolean[];
332+
332333
constructor() {
333334
this.hashMap = new Array(1001).fill(true);
334335
}
@@ -340,6 +341,7 @@ class SmallestInfiniteSet {
340341
return i;
341342
}
342343
}
344+
return -1;
343345
}
344346

345347
addBack(num: number): void {
@@ -357,6 +359,49 @@ class SmallestInfiniteSet {
357359
*/
358360
```
359361

362+
### **Rust**
363+
364+
```rust
365+
struct SmallestInfiniteSet {
366+
counter: [bool; 1000]
367+
}
368+
369+
370+
/**
371+
* `&self` means the method takes an immutable reference.
372+
* If you need a mutable reference, change it to `&mut self` instead.
373+
*/
374+
impl SmallestInfiniteSet {
375+
376+
fn new() -> Self {
377+
Self {
378+
counter: [true; 1000]
379+
}
380+
}
381+
382+
fn pop_smallest(&mut self) -> i32 {
383+
for i in 0..1000 {
384+
if self.counter[i] {
385+
self.counter[i] = false;
386+
return i as i32 + 1;
387+
}
388+
}
389+
-1
390+
}
391+
392+
fn add_back(&mut self, num: i32) {
393+
self.counter[num as usize - 1] = true;
394+
}
395+
}
396+
397+
/**
398+
* Your SmallestInfiniteSet object will be instantiated and called as such:
399+
* let obj = SmallestInfiniteSet::new();
400+
* let ret_1: i32 = obj.pop_smallest();
401+
* obj.add_back(num);
402+
*/
403+
```
404+
360405
### **...**
361406

362407
```

solution/2300-2399/2336.Smallest Number in Infinite Set/README_EN.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ func (h *hp) top() int { a := *h; return a[0] }
316316

317317
```ts
318318
class SmallestInfiniteSet {
319-
hashMap;
319+
private hashMap: boolean[];
320+
320321
constructor() {
321322
this.hashMap = new Array(1001).fill(true);
322323
}
@@ -328,6 +329,7 @@ class SmallestInfiniteSet {
328329
return i;
329330
}
330331
}
332+
return -1;
331333
}
332334

333335
addBack(num: number): void {
@@ -345,6 +347,49 @@ class SmallestInfiniteSet {
345347
*/
346348
```
347349

350+
### **Rust**
351+
352+
```rust
353+
struct SmallestInfiniteSet {
354+
counter: [bool; 1000]
355+
}
356+
357+
358+
/**
359+
* `&self` means the method takes an immutable reference.
360+
* If you need a mutable reference, change it to `&mut self` instead.
361+
*/
362+
impl SmallestInfiniteSet {
363+
364+
fn new() -> Self {
365+
Self {
366+
counter: [true; 1000]
367+
}
368+
}
369+
370+
fn pop_smallest(&mut self) -> i32 {
371+
for i in 0..1000 {
372+
if self.counter[i] {
373+
self.counter[i] = false;
374+
return i as i32 + 1;
375+
}
376+
}
377+
-1
378+
}
379+
380+
fn add_back(&mut self, num: i32) {
381+
self.counter[num as usize - 1] = true;
382+
}
383+
}
384+
385+
/**
386+
* Your SmallestInfiniteSet object will be instantiated and called as such:
387+
* let obj = SmallestInfiniteSet::new();
388+
* let ret_1: i32 = obj.pop_smallest();
389+
* obj.add_back(num);
390+
*/
391+
```
392+
348393
### **...**
349394

350395
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
struct SmallestInfiniteSet {
2+
counter: [bool; 1000]
3+
}
4+
5+
6+
/**
7+
* `&self` means the method takes an immutable reference.
8+
* If you need a mutable reference, change it to `&mut self` instead.
9+
*/
10+
impl SmallestInfiniteSet {
11+
12+
fn new() -> Self {
13+
Self {
14+
counter: [true; 1000]
15+
}
16+
}
17+
18+
fn pop_smallest(&mut self) -> i32 {
19+
for i in 0..1000 {
20+
if self.counter[i] {
21+
self.counter[i] = false;
22+
return i as i32 + 1;
23+
}
24+
}
25+
-1
26+
}
27+
28+
fn add_back(&mut self, num: i32) {
29+
self.counter[num as usize - 1] = true;
30+
}
31+
}
32+
33+
/**
34+
* Your SmallestInfiniteSet object will be instantiated and called as such:
35+
* let obj = SmallestInfiniteSet::new();
36+
* let ret_1: i32 = obj.pop_smallest();
37+
* obj.add_back(num);
38+
*/

solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class SmallestInfiniteSet {
2-
hashMap;
2+
private hashMap: boolean[];
3+
34
constructor() {
45
this.hashMap = new Array(1001).fill(true);
56
}
@@ -11,6 +12,7 @@ class SmallestInfiniteSet {
1112
return i;
1213
}
1314
}
15+
return -1;
1416
}
1517

1618
addBack(num: number): void {
@@ -25,4 +27,4 @@ class SmallestInfiniteSet {
2527
* var obj = new SmallestInfiniteSet()
2628
* var param_1 = obj.popSmallest()
2729
* obj.addBack(num)
28-
*/
30+
*/

0 commit comments

Comments
 (0)