Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78d53e1

Browse files
committed
feat: add solutions to lc problems: No.2349,2749
1 parent dd52bbf commit 78d53e1

File tree

8 files changed

+341
-1
lines changed

8 files changed

+341
-1
lines changed

‎solution/2300-2399/2349.Design a Number Container System/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,84 @@ class TreeSet<T = number> {
747747
*/
748748
```
749749

750+
#### Rust
751+
752+
```rust
753+
use std::collections::{HashMap, BTreeSet};
754+
755+
struct NumberContainers {
756+
d: HashMap<i32, i32>,
757+
g: HashMap<i32, BTreeSet<i32>>,
758+
}
759+
760+
/**
761+
* `&self` means the method takes an immutable reference.
762+
* If you need a mutable reference, change it to `&mut self` instead.
763+
*/
764+
impl NumberContainers {
765+
fn new() -> Self {
766+
Self {
767+
d: HashMap::new(),
768+
g: HashMap::new(),
769+
}
770+
}
771+
772+
fn change(&mut self, index: i32, number: i32) {
773+
if let Some(&old_number) = self.d.get(&index) {
774+
if let Some(set) = self.g.get_mut(&old_number) {
775+
set.remove(&index);
776+
}
777+
}
778+
self.d.insert(index, number);
779+
self.g.entry(number).or_insert_with(BTreeSet::new).insert(index);
780+
}
781+
782+
fn find(&self, number: i32) -> i32 {
783+
match self.g.get(&number) {
784+
Some(set) if !set.is_empty() => *set.iter().next().unwrap(),
785+
_ => -1,
786+
}
787+
}
788+
}
789+
```
790+
791+
#### C#
792+
793+
```cs
794+
public class NumberContainers {
795+
private Dictionary<int, int> d = new Dictionary<int, int>();
796+
private Dictionary<int, SortedSet<int>> g = new Dictionary<int, SortedSet<int>>();
797+
798+
public NumberContainers() {
799+
}
800+
801+
public void Change(int index, int number) {
802+
if (d.TryGetValue(index, out int oldNumber)) {
803+
g[oldNumber].Remove(index);
804+
}
805+
d[index] = number;
806+
if (!g.ContainsKey(number)) {
807+
g[number] = new SortedSet<int>();
808+
}
809+
g[number].Add(index);
810+
}
811+
812+
public int Find(int number) {
813+
if (!g.ContainsKey(number) || g[number].Count == 0) {
814+
return -1;
815+
}
816+
return g[number].Min;
817+
}
818+
}
819+
820+
/**
821+
* Your NumberContainers object will be instantiated and called as such:
822+
* NumberContainers obj = new NumberContainers();
823+
* obj.Change(index, number);
824+
* int param_2 = obj.Find(number);
825+
*/
826+
```
827+
750828
<!-- tabs:end -->
751829

752830
<!-- solution:end -->

‎solution/2300-2399/2349.Design a Number Container System/README_EN.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ nc.change(1, 10); // Your container at index 1 will be filled with number 10.
5454
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
5555
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
5656
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
57-
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
57+
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
5858
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
5959
</pre>
6060

@@ -745,6 +745,84 @@ class TreeSet<T = number> {
745745
*/
746746
```
747747

748+
#### Rust
749+
750+
```rust
751+
use std::collections::{HashMap, BTreeSet};
752+
753+
struct NumberContainers {
754+
d: HashMap<i32, i32>,
755+
g: HashMap<i32, BTreeSet<i32>>,
756+
}
757+
758+
/**
759+
* `&self` means the method takes an immutable reference.
760+
* If you need a mutable reference, change it to `&mut self` instead.
761+
*/
762+
impl NumberContainers {
763+
fn new() -> Self {
764+
Self {
765+
d: HashMap::new(),
766+
g: HashMap::new(),
767+
}
768+
}
769+
770+
fn change(&mut self, index: i32, number: i32) {
771+
if let Some(&old_number) = self.d.get(&index) {
772+
if let Some(set) = self.g.get_mut(&old_number) {
773+
set.remove(&index);
774+
}
775+
}
776+
self.d.insert(index, number);
777+
self.g.entry(number).or_insert_with(BTreeSet::new).insert(index);
778+
}
779+
780+
fn find(&self, number: i32) -> i32 {
781+
match self.g.get(&number) {
782+
Some(set) if !set.is_empty() => *set.iter().next().unwrap(),
783+
_ => -1,
784+
}
785+
}
786+
}
787+
```
788+
789+
#### C#
790+
791+
```cs
792+
public class NumberContainers {
793+
private Dictionary<int, int> d = new Dictionary<int, int>();
794+
private Dictionary<int, SortedSet<int>> g = new Dictionary<int, SortedSet<int>>();
795+
796+
public NumberContainers() {
797+
}
798+
799+
public void Change(int index, int number) {
800+
if (d.TryGetValue(index, out int oldNumber)) {
801+
g[oldNumber].Remove(index);
802+
}
803+
d[index] = number;
804+
if (!g.ContainsKey(number)) {
805+
g[number] = new SortedSet<int>();
806+
}
807+
g[number].Add(index);
808+
}
809+
810+
public int Find(int number) {
811+
if (!g.ContainsKey(number) || g[number].Count == 0) {
812+
return -1;
813+
}
814+
return g[number].Min;
815+
}
816+
}
817+
818+
/**
819+
* Your NumberContainers object will be instantiated and called as such:
820+
* NumberContainers obj = new NumberContainers();
821+
* obj.Change(index, number);
822+
* int param_2 = obj.Find(number);
823+
*/
824+
```
825+
748826
<!-- tabs:end -->
749827

750828
<!-- solution:end -->
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class NumberContainers {
2+
private Dictionary<int, int> d = new Dictionary<int, int>();
3+
private Dictionary<int, SortedSet<int>> g = new Dictionary<int, SortedSet<int>>();
4+
5+
public NumberContainers() {
6+
}
7+
8+
public void Change(int index, int number) {
9+
if (d.TryGetValue(index, out int oldNumber)) {
10+
g[oldNumber].Remove(index);
11+
}
12+
d[index] = number;
13+
if (!g.ContainsKey(number)) {
14+
g[number] = new SortedSet<int>();
15+
}
16+
g[number].Add(index);
17+
}
18+
19+
public int Find(int number) {
20+
if (!g.ContainsKey(number) || g[number].Count == 0) {
21+
return -1;
22+
}
23+
return g[number].Min;
24+
}
25+
}
26+
27+
/**
28+
* Your NumberContainers object will be instantiated and called as such:
29+
* NumberContainers obj = new NumberContainers();
30+
* obj.Change(index, number);
31+
* int param_2 = obj.Find(number);
32+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::collections::{BTreeSet, HashMap};
2+
3+
struct NumberContainers {
4+
d: HashMap<i32, i32>,
5+
g: HashMap<i32, BTreeSet<i32>>,
6+
}
7+
8+
/**
9+
* `&self` means the method takes an immutable reference.
10+
* If you need a mutable reference, change it to `&mut self` instead.
11+
*/
12+
impl NumberContainers {
13+
fn new() -> Self {
14+
Self {
15+
d: HashMap::new(),
16+
g: HashMap::new(),
17+
}
18+
}
19+
20+
fn change(&mut self, index: i32, number: i32) {
21+
if let Some(&old_number) = self.d.get(&index) {
22+
if let Some(set) = self.g.get_mut(&old_number) {
23+
set.remove(&index);
24+
}
25+
}
26+
self.d.insert(index, number);
27+
self.g
28+
.entry(number)
29+
.or_insert_with(BTreeSet::new)
30+
.insert(index);
31+
}
32+
33+
fn find(&self, number: i32) -> i32 {
34+
match self.g.get(&number) {
35+
Some(set) if !set.is_empty() => *set.iter().next().unwrap(),
36+
_ => -1,
37+
}
38+
}
39+
}

‎solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,47 @@ function makeTheIntegerZero(num1: number, num2: number): number {
169169
}
170170
```
171171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn make_the_integer_zero(num1: i32, num2: i32) -> i32 {
177+
let num1 = num1 as i64;
178+
let num2 = num2 as i64;
179+
for k in 1.. {
180+
let x = num1 - k * num2;
181+
if x < 0 {
182+
break;
183+
}
184+
if (x.count_ones() as i64) <= k && k <= x {
185+
return k as i32;
186+
}
187+
}
188+
-1
189+
}
190+
}
191+
```
192+
193+
#### C#
194+
195+
```cs
196+
public class Solution {
197+
public int MakeTheIntegerZero(int num1, int num2) {
198+
long a = num1, b = num2;
199+
for (long k = 1; ; ++k) {
200+
long x = a - k * b;
201+
if (x < 0) {
202+
break;
203+
}
204+
if (BitOperations.PopCount((ulong)x) <= k && k <= x) {
205+
return (int)k;
206+
}
207+
}
208+
return -1;
209+
}
210+
}
211+
```
212+
172213
<!-- tabs:end -->
173214

174215
<!-- solution:end -->

‎solution/2700-2799/2749.Minimum Operations to Make the Integer Zero/README_EN.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,47 @@ function makeTheIntegerZero(num1: number, num2: number): number {
167167
}
168168
```
169169

170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn make_the_integer_zero(num1: i32, num2: i32) -> i32 {
175+
let num1 = num1 as i64;
176+
let num2 = num2 as i64;
177+
for k in 1.. {
178+
let x = num1 - k * num2;
179+
if x < 0 {
180+
break;
181+
}
182+
if (x.count_ones() as i64) <= k && k <= x {
183+
return k as i32;
184+
}
185+
}
186+
-1
187+
}
188+
}
189+
```
190+
191+
#### C#
192+
193+
```cs
194+
public class Solution {
195+
public int MakeTheIntegerZero(int num1, int num2) {
196+
long a = num1, b = num2;
197+
for (long k = 1; ; ++k) {
198+
long x = a - k * b;
199+
if (x < 0) {
200+
break;
201+
}
202+
if (BitOperations.PopCount((ulong)x) <= k && k <= x) {
203+
return (int)k;
204+
}
205+
}
206+
return -1;
207+
}
208+
}
209+
```
210+
170211
<!-- tabs:end -->
171212

172213
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int MakeTheIntegerZero(int num1, int num2) {
3+
long a = num1, b = num2;
4+
for (long k = 1; ; ++k) {
5+
long x = a - k * b;
6+
if (x < 0) {
7+
break;
8+
}
9+
if (BitOperations.PopCount((ulong)x) <= k && k <= x) {
10+
return (int)k;
11+
}
12+
}
13+
return -1;
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn make_the_integer_zero(num1: i32, num2: i32) -> i32 {
3+
let num1 = num1 as i64;
4+
let num2 = num2 as i64;
5+
for k in 1.. {
6+
let x = num1 - k * num2;
7+
if x < 0 {
8+
break;
9+
}
10+
if (x.count_ones() as i64) <= k && k <= x {
11+
return k as i32;
12+
}
13+
}
14+
-1
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /