Posts

Showing posts from October, 2023

Follow the hints! - Part 4

Image
Not a hard problem but it requires a bit of focus on the details of the calculations. The hints really helped here. Code is down below, cheers, ACC. Minimum Equal Sum of Two Arrays After Replacing Zeros - LeetCode 2918. Minimum Equal Sum of Two Arrays After Replacing Zeros Medium 65 12 Add to List Share You are given two arrays  nums1  and  nums2  consisting of positive integers. You have to replace  all  the  0 's in both arrays with  strictly  positive integers such that the sum of elements of both arrays becomes  equal . Return  the  minimum  equal sum you can obtain, or  -1  if it is impossible .   Example 1: Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0] Output: 12 Explanation: We can replace 0's in the following way: - Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4]. - Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1]. Both arrays have an equal sum of 12. It can be shown that it is the mini

Sliding Window Technique - Part 10

Image
Another sliding window solution, this time we use the back pointer as the control. Code is a little cumbersome on the movement of left and right pointers, but it is still a standard sliding window technique. There is a need for a lexicographic comparator for binary strings (worst-case: O(n), but only when we have a new candidate). Code is down below, cheers, ACC. Shortest and Lexicographically Smallest Beautiful String - LeetCode 2904. Shortest and Lexicographically Smallest Beautiful String Medium 121 6 Add to List Share You are given a binary string  s  and a positive integer  k . A substring of  s  is  beautiful  if the number of  1 's in it is exactly  k . Let  len  be the length of the  shortest  beautiful substring. Return  the lexicographically  smallest  beautiful substring of string  s  with length equal to  len . If  s  doesn't contain a beautiful substring, return  an  empty  string . A string  a  is lexicographically  larger  than a string  b  (of the same length) i