This was a "Hard" question so I used the editorial.

In this approach, I iterate through the array and flip the first encountered '0' along with the next two elements. This ensures I always maximize the number of '1's as it moves forward. If any '0' remains in the last two positions, it is impossible to make all elements '1'.
class Solution:
def minOperations(self, nums: List[int]) -> int:
I use a sliding window approach with bitwise operations to find the longest nice subarray. The key observation is that a valid subarray must have all elements with unique bits (bitwise AND must be 0). I expand the right pointer and shrink the left pointer when a conflict arises.
class Solution:
def longestNiceSubarray(self, nums: List[int]) -> int:
I use a frequency counter to count the occurrences of each number in nums. If every number appears an even number of times, then nums can be divided into valid pairs. Else it's not possible to divide the array into required pairs.
class Solution:
def divideArray(self, nums: List[int]) -> bool:
I use a binary search approach to determine the minimum time needed to repair all cars. The search range is from 1 to the maximum possible repair time, calculated as min(ranks) * cars^2. The helper function canRepairWithinTime(t)
checks if the given time t
allows enough cars to be repaired.
class Solution:
def repairCars(self, ranks: List[int], cars: int) -> int:
For this question, I used a binary search approach to determine the minimum capability the robber needs. The search space is between the smallest and largest house values.
The helper function canRob(capability)
checks if it's possible to rob at least k
houses without stealing from adjacent ones.
I used a binary search approach to determine the maximum number of candies each child can receive. The search range is between 1 and the maximum pile size.
The helper function canAllocate
checks if it's possible to allocate at least k
piles of size mid
.
In this approach, I use a difference array to efficiently apply range updates. Instead of modifying nums at each query, I accumulate updates in the difference array. After processing each query, I apply the updates and check if nums becomes a zero array.
This approach didn't work for some edge cases so i used the editorial to see my mistakes.
In my approach, I iterate through the list and count the number of positive and negative numbers separately. If a number is greater than zero, I increase the positive count. If a number is less than zero, I increase the negative count. Finally, I return the maximum count between positive and negative numbers.
class Solution:
def maximumCount(self, nums: List[int]) -> int:
I use a sliding window approach to count substrings containing at least one 'a', 'b', and 'c'. I expand the window to include characters until all three are present. Then, I count all possible substrings ending at right
and reduce the window from left
.
class Solution:
def numberOfSubstrings(self, s: str) -> int: