Home > database >  Why am I getting keyError on this?
Why am I getting keyError on this?

Time:07-06

The problem is: Given an array containing 0s and 1s, if you are allowed to replace no more than ‘k’ 0s with 1s, find the length of the longest contiguous subarray having all 1s.

Input: Array=[0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], k=2 Output: 6 Explanation: Replace the '0' at index 5 and 8 to have the longest contiguous subarray of 1s having length 6.

def length_of_longest_substring(arr, k):
    '''
    Create a hashmap that records the values of 0 and 1, initialize them to 0. Do a sliding 
    window.
    WHILE the frequency of 0 is greater than k, subtract arr[windowStart] from HM and then 
    increment 
    wS.
    Use the max function to record longest substring length. Return that.
    '''

    hm = {'0': '0', '1': '0'}
    (windowStart, longest) = (0, 0)
    for windowEnd in range(len(arr)):
        right = arr[windowEnd]
        hm[right] = hm.get(right, 0)   1
        while hm["0"] > k:
            hm[arr[windowStart]] -= 1
            windowStart  = 1
        longest = max(longest, windowEnd - windowStart   1)
    return longest


def main():
    print(length_of_longest_substring([1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2)) 
    #Return 6
    print(length_of_longest_substring([1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3))
    #Return 9
main()

I am getting error with "while hm["0"] > k:" it says

File "main.py", line 12, in length_of_longest_substring
    while hm["0"] > k:
KeyError: 0

It works if I replace both starting indices with 0. I tried the .get function aswell. I did hm.get("0"), same error. I want the while loop to count the VALUES of 0. How can I achieve that? Thank you in advance, all is very much appreciated.

CodePudding user response:

I think you got the logic wrong in your for loop. I have modified it and the error got fixed.

# While the frequency of 0 is greater than k, subtract arr[windowStart] from hm and then increment windowStart.
if hm[0] > k:
    hm[arr[windowStart]] -= 1
    windowStart  = 1

# Record longest substring length.
longest = max(longest, windowEnd - windowStart   1)

# Increment the frequency of arr[windowEnd] in hm.
hm[arr[windowEnd]]  = 1

Output:

6
10

CodePudding user response:

You are comparing integer and STR convert the STR to int

Note: I am not solving the problem but resolving the error as requested by the question.

def length_of_longest_substring(arr, k):
    '''
    Create a hashmap that records the values of 0 and 1, initialize them to 0. Do a sliding 
    window.
    WHILE the frequency of 0 is greater than k, subtract arr[windowStart] from HM and then 
    increment 
    wS.
    Use the max function to record longest substring length. Return that.
    '''

    hm = {'0': '0', '1': '0'}
    (windowStart, longest) = (0, 0)
    for windowEnd in range(len(arr)):
        right = arr[windowEnd]
        hm[right] = hm.get(right, 0)   1
        while (((int)(hm["0"])) > k):
            hm[arr[windowStart]] -= 1
            windowStart  = 1
        longest = max(longest, windowEnd - windowStart   1)
    return longest


def main():
    print(length_of_longest_substring([1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1], 2)) 
    #Return 6
    print(length_of_longest_substring([1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3))
    #Return 9
main()
  • Related