From ad6f4919808188b173556fdfdee08bfc022ca965 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Sun, 25 Jun 2023 11:06:00 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.2379 --- .../README.md | 32 ++++++++++++++++++- .../README_EN.md | 32 ++++++++++++++++++- .../Solution.php | 26 +++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.php diff --git a/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md index 140d05f50fa7e..05c0591f67dbd 100644 --- a/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md +++ b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README.md @@ -209,10 +209,40 @@ int minimumRecolors(char *blocks, int k) { } ``` -### **...** +### **PHP** +```php +class Solution { + /** + * @param String $blocks + * @param Integer $k + * @return Integer + */ + function minimumRecolors($blocks, $k) { + $cnt = 0; + for ($i = 0; $i < $k; $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + } + $min = $cnt; + for ($i = $k; $i < strlen($blocks); $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + if ($blocks[$i - $k] === 'W') { + $cnt--; + } + $min = min($min, $cnt); + } + return $min; + } +} ``` +### **...** + +``` ``` diff --git a/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md index 8592ed6c96178..51be88d55f0e1 100644 --- a/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md +++ b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/README_EN.md @@ -189,10 +189,40 @@ int minimumRecolors(char *blocks, int k) { } ``` -### **...** +### **PHP** +```php +class Solution { + /** + * @param String $blocks + * @param Integer $k + * @return Integer + */ + function minimumRecolors($blocks, $k) { + $cnt = 0; + for ($i = 0; $i < $k; $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + } + $min = $cnt; + for ($i = $k; $i < strlen($blocks); $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + if ($blocks[$i - $k] === 'W') { + $cnt--; + } + $min = min($min, $cnt); + } + return $min; + } +} ``` +### **...** + +``` ``` diff --git a/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.php b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.php new file mode 100644 index 0000000000000..0b664e028b904 --- /dev/null +++ b/solution/2300-2399/2379.Minimum Recolors to Get K Consecutive Black Blocks/Solution.php @@ -0,0 +1,26 @@ +class Solution { + /** + * @param String $blocks + * @param Integer $k + * @return Integer + */ + function minimumRecolors($blocks, $k) { + $cnt = 0; + for ($i = 0; $i < $k; $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + } + $min = $cnt; + for ($i = $k; $i < strlen($blocks); $i++) { + if ($blocks[$i] === 'W') { + $cnt++; + } + if ($blocks[$i - $k] === 'W') { + $cnt--; + } + $min = min($min, $cnt); + } + return $min; + } +}