-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 使循环数组所有元素相等的最少秒数 | ||
// https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array | ||
// INLINE ../../images/array/minimum_seconds_to_equalize_a_circular_array.jpeg | ||
|
||
pub struct Solution; | ||
use std::collections::HashMap; | ||
|
||
impl Solution { | ||
pub fn minimum_seconds(nums: Vec<i32>) -> i32 { | ||
let mut map: HashMap<i32, Vec<usize>> = HashMap::new(); | ||
let n = nums.len(); | ||
let mut res = n; | ||
|
||
for (i, num) in nums.iter().enumerate() { | ||
map.entry(*num).or_insert(Vec::new()).push(i); | ||
} | ||
|
||
for pos in map.values() { | ||
let mut mx = pos[0] + n - pos[pos.len() - 1]; | ||
for i in 1..pos.len() { | ||
mx = mx.max(pos[i] - pos[i - 1]); | ||
} | ||
res = res.min(mx / 2); | ||
} | ||
|
||
res as i32 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/array/minimum_seconds_to_equalize_a_circular_array_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use rust_practice::array::minimum_seconds_to_equalize_a_circular_array::Solution; | ||
|
||
#[test] | ||
fn minimum_seconds() { | ||
// 示例 1: | ||
// 输入:nums = [1,2,1,2] | ||
// 输出:1 | ||
// 解释:我们可以在 1 秒内将数组变成相等元素: | ||
// - 第 1 秒,将每个位置的元素分别变为 [nums[3],nums[1],nums[3],nums[3]] 。变化后,nums = [2,2,2,2] 。 | ||
// 1 秒是将数组变成相等元素所需要的最少秒数。 | ||
let nums = vec![1, 2, 1, 2]; | ||
assert_eq!(Solution::minimum_seconds(nums), 1); | ||
|
||
// 示例 2: | ||
// 输入:nums = [2,1,3,3,2] | ||
// 输出:2 | ||
// 解释:我们可以在 2 秒内将数组变成相等元素: | ||
// - 第 1 秒,将每个位置的元素分别变为 [nums[0],nums[2],nums[2],nums[2],nums[3]] 。变化后,nums = [2,3,3,3,3] 。 | ||
// - 第 2 秒,将每个位置的元素分别变为 [nums[1],nums[1],nums[2],nums[3],nums[4]] 。变化后,nums = [3,3,3,3,3] 。 | ||
// 2 秒是将数组变成相等元素所需要的最少秒数。 | ||
let nums = vec![2, 1, 3, 3, 2]; | ||
assert_eq!(Solution::minimum_seconds(nums), 2); | ||
|
||
// 示例 3: | ||
// 输入:nums = [5,5,5,5] | ||
// 输出:0 | ||
// 解释:不需要执行任何操作,因为一开始数组中的元素已经全部相等。 | ||
let nums = vec![5, 5, 5, 5]; | ||
assert_eq!(Solution::minimum_seconds(nums), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters