Skip to content

Commit

Permalink
feat: 使循环数组所有元素相等的最少秒数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 30, 2024
1 parent a841068 commit e4decc0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

### 数组/队列/集合/映射

- [使循环数组所有元素相等的最少秒数](src/array/minimum_seconds_to_equalize_a_circular_array.cpp) [数组, 哈希表]

- LeetCode 2808. 使循环数组所有元素相等的最少秒数 <https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array>

- [最长交替子数组](src/array/longest_alternating_subarray.cpp) [数组, 枚举]

- LeetCode 2765. 最长交替子数组 <https://leetcode.cn/problems/longest-alternating-subarray>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/array/minimum_seconds_to_equalize_a_circular_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 使循环数组所有元素相等的最少秒数
// https://leetcode.cn/problems/minimum-seconds-to-equalize-a-circular-array
// INLINE ../../images/array/minimum_seconds_to_equalize_a_circular_array.jpeg

#include <headers.hpp>

class Solution {
public:
int minimumSeconds(vector<int> &nums) {
unordered_map<int, vector<int>> map;
int n = nums.size(), res = n;
for (int i = 0; i < n; ++i) {
map[nums[i]].push_back(i);
}
for (auto &pos : map) {
int mx = pos.second[0] + n - pos.second.back();
for (int i = 1; i < pos.second.size(); ++i) {
mx = max(mx, pos.second[i] - pos.second[i - 1]);
}
res = min(res, mx / 2);
}
return res;
}
};
32 changes: 32 additions & 0 deletions test/array/minimum_seconds_to_equalize_a_circular_array_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <array/minimum_seconds_to_equalize_a_circular_array.cpp>

TEST(使循环数组所有元素相等的最少秒数, minimumSeconds) {
Solution solution;
// 示例 1:
// 输入:nums = [1,2,1,2]
// 输出:1
// 解释:我们可以在 1 秒内将数组变成相等元素:
// - 第 1 秒,将每个位置的元素分别变为 [nums[3],nums[1],nums[3],nums[3]]
// 。变化后,nums = [2,2,2,2] 。 1 秒是将数组变成相等元素所需要的最少秒数。
vector<int> nums = {1, 2, 1, 2};
EXPECT_EQ(solution.minimumSeconds(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 秒是将数组变成相等元素所需要的最少秒数。
nums = {2, 1, 3, 3, 2};
EXPECT_EQ(solution.minimumSeconds(nums), 2);

// 示例 3:
// 输入:nums = [5,5,5,5]
// 输出:0
// 解释:不需要执行任何操作,因为一开始数组中的元素已经全部相等。
nums = {5, 5, 5, 5};
EXPECT_EQ(solution.minimumSeconds(nums), 0);
}
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2024-01-29 10:39:09
// 执行编译时间:2024-01-30 10:07:21
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down

0 comments on commit e4decc0

Please sign in to comment.