Skip to content

Latest commit

 

History

History
19 lines (14 loc) · 702 Bytes

1758. 生成交替二进制字符串的最少操作数.md

File metadata and controls

19 lines (14 loc) · 702 Bytes
  • 动态规划
function minOperations(s: string): number {
    let zeroCount: number = 0, // 从左到右把当前位置变为0,累计需要变动多少次呢?
        oneCount: number = 0; // 从左到右把当前位置变为1,累计需要变动多少次呢?

    for (let i = 0; i < s.length; ++i) {
        const [lastOneCount, lastZeroCount]: [number, number] = [oneCount, zeroCount];
        zeroCount = +(s[i] === '1') + lastOneCount;
        oneCount = +(s[i] === '0') + lastZeroCount;
    }

    return Math.min(zeroCount, oneCount);
};