Minimum Number of K Consecutive Bit Flips - Leetcode 995 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation 1
9:18 - Coding Explanation 1
12:13 - Drawing Explanation 2
16:09 - Coding Explanation 2

leetcode 995

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

To everyone asking *how* to arrive at the intuition of greedily trying to flip any "0" bits, this was basically my thought process that led me there:
1. Since a flipping operation is reversible, that must mean that if I can go from "nums" to all 1s with flips, it must also be possible to go from all 1s to "nums".
2. If we ASSUME the array starts off as being all 1s, that would mean that the first bit that is 0 would HAVE to be the boundry of a flipping location. Since, those all 1's aren't going to sponaneously go from their initial state of "1" to "0" unless it is the start of a flip.
3. This assumption can pretty much apply recursively, where we can assume that any unexpected transition from "1" to "0" represents another flip.
4. If we reach the end and there are still flips not accounted for, that means there were flip boundaries at intervals other than "k" and that we didn't start with all 1s to begin with.

grantpeterson
Автор

i know you made it pretty simple, but coming up with this logic of mod is basically made me go into existential crisis and question my reality

betabias
Автор

Solved it on my own Beats(94%). Finally seeing the fruit of almost year of practise.

class Solution:
def minKBitFlips(self, nums: List[int], k: int) -> int:
n = len(nums)
flips = [0]*n
flipSum = 0
count = 0

# Count the number of flips efficiently
for i in range(n - k + 1):
if (nums[i] == 0 and (flipSum == 0 or flipSum % 2 == 0)) or (nums[i] == 1 and flipSum % 2 == 1):
count += 1
flips[i] = 1
flipSum += 1
if i + 1 >= k:
flipSum -= flips[i - k + 1]

# Check if last k character is 1
for i in range(n - k + 1, n):
if (nums[i] == 0 and (flipSum == 0 or flipSum % 2 == 0)) or (nums[i] == 1 and flipSum % 2 == 1):
return -1
flipSum -= flips[i - k + 1]

return count

freecourseplatformenglish
Автор

holy cow as soon as i understood how to use queue i solved it instantly on my own. Thanks man.

prajwalchoudhary
Автор

Tell me honestly how do you come up with approach?
I can go waste whole day and still can't figure out the solution

RohanKumar-qy
Автор

First try with brute force and get TLE :D

luuduytoan
Автор

very similar to biweekly problems this week.

chrischika
Автор

Why use while? you can simply use if statement to pop the left element.

srprawinraja
Автор

Fun fact: in the constant space solution, you don't actually need to check if `i-k >=0`. If it ends up being negative, it'll be a lookup from end of the array (i.e. nums[-2]). For all k values, since we haven't changed any of the numbers in the indexes ahead, it'll still do what we want it to do (which is catch any changes in the past only when it sees a 2. It might still be good practice to ensure you are not going out of bounds though...

aminfardi
Автор

Hi Neetcode, I’ve been focused on learning algorithms and patterns for about a year now, and normally when I see questions I could usually figure out what pattern or algorithm to use. So my question is how do I get good at solving these kind of questions cause they don’t follow the popular algorithms or patterns

chisomedoka
Автор

awesome, u changed this Ques from Hard to Easy,
this is why LeetCode ques in interview seem unfair sometimes,
if u know u know

jaatharsh
Автор

Waaoooo great explanation, is this a pattern? i haven't done any bit manipulation questions, though I knew I can use sliding window here but still just didn't know how....

Aryan-rbyk
Автор

If you are still confused, I highly recommend to try with very similar problems that have medium level - 3191. and 3192.

qwertyluk
Автор

Everything is nice nav. but one suggestion, while u provide the code, can u give a side-by-side animation on what is happening in the list or the code that runs..that would be really helpful.thank you

loke_mc
Автор

I understand how the solution works. I just don't understand why the greedy approach is the best solution here. How can a person who sees this problem guarantee that the greedy approach is the best way to go?

chrisboumalhab
Автор

Very Great Explanation. Thank you so much !!!

sunshineandrainbow
Автор

remember: if your love makes you weak its not love .
love only makes your partner stronger and as you

priyanshiAIML
Автор

Solved it but with O(k) space! Sadly, unable to solve it using O(1) space!

satyamjha
Автор

solution in java

class Solution {
public int minKBitFlips(int[] nums, int k){

Queue<Integer> q = new LinkedList<>();

int i = 0;
int n = nums.length;
int oprs = 0;

while( i < n){

if( i > ( n - k) ){
if( (nums[i] + q.size()%2)%2 == 0 ) return -1;
}
else {
if( (nums[i] + q.size()%2)%2 == 0 ){
oprs++;
q.add(i);
}
}


if( !q.isEmpty() && q.peek() == (i - k + 1) ){
q.poll();
}

i++;

}

return oprs;
}
}

AnandKumar-kzls
Автор

Why did you create multiple channels? Why are you no longer uploading video in your old channel?

aseshshrestha
join shbcf.ru