-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
61 additions
and
1 deletion.
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
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
24
src/array/minimum_seconds_to_equalize_a_circular_array.cpp
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,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
32
test/array/minimum_seconds_to_equalize_a_circular_array_test.cpp
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,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); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-29 10:39:09 | ||
// 执行编译时间:2024-01-30 10:07:21 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|