- 动态规划
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);
};