leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 0139196197967243a515de3c3eb7eb89d8dd09d1
parent ebdf49c4de003d3de6feb752c630f2c1c719d1e3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon, 24 Jun 2024 14:27:54 +0200

Daily Problem

Diffstat:
AProblems/0995.cpp | 19+++++++++++++++++++
MREADME.md | 1+
2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/Problems/0995.cpp b/Problems/0995.cpp @@ -0,0 +1,19 @@ +class Solution { + public: + int minKBitFlips(vector<int> &nums, int k) const { + const int n = size(nums); + int res = 0, flips = 0; + + for (int i = 0; i < n; i++) { + if (nums[i] == flips % 2) { + if (i > n - k) return -1; + nums[i] -= 2, res++, flips++; + } + + const int prev = i - k + 1; + if (i >= k - 1 && nums[prev] < 0) nums[prev] += 2, flips--; + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -612,6 +612,7 @@ for solving problems. | 0992 | Hard | [Subarrays with K Different Integers](Problems/0992.cpp) | | 0993 | Easy | [Cousins in Binary Tree](Problems/0993.cpp) | | 0994 | Medium | [Rotting Oranges](Problems/0994.cpp) | +| 0995 | Hard | [Minimum Number of K Consecutive Bit Flips](Problems/0995.cpp) | | 0997 | Easy | [Find the Town Judge](Problems/0997.cpp) | | 0998 | Medium | [Maximum Binary Tree II](Problems/0998.cpp) | | 1002 | Easy | [Find Common Characters](Problems/1002.cpp) |