Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • These problems are sliding windows but solve by 2 passes
  • 992. Subarrays with K different Integers
  • 930. Binary Subarrays with Sum
  • 1248. Count Number of Nice Subarrays

Was this helpful?

  1. 2. Array and Numbers
  2. Sliding Window

Sliding Window At Most Problem

PreviousSliding WindowNextWord Break

Last updated 4 years ago

Was this helpful?

These problems are sliding windows but solve by 2 passes

992. Subarrays with K different Integers

Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A good if the number of different integers in that subarray is exactly K.

(For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.)

Return the number of good subarrays of A

def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
    return self.atMost(A, K) - self.atMost(A, K-1)

def atMost(self, A, K):
    i = 0
    record = collections.defaultdict(int)
    count = 0
    n = len(A)
    
    for j in range(n):
        if record[A[j]] == 0:
            K-=1
        record[A[j]] += 1
        
        while K < 0:
            record[A[i]] -=1
            if record[A[i]] == 0:
                K+=1
            i+=1
        count += j-i+1    
    return count

930. Binary Subarrays with Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S?

def numSubarraysWithSum(self, A: List[int], S: int) -> int:
    return self.atMost(A, S) - self.atMost(A, S-1)

def atMost(self, A, S):
    # [0,0,0,0,0], 0
    if S < 0: return 0
    i = 0
    n = len(A)
    count = 0
    
    for j in range(n):
        S -= A[j]
        while S < 0:
            S += A[i] 
            i+=1
        count += j-i+1   
        
    return count

1248. Count Number of Nice Subarrays

Given an array of integers nums and an integer k. A subarray is called nice if there are k odd numbers on it.

Return the number of nice sub-arrays.

def numberOfSubarrays(self, nums: List[int], k: int) -> int:
    return self.atMost(nums, k) - self.atMost(nums, k-1)

def atMost(self, nums, k):
    i = 0
    count = 0
    
    for j in range(len(nums)):
        k -= 1 if nums[j]%2 else 0
        while k < 0:
            k += 1 if nums[i]%2 else 0
            i += 1
        count += j-i+1   
    return count
See Lee's posts