Skip to content

Latest commit

 

History

History
33 lines (22 loc) · 826 Bytes

1578. 避免重复字母的最小删除成本.md

File metadata and controls

33 lines (22 loc) · 826 Bytes
  • 滑动窗口
function minCost(s: string, cost: number[]): number {

    let result: number = 0,
        rightIndex: number = 1;

    while (rightIndex < s.length) {
        let maxCost: number = 0,
            minCost: number = 0;

        while (rightIndex < s.length && s[rightIndex - 1] === s[rightIndex]) {
            maxCost = Math.max(maxCost, cost[rightIndex - 1], cost[rightIndex]);

            minCost && (minCost -= cost[rightIndex - 1]);
            minCost += cost[rightIndex - 1] + cost[rightIndex];

            ++rightIndex;
        }
        
        minCost -= maxCost;
        result += minCost;
        ++rightIndex;
    }

    return result;
};