Minimum Swaps to Group All 1's Together 1 - In an array

Problem A swap is defined as taking two distinct positions in an array and swapping the values in them. Given a binary array nums, return the minimum number of swaps required to group all 1’s present in the array together at any location. Examples Example 1: Input: nums = [1,0,1,0,1] Output: 1 Explanation: There are 3 ways to group all 1's together: [1,{0},1,0,{1}] => [1,1,1,0,0] using 1 swap. [1,0,1,{0},{1}] => [{1},{0},1,1,0] => [0,1,1,1,0] using 1 swaps. [{1},0,1,{0},1] => [0,0,1,1,1] using 1 swap. The minimum is 1. ...

Minimum Swaps to Group All 1's Together 2 - In circular array

Problem A swap is defined as taking two distinct positions in an array and swapping the values in them. A circular array is defined as an array where we consider the first element and the last element to be adjacent. Given a binary circular array nums, return the minimum number of swaps required to group all 1’s present in the array together at any location. Examples Example 1: ...

Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Problem

Problem Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit. Examples Example 1: Input: nums = [8,2,4,7], limit = 4 Output: 2 Explanation: All subarrays are: [8] with maximum absolute diff |8-8| = 0 <= 4. [8,2] with maximum absolute diff |8-2| = 6 > 4. [8,2,4] with maximum absolute diff |8-2| = 6 > 4. [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4. [2] with maximum absolute diff |2-2| = 0 <= 4. [2,4] with maximum absolute diff |2-4| = 2 <= 4. [2,4,7] with maximum absolute diff |2-7| = 5 > 4. [4] with maximum absolute diff |4-4| = 0 <= 4. [4,7] with maximum absolute diff |4-7| = 3 <= 4. [7] with maximum absolute diff |7-7| = 0 <= 4. Therefore, the size of the longest subarray is 2. ...

Count Number of Nice Subarrays Problem

Problem Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it. Return the number of nice sub-arrays. Examples Example 1: Input: nums = [1,1,2,1,1], k = 3 Output: 2 Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1]. ...

Grumpy Bookstore Owner Problem

Problem There is a bookstore owner that has a store open for n minutes. Every minute, some number of customers enter the store. You are given an integer array customers of length n where customers[i] is the number of the customer that enters the store at the start of the ith minute and all those customers leave after the end of that minute. On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where grumpy[i] is 1 if the bookstore owner is grumpy during the ith minute, and is 0 otherwise. ...

Get Equal Substrings Within Budget

Problem You are given two strings s and t of the same length and an integer maxCost. You want to change s to t. Changing the ith character of s to ith character of t costs |s[i] - t[i]| (i.e., the absolute difference between the ASCII values of the characters). Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost. If there is no substring from s that can be changed to its corresponding substring from t, return 0. ...

Largest Local Values in a Matrix

Problem You are given an n x n integer matrix grid. Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that: maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1. In other words, we want to find the largest value in every contiguous 3 x 3 matrix in grid. ...

Contains Duplicate 2 - within k distance

Problem Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. Examples Example 1: Input nums = [1,2,3,1], k = 3 Output: true ...

Maximum Subarray Sum

Problem Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Examples Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. ...

Longest Substring with At Most K Distinct Characters Problem

Problem Given a string S, find the length of the longest substring T that contains at most k distinct characters. Examples Example 1: Input: S = "eceba" and k = 3 Output: 4 Explanation: T = "eceb" Example 2: Input: S = "WORLD" and k = 4 Output: 4 Explanation: T = "WORL" or "ORLD" ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy