Skip to content

Commit 8f169c7

Browse files
committed
Runtime: 775 ms (Top 27.44%) | Memory: 140.3 MB (Top 88.77%)
1 parent 4c63b43 commit 8f169c7

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,76 @@
1+
// Runtime: 775 ms (Top 27.44%) | Memory: 140.3 MB (Top 88.77%)
12
class Solution {
23
public:
34
int minimumOperations(vector<int>& nums) {
4-
5+
56
int totalEven = 0, totalOdd = 0;
6-
7+
78
unordered_map<int,int> mapEven, mapOdd;
8-
9+
910
for(int i=0;i<nums.size();i++) {
1011
if(i%2==0) {
1112
totalEven++;
1213
mapEven[nums[i]]++;
1314
}
14-
15+
1516
else {
1617
totalOdd++;
1718
mapOdd[nums[i]]++;
1819
}
1920
}
20-
21-
21+
2222
int firstEvenCount = 0, firstEven = 0;
2323
int secondEvenCount = 0, secondEven = 0;
24-
24+
2525
for(auto it=mapEven.begin();it!=mapEven.end();it++) {
2626
int num = it->first;
2727
int count = it->second;
28-
28+
2929
if(count>=firstEvenCount) {
3030
secondEvenCount = firstEvenCount;
3131
secondEven = firstEven;
3232
firstEvenCount = count;
3333
firstEven = num;
3434
}
35-
35+
3636
else if(count >= secondEvenCount) {
3737
secondEvenCount = count;
3838
secondEven = num;
3939
}
4040
}
41-
42-
41+
4342
int firstOddCount = 0, firstOdd = 0;
4443
int secondOddCount = 0, secondOdd = 0;
45-
46-
44+
4745
for(auto it=mapOdd.begin();it!=mapOdd.end();it++) {
4846
int num = it->first;
4947
int count = it->second;
50-
48+
5149
if(count>=firstOddCount) {
5250
secondOddCount = firstOddCount;
5351
secondOdd = firstOdd;
5452
firstOddCount = count;
5553
firstOdd = num;
5654
}
57-
55+
5856
else if(count>=secondOddCount) {
5957
secondOddCount = count;
6058
secondOdd = num;
6159
}
6260
}
63-
61+
6462
int operationsEven = 0, operationsOdd = 0;
65-
66-
63+
6764
operationsEven = totalEven - firstEvenCount;
68-
65+
6966
if(firstEven!=firstOdd) operationsEven += (totalOdd - firstOddCount);
7067
else operationsEven += (totalOdd - secondOddCount);
71-
72-
68+
7369
operationsOdd = totalOdd - firstOddCount;
7470
if(firstOdd!=firstEven) operationsOdd += (totalEven - firstEvenCount);
7571
else operationsOdd += (totalEven - secondEvenCount);
76-
77-
72+
7873
return min(operationsEven, operationsOdd);
79-
74+
8075
}
81-
};
76+
};

0 commit comments

Comments
 (0)