Posts

Showing posts from September, 2026

Sliding Window: LC 2024 - Maximize the Confusion of an Exam (Optimized solution)

 Description - Given a binary array nums , you should delete one element from it. Return the size of the longest non-empty subarray containing only 1 's in the resulting array . Return 0 if there is no such subarray.   Example 1: Input: nums = [1,1,0,1] Output: 3 Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's. Example 2: Input: nums = [0,1,1,1,0,1,1,0,1] Output: 5 Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1]. Example 3: Input: nums = [1,1,1] Output: 2 Explanation: You must delete one element. class Solution { public int maxConsecutiveAnswers(String ak, int k) { int i = 0; int[] freq = new int[15]; char[] sc = ak.toCharArray(); for(char c: sc) { ++freq[c-'F']; if(freq[0] > k && freq[14] > k) --freq[sc[i++]-'F']; } return ak.length()...

Sliding window: LC - 904. Fruit Into Baskets - Optimal solution

 It is a great study material for learning the algorithms. It need to understand how they are doing it and space optimization. Problem statement: You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the i th tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow: You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick . Example 1: Input: fruits ...

Leetcode - Presum

 Ref the presum questions 325. Maximum Size Subarray Sum Equals k 560. Subarray Sum Equals K 525. Contiguous Array 1248. Count Number of Nice Subarrays 1371. Find Longest Substring Containing Vowels in Even Counts $ 930. Binary Subarrays With Sum 974. Subarray Sums Divisible by K 523. Continuous Subarray Sum 2845. Count of Interesting Subarrays 528. Random Pick with Weight 1124. Longest Well-Performing Interval