Skip to content

Commit f9c4ed0

Browse files
authored
feat: add solutions to lc problems (doocs#2438)
1 parent 9b1aa7b commit f9c4ed0

File tree

15 files changed

+232
-90
lines changed

15 files changed

+232
-90
lines changed

solution/2500-2599/2549.Count Distinct Numbers on Board/README.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
### 方法一:脑筋急转弯
6161

62-
由于每一次对桌面上的数字 $n$ 进行操作,会使得数字 $n-1$ 也出现在桌面上,因此最终桌面上的数字为 $[2,...n]$, $n-1$ 个数字。
62+
由于每一次对桌面上的数字 $n$ 进行操作,会使得数字 $n-1$ 也出现在桌面上,因此最终桌面上的数字为 $[2,...n]$, $n-1$ 个数字。
6363

6464
注意到 $n$ 可能为 $1$,因此需要特判。
6565

@@ -92,10 +92,7 @@ public:
9292
9393
```go
9494
func distinctIntegers(n int) int {
95-
if n == 1 {
96-
return 1
97-
}
98-
return n - 1
95+
return max(1, n-1)
9996
}
10097
```
10198

@@ -108,11 +105,7 @@ function distinctIntegers(n: number): number {
108105
```rust
109106
impl Solution {
110107
pub fn distinct_integers(n: i32) -> i32 {
111-
if n == 1 {
112-
return 1;
113-
}
114-
115-
n - 1
108+
(1).max(n - 1)
116109
}
117110
}
118111
```

solution/2500-2599/2549.Count Distinct Numbers on Board/README_EN.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ public:
8888
8989
```go
9090
func distinctIntegers(n int) int {
91-
if n == 1 {
92-
return 1
93-
}
94-
return n - 1
91+
return max(1, n-1)
9592
}
9693
```
9794

@@ -104,11 +101,7 @@ function distinctIntegers(n: number): number {
104101
```rust
105102
impl Solution {
106103
pub fn distinct_integers(n: i32) -> i32 {
107-
if n == 1 {
108-
return 1;
109-
}
110-
111-
n - 1
104+
(1).max(n - 1)
112105
}
113106
}
114107
```
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
func distinctIntegers(n int) int {
2-
if n == 1 {
3-
return 1
4-
}
5-
return n - 1
2+
return max(1, n-1)
63
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
impl Solution {
22
pub fn distinct_integers(n: i32) -> i32 {
3-
if n == 1 {
4-
return 1;
5-
}
6-
7-
n - 1
3+
(1).max(n - 1)
84
}
95
}

solution/2600-2699/2671.Frequency Tracker/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,61 @@ class FrequencyTracker {
274274
*/
275275
```
276276

277+
```rust
278+
use std::collections::HashMap;
279+
280+
struct FrequencyTracker {
281+
cnt: HashMap<i32, i32>,
282+
freq: HashMap<i32, i32>,
283+
}
284+
285+
/**
286+
* `&self` means the method takes an immutable reference.
287+
* If you need a mutable reference, change it to `&mut self` instead.
288+
*/
289+
impl FrequencyTracker {
290+
fn new() -> Self {
291+
FrequencyTracker {
292+
cnt: HashMap::new(),
293+
freq: HashMap::new(),
294+
}
295+
}
296+
297+
fn add(&mut self, number: i32) {
298+
let count = self.cnt.entry(number).or_insert(0);
299+
let prev_freq = self.freq.entry(*count).or_insert(0);
300+
*prev_freq -= 1;
301+
*count += 1;
302+
let new_freq = self.freq.entry(*count).or_insert(0);
303+
*new_freq += 1;
304+
}
305+
306+
fn delete_one(&mut self, number: i32) {
307+
if let Some(count) = self.cnt.get_mut(&number) {
308+
if *count > 0 {
309+
if let Some(prev_freq) = self.freq.get_mut(count) {
310+
*prev_freq -= 1;
311+
}
312+
*count -= 1;
313+
if let Some(new_freq) = self.freq.get_mut(count) {
314+
*new_freq += 1;
315+
}
316+
}
317+
}
318+
}
319+
320+
fn has_frequency(&self, frequency: i32) -> bool {
321+
self.freq.get(&frequency).map_or(false, |&freq| freq > 0)
322+
}
323+
}/**
324+
* Your FrequencyTracker object will be instantiated and called as such:
325+
* let obj = FrequencyTracker::new();
326+
* obj.add(number);
327+
* obj.delete_one(number);
328+
* let ret_3: bool = obj.has_frequency(frequency);
329+
*/
330+
```
331+
277332
<!-- tabs:end -->
278333

279334
<!-- end -->

solution/2600-2699/2671.Frequency Tracker/README_EN.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,61 @@ class FrequencyTracker {
273273
*/
274274
```
275275

276+
```rust
277+
use std::collections::HashMap;
278+
279+
struct FrequencyTracker {
280+
cnt: HashMap<i32, i32>,
281+
freq: HashMap<i32, i32>,
282+
}
283+
284+
/**
285+
* `&self` means the method takes an immutable reference.
286+
* If you need a mutable reference, change it to `&mut self` instead.
287+
*/
288+
impl FrequencyTracker {
289+
fn new() -> Self {
290+
FrequencyTracker {
291+
cnt: HashMap::new(),
292+
freq: HashMap::new(),
293+
}
294+
}
295+
296+
fn add(&mut self, number: i32) {
297+
let count = self.cnt.entry(number).or_insert(0);
298+
let prev_freq = self.freq.entry(*count).or_insert(0);
299+
*prev_freq -= 1;
300+
*count += 1;
301+
let new_freq = self.freq.entry(*count).or_insert(0);
302+
*new_freq += 1;
303+
}
304+
305+
fn delete_one(&mut self, number: i32) {
306+
if let Some(count) = self.cnt.get_mut(&number) {
307+
if *count > 0 {
308+
if let Some(prev_freq) = self.freq.get_mut(count) {
309+
*prev_freq -= 1;
310+
}
311+
*count -= 1;
312+
if let Some(new_freq) = self.freq.get_mut(count) {
313+
*new_freq += 1;
314+
}
315+
}
316+
}
317+
}
318+
319+
fn has_frequency(&self, frequency: i32) -> bool {
320+
self.freq.get(&frequency).map_or(false, |&freq| freq > 0)
321+
}
322+
}/**
323+
* Your FrequencyTracker object will be instantiated and called as such:
324+
* let obj = FrequencyTracker::new();
325+
* obj.add(number);
326+
* obj.delete_one(number);
327+
* let ret_3: bool = obj.has_frequency(frequency);
328+
*/
329+
```
330+
276331
<!-- tabs:end -->
277332

278333
<!-- end -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::collections::HashMap;
2+
3+
struct FrequencyTracker {
4+
cnt: HashMap<i32, i32>,
5+
freq: HashMap<i32, 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 FrequencyTracker {
13+
fn new() -> Self {
14+
FrequencyTracker {
15+
cnt: HashMap::new(),
16+
freq: HashMap::new(),
17+
}
18+
}
19+
20+
fn add(&mut self, number: i32) {
21+
let count = self.cnt.entry(number).or_insert(0);
22+
let prev_freq = self.freq.entry(*count).or_insert(0);
23+
*prev_freq -= 1;
24+
*count += 1;
25+
let new_freq = self.freq.entry(*count).or_insert(0);
26+
*new_freq += 1;
27+
}
28+
29+
fn delete_one(&mut self, number: i32) {
30+
if let Some(count) = self.cnt.get_mut(&number) {
31+
if *count > 0 {
32+
if let Some(prev_freq) = self.freq.get_mut(count) {
33+
*prev_freq -= 1;
34+
}
35+
*count -= 1;
36+
if let Some(new_freq) = self.freq.get_mut(count) {
37+
*new_freq += 1;
38+
}
39+
}
40+
}
41+
}
42+
43+
fn has_frequency(&self, frequency: i32) -> bool {
44+
self.freq.get(&frequency).map_or(false, |&freq| freq > 0)
45+
}
46+
}/**
47+
* Your FrequencyTracker object will be instantiated and called as such:
48+
* let obj = FrequencyTracker::new();
49+
* obj.add(number);
50+
* obj.delete_one(number);
51+
* let ret_3: bool = obj.has_frequency(frequency);
52+
*/

solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@
5252

5353
## 解法
5454

55-
### 方法一
55+
### 方法一:倒序合并
56+
57+
根据题目描述,为了最大化合并后的数组中的最大元素,我们应该先合并右侧的元素,使得右侧的元素尽可能大,从而尽可能多地执行合并操作,最终得到最大的元素。
58+
59+
因此,我们可以从右向左遍历数组,对于每个位置 $i$,其中 $i \in [0, n - 2]$,如果 $nums[i] \leq nums[i + 1]$,我们就将 $nums[i]$ 更新为 $nums[i] + nums[i + 1]$。这样做,相当于将 $nums[i]$ 与 $nums[i + 1]$ 合并,并且删掉 $nums[i]$。
60+
61+
最终,数组中的最大元素就是合并后的数组中的最大元素。
62+
63+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
5664

5765
<!-- tabs:start -->
5866

solution/2700-2799/2789.Largest Element in an Array after Merge Operations/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ There is only one element in the final array, which is 11.
5050

5151
## Solutions
5252

53-
### Solution 1
53+
### Solution 1: Merge in Reverse Order
54+
55+
According to the problem description, in order to maximize the maximum element in the merged array, we should merge the elements on the right first, making the elements on the right as large as possible, so as to perform as many merge operations as possible and finally get the maximum element.
56+
57+
Therefore, we can traverse the array from right to left. For each position $i$, where $i \in [0, n - 2]$, if $nums[i] \leq nums[i + 1]$, we update $nums[i]$ to $nums[i] + nums[i + 1]$. Doing so is equivalent to merging $nums[i]$ and $nums[i + 1]$ and deleting $nums[i]$.
58+
59+
In the end, the maximum element in the array is the maximum element in the merged array.
60+
61+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
5462

5563
<!-- tabs:start -->
5664

solution/2800-2899/2864.Maximum Odd Binary Number/README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ class Solution:
6464
```java
6565
class Solution {
6666
public String maximumOddBinaryNumber(String s) {
67-
int cnt = 0;
68-
for (char c : s.toCharArray()) {
69-
if (c == '1') {
70-
++cnt;
71-
}
72-
}
67+
int cnt = s.length() - s.replace("1", "").length();
7368
return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
7469
}
7570
}
@@ -79,16 +74,8 @@ class Solution {
7974
class Solution {
8075
public:
8176
string maximumOddBinaryNumber(string s) {
82-
int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
83-
string ans;
84-
for (int i = 1; i < cnt; ++i) {
85-
ans.push_back('1');
86-
}
87-
for (int i = 0; i < s.size() - cnt; ++i) {
88-
ans.push_back('0');
89-
}
90-
ans.push_back('1');
91-
return ans;
77+
int cnt = count(s.begin(), s.end(), '1');
78+
return string(cnt - 1, '1') + string(s.size() - cnt, '0') + '1';
9279
}
9380
};
9481
```
@@ -102,14 +89,23 @@ func maximumOddBinaryNumber(s string) string {
10289

10390
```ts
10491
function maximumOddBinaryNumber(s: string): string {
105-
let cnt = 0;
106-
for (const c of s) {
107-
cnt += c === '1' ? 1 : 0;
108-
}
92+
const cnt = s.length - s.replace(/1/g, '').length;
10993
return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1';
11094
}
11195
```
11296

97+
```rust
98+
impl Solution {
99+
pub fn maximum_odd_binary_number(s: String) -> String {
100+
let cnt = s
101+
.chars()
102+
.filter(|&c| c == '1')
103+
.count();
104+
"1".repeat(cnt - 1) + &"0".repeat(s.len() - cnt) + "1"
105+
}
106+
}
107+
```
108+
113109
<!-- tabs:end -->
114110

115111
<!-- end -->

0 commit comments

Comments
 (0)