Skip to content

Latest commit

 

History

History
41 lines (35 loc) · 889 Bytes

2379.得到-k-个黑块的最少涂色次数.md

File metadata and controls

41 lines (35 loc) · 889 Bytes

2379.得到-k-个黑块的最少涂色次数

/*
 * @lc app=leetcode.cn id=2379 lang=typescript
 *
 * [2379] 得到 K 个黑块的最少涂色次数
 */

// @lc code=start
function minimumRecolors(blocks: string, k: number): number {}
// @lc code=end

解法 1: 滑动窗口

function minimumRecolors(blocks: string, k: number): number {
  let res = Infinity,
    cnt = 0
  for (let i = 0; i < blocks.length; i++) {
    if (i >= k) {
      res = Math.min(res, cnt)
      if (blocks[i - k] === 'W') cnt--
    }
    if (blocks[i] === 'W') cnt++
  }
  return Math.min(res, cnt)
}

Case

test.each([
  { input: { blocks: 'WBBWWBBWBW', k: 7 }, output: 3 },
  { input: { blocks: 'WBWBBBW', k: 2 }, output: 0 },
])('input: blocks = $input.blocks, k = $input.k', ({ input: { blocks, k }, output }) => {
  expect(minimumRecolors(blocks, k)).toEqual(output)
})