Skip to content

Commit 9cd7026

Browse files
committed
update: 添加问题“2598.执行操作后的最大MEX”的代码和题解 (#1176)
2598: AC.cpp+py+java+go+rust (#1175) WA.cpp AC.cpp - AC,9.76%,37.81% AC.py - AC,48.78%,43.90% AC.java - AC,9.30%,6.98% AC.go - AC,50.00%,12.50% AC.rust - AC,50.00%,12.50% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 5327e8c commit 9cd7026

17 files changed

+384
-73
lines changed

.commitmsg

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
3350: AC.cpp (#1173)
1+
2598: AC.cpp+py+java+go+rust (#1175)
22

3-
WA.cpp+py+go+java+rust
4-
AC.cpp - AC,5.12%,21.99%
5-
WA.cpp - 写法2
6-
AC.cpp - 写法2,AC,98.49%,48.80%
7-
AC.py - AC,32.38%,85.24%
8-
AC.go - AC,91.89%,54.05%
9-
AC.java - AC,36.99%,75.72%
10-
AC.rust - AC,100.00%,20.00%
3+
WA.cpp
4+
AC.cpp - AC,9.76%,37.81%
5+
AC.py - AC,48.78%,43.90%
6+
AC.java - AC,9.30%,6.98%
7+
AC.go - AC,50.00%,12.50%
8+
AC.rust - AC,50.00%,12.50%
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-16 19:40:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-16 19:43:19
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS IS NOT RIGHT
12+
class Solution {
13+
public:
14+
int findSmallestInteger(vector<int>& nums, int value) {
15+
unordered_map<int, int> ma;
16+
for (int t : nums) {
17+
ma[t % value]++;
18+
}
19+
int ans = 0;
20+
while (true) {
21+
cout << ans << ',' << ma[ans] << endl;
22+
if (--ma[ans] == -1) {
23+
return ans;
24+
}
25+
ans++;
26+
}
27+
}
28+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-16 19:40:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-16 20:01:33
6+
*/
7+
package main
8+
9+
func findSmallestInteger(nums []int, value int) (ans int) {
10+
cnt := map[int]int{}
11+
for _, t := range nums {
12+
cnt[(t % value + value) % value]++
13+
}
14+
for cnt[ans % value] > 0 {
15+
cnt[ans % value]--
16+
ans++
17+
}
18+
return
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-16 19:40:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-16 19:59:10
6+
*/
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
10+
class Solution {
11+
public int findSmallestInteger(int[] nums, int value) {
12+
Map<Integer, Integer> cnt = new HashMap<>();
13+
for (int t : nums) {
14+
cnt.merge((t % value + value) % value, 1, Integer::sum);
15+
}
16+
int ans = 0;
17+
while (cnt.merge(ans % value, -1, Integer::sum) >= 0) { // 记得是大于等于
18+
ans++;
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-10-16 19:40:17
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-10-16 19:55:23
6+
'''
7+
from typing import List
8+
from collections import Counter
9+
10+
class Solution:
11+
def findSmallestInteger(self, nums: List[int], value: int) -> int:
12+
cnt = Counter(x % value for x in nums) # python负数模正数是正数
13+
ans = 0
14+
while cnt[ans % value]:
15+
cnt[ans % value] -= 1
16+
ans += 1
17+
return ans
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-16 19:40:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-16 20:18:30
6+
*/
7+
use std::collections::HashMap;
8+
9+
// 这语言设计的真精妙
10+
impl Solution {
11+
pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {
12+
let mut cnt: HashMap<i32, i32> = HashMap::new();
13+
for x in nums {
14+
*cnt.entry((x % value + value) % value).or_insert(0) += 1;
15+
}
16+
17+
for ans in 0.. {
18+
// if matches!(cnt.get_mut(&(ans % value)), Some(v) if *v > 0) {
19+
// *v -= 1; // 无法使用v报错
20+
// continue;
21+
// }
22+
if let Some(v) = cnt.get_mut(&(ans % value)) {
23+
if *v > 0 { // 记得是大于
24+
*v -= 1;
25+
continue;
26+
}
27+
}
28+
return ans;
29+
}
30+
unreachable!()
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-16 19:40:17
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-16 20:23:38
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS IS RIGHT
12+
class Solution {
13+
public:
14+
int findSmallestInteger(vector<int>& nums, int value) {
15+
unordered_map<int, int> ma;
16+
for (int t : nums) {
17+
ma[(t % value + value) % value]++;
18+
}
19+
int ans = 0;
20+
while (true) {
21+
if (--ma[ans % value] == -1) {
22+
return ans;
23+
}
24+
ans++;
25+
}
26+
}
27+
};

Codes/3350-adjacent-increasing-subarrays-detection-ii.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* @Author: LetMeFly
33
* @Date: 2025-10-15 22:07:17
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-10-15 22:12:42
5+
* @LastEditTime: 2025-10-15 22:52:33
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS IS NOT RIGHT
1211
class Solution {
1312
public:
1413
int maxIncreasingSubarrays(vector<int>& nums) {
@@ -18,12 +17,13 @@ class Solution {
1817
if (t <= lastVal) {
1918
ans = max(ans, min(lastCnt, nowCnt));
2019
ans = max(ans, nowCnt / 2);
21-
lastCnt = nowCnt, nowCnt = 0;
20+
// printf("ans = %d, t = %d, lastCnt = %d, nowCnt = %d\n", ans, t, lastCnt, nowCnt);
21+
lastCnt = nowCnt, nowCnt = 1;
2222
} else {
2323
nowCnt++;
2424
}
2525
lastVal = t;
2626
}
27-
return ans;
27+
return max({ans, min(lastCnt, nowCnt), nowCnt / 2});
2828
}
29-
};
29+
};

Codes/3350-adjacent-increasing-subarrays-detection-ii_right.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

Codes/3350-adjacent-increasing-subarrays-detection-ii_xieFa2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* @Author: LetMeFly
33
* @Date: 2025-10-15 22:07:17
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-10-15 22:20:31
5+
* @LastEditTime: 2025-10-15 22:20:57
66
*/
77
#if defined(_WIN32) || defined(__APPLE__)
88
#include "_[1,2]toVector.h"
99
#endif
1010

11-
// THIS IS NOT RIGHT
1211
class Solution {
1312
public:
1413
int maxIncreasingSubarrays(vector<int>& nums) {
@@ -17,6 +16,7 @@ class Solution {
1716
nowCnt++;
1817
if (i == nums.size() - 1 || nums[i] >= nums[i + 1]) { // 递增数组之尾
1918
ans = max({ans, min(lastCnt, nowCnt), nowCnt / 2});
19+
lastCnt = nowCnt, nowCnt = 0;
2020
}
2121
}
2222
return ans;

0 commit comments

Comments
 (0)