반응형

https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string

 

Minimum Changes To Make Alternating Binary String - LeetCode

Can you solve this real interview question? Minimum Changes To Make Alternating Binary String - You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alte

leetcode.com

 

첫번째 문자가 0인 경우와 1인 경우를 가정해서, 두 경우에 대한 연산 횟수 중 더 작은 값을 구해줍니다. 

class Solution {
public:
    int minOperations(string s) {
        int c1 = 0, c2 = 0;
        for(int i=0; i<s.size(); i++) {
            if(i % 2) {
                if(s[i] == '1') c1++;
                else c2++;
            } else {
                if(s[i] == '0') c1++;
                else c2++;
            }
        }
        return min(c1, c2);
    }
};
반응형

+ Recent posts