Posts

Showing posts from December, 2024

2466. Count Ways To Build Good Strings

Given the integers  zero ,  one ,  low , and  high , we can construct a string by starting with an empty string, and then at each step perform either of the following: Append the character  '0'   zero  times. Append the character  '1'   one  times. This can be performed any number of times. A  good  string is a string constructed by the above process having a  length  between  low  and  high  ( inclusive ). Return  the number of  different  good strings that can be constructed satisfying these properties.  Since the answer can be large, return it  modulo   10 9  + 7 .   Example 1: Input: low = 3, high = 3, zero = 1, one = 1 Output: 8 Explanation: One possible valid good string is "011". It can be constructed as follows: "" -> "0" -> "01" -> "011". All binary strings from "000" to "111" are good strings in this example. Example 2: Input: low = ...

1639. Number of Ways to Form a Target String Given a Dictionary

You are given a list of strings of the  same length   words  and a string  target . Your task is to form  target  using the given  words  under the following rules: target  should be formed from left to right. To form the  i th  character ( 0-indexed ) of  target , you can choose the  k th  character of the  j th  string in  words  if  target[i] = words[j][k] . Once you use the  k th  character of the  j th  string of  words , you  can no longer  use the  x th  character of any string in  words  where  x <= k . In other words, all characters to the left of or at index  k  become unusuable for every string. Repeat the process until you form the string  target . Notice  that you can use  multiple characters  from the  same string  in  words  provided the conditions above are met. Retu...

689. Maximum Sum of 3 Non-Overlapping Subarrays

Given an integer array  nums  and an integer  k , find three non-overlapping subarrays of length  k  with maximum sum and return them. Return the result as a list of indices representing the starting position of each interval ( 0-indexed ). If there are multiple answers, return the lexicographically smallest one.   Example 1: Input: nums = [1,2,1,2,6,7,5,1], k = 2 Output: [0,3,5] Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5]. We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger. Example 2: Input: nums = [1,2,1,2,1,2,1,2,1], k = 2 Output: [0,2,4]   Constraints: 1 <= nums.length <= 2 * 10 4 1 <= nums[i] < 2 16 1 <= k <= floor(nums.length / 3) Solution 1: Sliding Window Solution 2: Preprocessing Prefix and Suffix + Enumerating Middle Subarray C++ class Solution { public: vector < int > maxSumOfThreeSubarrays ( vector < int ...