当前位置:网站首页>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 <= 1051 <= 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).
边栏推荐
- Asynchronous task Whenall timeout - Async task WhenAll with timeout
- [online chat] the original wechat applet can also reply to Facebook homepage messages!
- Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
- 18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
- 18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
- 亲测可用fiddler手机抓包配置代理后没有网络
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
- FFmpeg学习——核心模块
- Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
猜你喜欢

【DesignMode】装饰者模式(Decorator pattern)

MySql——CRUD

软件测试工程师必会的银行存款业务,你了解多少?
![Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]

总结了 800多个 Kubectl 别名,再也不怕记不住命令了!

There is no network after configuring the agent by capturing packets with Fiddler mobile phones

Senparc.Weixin.Sample.MP源码剖析

权限问题:source .bash_profile permission denied

Upgrade openssl-1.1.1p for openssl-1.0.2k

选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
随机推荐
Open3D 点云随机添加噪声
单商户V4.4,初心未变,实力依旧!
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
What are the functions of Yunna fixed assets management system?
Make a short video clip number of we media film and television. Where can I download the material?
How much do you know about the bank deposit business that software test engineers must know?
Configuring OSPF GR features for Huawei devices
Senparc.Weixin.Sample.MP源码剖析
wx.getLocation(Object object)申请方法,最新版
JS 这次真的可以禁止常量修改了!
CloudCompare&PCL 点云随机添加噪声
用列錶初始化你的vector&&initializer_list簡介
Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematical model Lotka Volterra
云呐|公司固定资产管理系统有哪些?
Key structure of ffmpeg -- AVCodecContext
Mysql - CRUD
PV静态创建和动态创建