Untitled

unknown
plain_text
a month ago
1.7 kB
0
Indexable
Never
```class Solution {
public:
int helper(vector<int>& nums, int b1, int b2){
int n = nums.size();
int non1 = 0, non2 = 0, non3 = 0;
for(int i = 0; i <= b1; i++){
if(nums[i] != 1)
non1++;
}
for(int i = b1+1; i <= b2; i++){
if(nums[i] != 2)
non2++;
}
for(int i = b2+1; i < n; i++){
if(nums[i] != 3)
non3++;
}
return non1+non2+non3;
}
int helper2(vector<int>& nums, int b, int n1, int n2){
int n = nums.size();
int tmp1 = 0, tmp2 = 0;
for(int i = 0; i <= b; i++){
if(nums[i] != n1)
tmp1++;
}
for(int i = b+1; i < n; i++){
if(nums[i] != n2)
tmp2++;
}
return tmp1+tmp2;
}
int minimumOperations(vector<int>& nums){
int n = nums.size();
if(n == 1)
return 0;
int ans = n;
for(int b1 = 0; b1 < n; b1++){
for(int b2 = 0; b2 < n; b2++){
ans = min(ans, helper(nums, b1, b2));
}
}
for(int i = 0; i < n; i++){
ans = min(ans, helper2(nums, i, 1, 2));
}
for(int i = 0; i < n; i++){
ans = min(ans, helper2(nums, i, 2, 3));
}
for(int i = 0; i < n; i++){
ans = min(ans, helper2(nums, i, 1, 3));
}
int non1 = 0, non2 = 0, non3 = 0;
for(int i = 0; i < n; i++){
if(nums[i] != 1)
non1++;
if(nums[i] != 2)
non2++;
if(nums[i] != 3)
non3++;
}
return min(ans, min(min(non1, non2), non3));
}
};```