Skip to content

Commit 833e953

Browse files
committed
+ problem 1224
1 parent 9421323 commit 833e953

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 1224. Maximum Equal Frequency
2+
Given an array `nums` of positive integers, return the longest possible length of an array prefix of `nums`, such that it is possible to remove **exactly one** element from this prefix so that every number that has appeared in it will have the same number of occurrences.
3+
4+
If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [2,2,1,1,5,3,3,5]
9+
<strong>Output:</strong> 7
10+
<strong>Explanation:</strong> For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4] = 5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
16+
<strong>Output:</strong> 13
17+
</pre>
18+
19+
#### Constraints:
20+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
21+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
22+
23+
## Solutions (Rust)
24+
25+
### 1. Solution
26+
```Rust
27+
use std::collections::HashMap;
28+
29+
impl Solution {
30+
pub fn max_equal_freq(nums: Vec<i32>) -> i32 {
31+
let mut count_nums = HashMap::new();
32+
let mut count_ocurrences = HashMap::new();
33+
34+
for &num in &nums {
35+
*count_nums.entry(num).or_insert(0) += 1;
36+
}
37+
for &ocurrence in count_nums.values() {
38+
*count_ocurrences.entry(ocurrence).or_insert(0) += 1;
39+
}
40+
41+
for i in (0..nums.len()).rev() {
42+
if count_ocurrences.len() == 1 {
43+
let (&k0, &v0) = count_ocurrences.iter().next().unwrap();
44+
45+
if k0 == 1 || v0 == 1 {
46+
return i as i32 + 1;
47+
}
48+
} else if count_ocurrences.len() == 2 {
49+
let (&k0, &v0) = count_ocurrences.iter().max().unwrap();
50+
let (&k1, &v1) = count_ocurrences.iter().min().unwrap();
51+
52+
if (k1, v1) == (1, 1) || (k0 == k1 + 1 && v0 == 1) {
53+
return i as i32 + 1;
54+
}
55+
}
56+
57+
*count_ocurrences.get_mut(&count_nums[&nums[i]]).unwrap() -= 1;
58+
if count_ocurrences.get(&(count_nums[&nums[i]])) == Some(&0) {
59+
count_ocurrences.remove(&count_nums[&nums[i]]);
60+
}
61+
*count_nums.get_mut(&nums[i]).unwrap() -= 1;
62+
if count_nums[&nums[i]] > 0 {
63+
*count_ocurrences.entry(count_nums[&nums[i]]).or_insert(0) += 1;
64+
}
65+
}
66+
67+
1
68+
}
69+
}
70+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 1224. 最大相等频率
2+
给你一个正整数数组 `nums`,请你帮忙从该数组中找出能满足下面要求的 **最长** 前缀,并返回该前缀的长度:
3+
4+
* 从前缀中 **恰好删除一个** 元素后,剩下每个数字的出现次数都相同。
5+
6+
如果删除这个元素后没有剩余元素存在,仍可认为每个数字都具有相同的出现次数(也就是 0 次)。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> nums = [2,2,1,1,5,3,3,5]
11+
<strong>输出:</strong> 7
12+
<strong>解释:</strong> 对于长度为 7 的子数组 [2,2,1,1,5,3,3],如果我们从中删去 nums[4] = 5,就可以得到 [2,2,1,1,3,3],里面每个数字都出现了两次。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
18+
<strong>输出:</strong> 13
19+
</pre>
20+
21+
#### 提示:
22+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
23+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
24+
25+
## 题解 (Rust)
26+
27+
### 1. 题解
28+
```Rust
29+
use std::collections::HashMap;
30+
31+
impl Solution {
32+
pub fn max_equal_freq(nums: Vec<i32>) -> i32 {
33+
let mut count_nums = HashMap::new();
34+
let mut count_ocurrences = HashMap::new();
35+
36+
for &num in &nums {
37+
*count_nums.entry(num).or_insert(0) += 1;
38+
}
39+
for &ocurrence in count_nums.values() {
40+
*count_ocurrences.entry(ocurrence).or_insert(0) += 1;
41+
}
42+
43+
for i in (0..nums.len()).rev() {
44+
if count_ocurrences.len() == 1 {
45+
let (&k0, &v0) = count_ocurrences.iter().next().unwrap();
46+
47+
if k0 == 1 || v0 == 1 {
48+
return i as i32 + 1;
49+
}
50+
} else if count_ocurrences.len() == 2 {
51+
let (&k0, &v0) = count_ocurrences.iter().max().unwrap();
52+
let (&k1, &v1) = count_ocurrences.iter().min().unwrap();
53+
54+
if (k1, v1) == (1, 1) || (k0 == k1 + 1 && v0 == 1) {
55+
return i as i32 + 1;
56+
}
57+
}
58+
59+
*count_ocurrences.get_mut(&count_nums[&nums[i]]).unwrap() -= 1;
60+
if count_ocurrences.get(&(count_nums[&nums[i]])) == Some(&0) {
61+
count_ocurrences.remove(&count_nums[&nums[i]]);
62+
}
63+
*count_nums.get_mut(&nums[i]).unwrap() -= 1;
64+
if count_nums[&nums[i]] > 0 {
65+
*count_ocurrences.entry(count_nums[&nums[i]]).or_insert(0) += 1;
66+
}
67+
}
68+
69+
1
70+
}
71+
}
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn max_equal_freq(nums: Vec<i32>) -> i32 {
5+
let mut count_nums = HashMap::new();
6+
let mut count_ocurrences = HashMap::new();
7+
8+
for &num in &nums {
9+
*count_nums.entry(num).or_insert(0) += 1;
10+
}
11+
for &ocurrence in count_nums.values() {
12+
*count_ocurrences.entry(ocurrence).or_insert(0) += 1;
13+
}
14+
15+
for i in (0..nums.len()).rev() {
16+
if count_ocurrences.len() == 1 {
17+
let (&k0, &v0) = count_ocurrences.iter().next().unwrap();
18+
19+
if k0 == 1 || v0 == 1 {
20+
return i as i32 + 1;
21+
}
22+
} else if count_ocurrences.len() == 2 {
23+
let (&k0, &v0) = count_ocurrences.iter().max().unwrap();
24+
let (&k1, &v1) = count_ocurrences.iter().min().unwrap();
25+
26+
if (k1, v1) == (1, 1) || (k0 == k1 + 1 && v0 == 1) {
27+
return i as i32 + 1;
28+
}
29+
}
30+
31+
*count_ocurrences.get_mut(&count_nums[&nums[i]]).unwrap() -= 1;
32+
if count_ocurrences.get(&(count_nums[&nums[i]])) == Some(&0) {
33+
count_ocurrences.remove(&count_nums[&nums[i]]);
34+
}
35+
*count_nums.get_mut(&nums[i]).unwrap() -= 1;
36+
if count_nums[&nums[i]] > 0 {
37+
*count_ocurrences.entry(count_nums[&nums[i]]).or_insert(0) += 1;
38+
}
39+
}
40+
41+
1
42+
}
43+
}

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@
660660
[1221][1221l]|[Split a String in Balanced Strings][1221] |![rb]&nbsp;&nbsp;![rs]
661661
[1222][1222l]|[Queens That Can Attack the King][1222] |![rs]
662662
[1223][1223l]|[Dice Roll Simulation][1223] |![rs]
663+
[1224][1224l]|[Maximum Equal Frequency][1224] |![rs]
663664
[1232][1232l]|[Check If It Is a Straight Line][1232] |![rs]
664665
[1233][1233l]|[Remove Sub-Folders from the Filesystem][1233] |![rs]
665666
[1234][1234l]|[Replace the Substring for Balanced String][1234] |![rb]&nbsp;&nbsp;![rs]
@@ -1957,6 +1958,7 @@
19571958
[1221]:Problemset/1221-Split%20a%20String%20in%20Balanced%20Strings/README.md#1221-split-a-string-in-balanced-strings
19581959
[1222]:Problemset/1222-Queens%20That%20Can%20Attack%20the%20King/README.md#1222-queens-that-can-attack-the-king
19591960
[1223]:Problemset/1223-Dice%20Roll%20Simulation/README.md#1223-dice-roll-simulation
1961+
[1224]:Problemset/1224-Maximum%20Equal%20Frequency/README.md#1224-maximum-equal-frequency
19601962
[1232]:Problemset/1232-Check%20If%20It%20Is%20a%20Straight%20Line/README.md#1232-check-if-it-is-a-straight-line
19611963
[1233]:Problemset/1233-Remove%20Sub-Folders%20from%20the%20Filesystem/README.md#1233-remove-sub-folders-from-the-filesystem
19621964
[1234]:Problemset/1234-Replace%20the%20Substring%20for%20Balanced%20String/README.md#1234-replace-the-substring-for-balanced-string
@@ -3257,6 +3259,7 @@
32573259
[1221l]:https://leetcode.com/problems/split-a-string-in-balanced-strings/
32583260
[1222l]:https://leetcode.com/problems/queens-that-can-attack-the-king/
32593261
[1223l]:https://leetcode.com/problems/dice-roll-simulation/
3262+
[1224l]:https://leetcode.com/problems/maximum-equal-frequency/
32603263
[1232l]:https://leetcode.com/problems/check-if-it-is-a-straight-line/
32613264
[1233l]:https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/
32623265
[1234l]:https://leetcode.com/problems/replace-the-substring-for-balanced-string/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@
660660
[1221][1221l]|[分割平衡字符串][1221] |![rb]&nbsp;&nbsp;![rs]
661661
[1222][1222l]|[可以攻击国王的皇后][1222] |![rs]
662662
[1223][1223l]|[掷骰子模拟][1223] |![rs]
663+
[1224][1224l]|[最大相等频率][1224] |![rs]
663664
[1232][1232l]|[缀点成线][1232] |![rs]
664665
[1233][1233l]|[删除子文件夹][1233] |![rs]
665666
[1234][1234l]|[替换子串得到平衡字符串][1234] |![rb]&nbsp;&nbsp;![rs]
@@ -1957,6 +1958,7 @@
19571958
[1221]:Problemset/1221-Split%20a%20String%20in%20Balanced%20Strings/README_CN.md#1221-分割平衡字符串
19581959
[1222]:Problemset/1222-Queens%20That%20Can%20Attack%20the%20King/README_CN.md#1222-可以攻击国王的皇后
19591960
[1223]:Problemset/1223-Dice%20Roll%20Simulation/README_CN.md#1223-掷骰子模拟
1961+
[1224]:Problemset/1224-Maximum%20Equal%20Frequency/README_CN.md#1224-最大相等频率
19601962
[1232]:Problemset/1232-Check%20If%20It%20Is%20a%20Straight%20Line/README_CN.md#1232-缀点成线
19611963
[1233]:Problemset/1233-Remove%20Sub-Folders%20from%20the%20Filesystem/README_CN.md#1233-删除子文件夹
19621964
[1234]:Problemset/1234-Replace%20the%20Substring%20for%20Balanced%20String/README_CN.md#1234-替换子串得到平衡字符串
@@ -3257,6 +3259,7 @@
32573259
[1221l]:https://leetcode.cn/problems/split-a-string-in-balanced-strings/
32583260
[1222l]:https://leetcode.cn/problems/queens-that-can-attack-the-king/
32593261
[1223l]:https://leetcode.cn/problems/dice-roll-simulation/
3262+
[1224l]:https://leetcode.cn/problems/maximum-equal-frequency/
32603263
[1232l]:https://leetcode.cn/problems/check-if-it-is-a-straight-line/
32613264
[1233l]:https://leetcode.cn/problems/remove-sub-folders-from-the-filesystem/
32623265
[1234l]:https://leetcode.cn/problems/replace-the-substring-for-balanced-string/

0 commit comments

Comments
 (0)