Sliding Window

Minimum Operations to Reduce X to Zero

February 3, 2024
medium
Sliding Window

Problem Statement # You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. The goal is to exactly reduce x to zero using the minimum number of operations, and return this number. If it is not possible, return -1. Solution Approach # The solution involves finding the longest subarray with the sum equal to totalSum - x (where totalSum is the sum of all elements in nums). ...

Maximum Points You Can Obtain from Cards

January 26, 2024
medium
Sliding Window

Problem Statement # There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have k steps to take cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain. ...

Find All Anagrams in a String

January 21, 2024
medium
Map, Sliding Window

Problem Statement # Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consist of lowercase English letters only and the order of output does not matter. Example: Input: s = “cbaebabacd”, p = “abc” Output: [0, 6] Explanation: The substring with start index = 0 is “cba”, which is an anagram of “abc”. The substring with start index = 6 is “bac”, also an anagram of “abc”. ...

Minimum Swaps to Group All 1s Together

January 11, 2024
medium
Sliding Window

Problem # Given a binary array data, your task is to find the minimum number of swaps required to group all 1’s present in the array together in any place in the array. Example: Input: data = [1, 0, 1, 0, 1] Output: 1 Explanation: One of the ways to group all 1’s together is [1, 1, 1, 0, 0], which can be achieved by 1 swap. Solution Approach # The solution involves using a sliding window approach. ...

Shortest subarray length

January 9, 2024
medium
Sliding Window

Problem # Given an array of integers nums and a target value k, find the length of the shortest subarray whose sum is greater than or equal to k. Solution # Steps: Initialize two pointers, left and right, both at the beginning of the array. Initialize a variable current_sum to 0. Iterate while right is within the bounds of the array: Add nums[right] to current_sum. While current_sum is greater than or equal to k: Update the minimum length if needed: min_length = min(min_length, right - left + 1). ...

Longest Substring Without Repeating Characters

January 1, 2024
medium
Sliding Window

Problem # Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2: Input: s = “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3: Input: s = “pwwkew” Output: 3 Explanation: The answer is “wke”, with the length of 3. ...

Minimum Window Substring

December 27, 2023
medium
Sliding Window, Hash Table

Problem # Given two strings s and t, return the minimum window in s which will contain all the characters in t. If there is no such window in s that covers all characters in t, return the empty string. Solution # The Python function min_window successfully finds the minimum window in the string s that contains all the characters of the string t. In the provided example with s = "ADOBECODEBANC" and t = "ABC", the function returned "BANC" as the smallest substring of s that includes all the characters in t. ...

Sliding Window

December 20, 2023
medium
Sliding Window, heap

Problem # Given an array of integers and a sliding window of size k, find the maximum value in each window. The window slides one position to the right at a time. Solution # import collections def max_sliding_window(nums, k): n = len(nums) if n == 0 or k == 0: return [] deque = collections.deque() result = [-1] * (n - k + 1) for i in range(n): while deque and deque[0] < i - k + 1: deque. ...