반응형

https://leetcode.com/problems/maximum-score-after-splitting-a-string/description/

 

Maximum Score After Splitting a String - LeetCode

Can you solve this real interview question? Maximum Score After Splitting a String - Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring). The scor

leetcode.com

 

1의 개수를 구해두고, s를 다시 순회하며 각 지점에서의 0과 1의 개수를 구하여 점수를 계산해줍니다. 

class Solution {
public:
    int maxScore(string s) {
        int oc = 0;
        for(int i=0; i<s.size(); i++) {
            if(s[i] == '1') oc++;
        }
        
        int res = 0, zc = 0;
        for(int i=0; i<s.size(); i++) {
            if(s[i] == '0') zc++;
            else oc--;
            res = max(res, zc + oc);
        }
        return res;
    }
};
반응형

+ Recent posts