当前位置:网站首页>LeetCode 6005. The minimum operand to make an array an alternating array
LeetCode 6005. The minimum operand to make an array an alternating array
2022-07-06 00:09:00 【Daylight629】
6005. The minimum number of operands to make an array an alternating array
I'll give you a subscript from 0 Starting array nums
, The array consists of n
It's made up of four positive integers .
If the following conditions are met , The array nums
It's a Alternating array :
nums[i - 2] == nums[i]
, among2 <= i <= n - 1
.nums[i - 1] != nums[i]
, among1 <= i <= n - 1
.
In one step operation in , You can choose the subscript i
And will nums[i]
change by any Positive integer .
Returns the that makes the array an alternating array The minimum number of operands .
Example 1:
Input :nums = [3,1,3,2,4,3]
Output :3
explain :
One way to turn an array into an alternating array is to convert the array to [3,1,3,1,3,1] .
under these circumstances , The operands are 3 .
Can prove that , Operands are less than 3 Under the circumstances , Cannot make an array into an alternating array .
Example 2:
Input :nums = [1,2,2,2,2]
Output :2
explain :
One way to turn an array into an alternating array is to convert the array to [1,2,1,2,1].
under these circumstances , The operands are 2 .
Be careful , Array cannot be converted to [2,2,2,2,2] . Because in this case ,nums[0] == nums[1], The condition of alternating array is not satisfied .
Tips :
1 <= nums.length <= 105
1 <= nums[i] <= 105
Two 、 Method 1
Count the most frequent occurrences of , Then find the one that appears the most and the second most , If it's not equal, subtract these , If equal, subtract the maximum sum
class Solution {
public int minimumOperations(int[] nums) {
Map<Integer, Integer> map1 = new HashMap<>();
Map<Integer, Integer> map2 = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if ((i & 1) == 1) {
map1.put(nums[i], map1.getOrDefault(nums[i], 0) + 1);
} else {
map2.put(nums[i], map2.getOrDefault(nums[i], 0) + 1);
}
}
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (Map.Entry<Integer,Integer> entry : map1.entrySet()) {
if (entry.getValue() > a) {
b = a;
a = entry.getValue();
x1 = entry.getKey();
}
else if (entry.getValue() > b) {
b = entry.getValue();
}
}
for (Map.Entry<Integer,Integer> entry : map2.entrySet()) {
if (entry.getValue() > c) {
d = c;
c = entry.getValue();
y1 = entry.getKey();
}
else if (entry.getValue() > d) {
d = entry.getValue();
}
}
if (x1 == y1) {
return nums.length - Math.max(a + d, b + c);
} else {
return nums.length - (a + c);
}
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- Senparc. Weixin. Sample. MP source code analysis
- Mathematical model Lotka Volterra
- XML configuration file (DTD detailed explanation)
- [SQL] SQL expansion languages of mainstream databases (T-SQL, pl/sql, pl/pgsql)
- Huawei equipment is configured with OSPF and BFD linkage
- C reflection and type
- Configuring OSPF GR features for Huawei devices
- Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
- What are Yunna's fixed asset management systems?
- Transport layer protocol ----- UDP protocol
猜你喜欢
About the slmgr command
Open source CRM customer relationship system management system source code, free sharing
FFT learning notes (I think it is detailed)
Wechat applet -- wxml template syntax (with notes)
【DesignMode】组合模式(composite mode)
妙才周刊 - 8
Key structure of ffmpeg - avformatcontext
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
Browser local storage
C reflection and type
随机推荐
传输层协议------UDP协议
【GYM 102832H】【模板】Combination Lock(二分图博弈)
转:未来,这样的组织才能扛住风险
Senparc.Weixin.Sample.MP源码剖析
Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
MySql——CRUD
[SQL] SQL expansion languages of mainstream databases (T-SQL, pl/sql, pl/pgsql)
FFMPEG关键结构体——AVCodecContext
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
Transport layer protocol ----- UDP protocol
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
Make a short video clip number of we media film and television. Where can I download the material?
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
提升工作效率工具:SQL批量生成工具思想
VBA fast switching sheet
What is a humble but profitable sideline?
MySQL functions
妙才周刊 - 8
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
Open3D 点云随机添加噪声