From 686fe5069d6bcd401bcc371c6c7acafcc96d3fd0 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Sun, 9 Mar 2025 00:34:14 +0530 Subject: [PATCH] Create 2379-minimum-recolors-to-get-k-consecutive-black-blocks.js Solved minimum-recolors-to-get-k-consecutive-black-blocks --- ...olors-to-get-k-consecutive-black-blocks.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 javascript/2379-minimum-recolors-to-get-k-consecutive-black-blocks.js diff --git a/javascript/2379-minimum-recolors-to-get-k-consecutive-black-blocks.js b/javascript/2379-minimum-recolors-to-get-k-consecutive-black-blocks.js new file mode 100644 index 000000000..e778a11f5 --- /dev/null +++ b/javascript/2379-minimum-recolors-to-get-k-consecutive-black-blocks.js @@ -0,0 +1,31 @@ +/** + * Sliding Window + * Time O(n) | Space O(1) + * https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks + * @param {string} blocks + * @param {number} k + * @return {number} + */ +var minimumRecolors = function(blocks, k) { + + let whiteCount = 0; + for (let i = 0; i < k; i++) { + if (blocks[i] === "W") whiteCount++; + } + + let left = 0; + let right = k-1; + + let min = Infinity; + while (right < blocks.length) { + min = Math.min(min, whiteCount); + + if (blocks[left] === "W") whiteCount--; + left++; + right++; + if (right < blocks.length && blocks[right] === "W") whiteCount++; + } + + return min; +}; +