Snippets Collections
def dutch_flag_sort(arr):
  #set low and high pointer 
  low, high = 0, len(arr) - 1 

  i = 0 

  #iterate through arr 
  while i <= high: 
    #if arr[i] is 0, set it before low via swapping 
    #elif arr[i] is 2, set it after high via swapping 
    #elif arr[i] is 1, it will naturally fall in the middle 
    if arr[i] == 0: 
      arr[i], arr[low] = arr[low], arr[i] 
      low += 1 
      i += 1 
    elif arr[i] == 1: 
      i += 1 
    else: #arr[i] == 2: 
      arr[i], arr[high] = arr[high], arr[i] 
      high -= 1 
    
  
  return   
def find_subarrays(arr, target):
  def Remove(lst):
    return ([list(i) for i in {*[tuple(sorted(i)) for i in lst]}])
  result = []
  #iterate through arr (up to len(arr) - 1) 
  for i in range(len(arr) - 1): 
      first = arr[i]
      second = arr[i + 1] 
      product = first * second 
      if first < target and first not in result: 
          result.append([first]) 
      if second < target and second not in result: 
          result.append([second])
      if product < target: 
        result.append([first, second]) 
      else: 
          continue 
    

  
  return Remove(result)  


#alternate solution that avoids making duplicate sublists in the first place 
from collections import deque


def find_subarrays(arr, target):
  result = []
  product = 1
  left = 0
  for right in range(len(arr)):
    product *= arr[right]
    while (product >= target and left <= right):
      product /= arr[left]
      left += 1
    # since the product of all numbers from left to right is less than the target therefore,
    # all subarrays from left to right will have a product less than the target too; to avoid
    # duplicates, we will start with a subarray containing only arr[right] and then extend it
    temp_list = deque()
    for i in range(right, left-1, -1):
      temp_list.appendleft(arr[i])
      result.append(list(temp_list))
  return result


def main():
  print(find_subarrays([2, 5, 3, 10], 30))
  print(find_subarrays([8, 2, 6, 5], 50))


main()
def length_of_longest_substring(arr, k):
  #set start, maxLength, maxRepeat 
  start, maxLength, maxRepeat = 0, 0, 0 
  #set frequencyDict 
  frequencyDict = {} 
  
  #iterate through arr
  for end in range(len(arr)): 
    #add to frequencyDict 
    right = arr[end]
    if right not in frequencyDict: 
        frequencyDict[right] = 1 
    else: 
        frequencyDict[right] += 1 
    #find maxRepeat 
    maxRepeat = max(maxRepeat, frequencyDict[right]) 
    
    #evaluate window 
    if (end - start + 1 - maxRepeat) > k: 
        #reduce window by 1 and adjust frequency map 
        left = arr[start] 
        frequencyDict[left] -= 1 
        start += 1 
    
    #check maxLength 
    maxLength = max(maxLength, end - start + 1) 

  return maxLength

length_of_longest_substring([0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1], 3)

star

Tue Sep 27 2022 00:07:31 GMT+0000 (Coordinated Universal Time) https://www.educative.io/courses/grokking-the-coding-interview/RMBxV6jz6Q0

#python #grokking
star

Mon Sep 26 2022 23:46:41 GMT+0000 (Coordinated Universal Time) https://www.educative.io/courses/grokking-the-coding-interview/RMV1GV1yPYz

#python #grokking

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension