Posts

Showing posts from 2025

Prefix Sum X

Image
The description of the problem asks for a Prefix Sum, so there is no confusion. Further optimization can be made by combining the two initial for-loops into one. Code is down below, cheers, ACC. Maximum Score of a Split - LeetCode You are given an integer array  nums  of length  n . Choose an index  i  such that  0 <= i < n - 1 . For a chosen split index  i : Let  prefixSum(i)  be the sum of  nums[0] + nums[1] + ... + nums[i] . Let  suffixMin(i)  be the minimum value among  nums[i + 1], nums[i + 2], ..., nums[n - 1] . The  score  of a split at index  i  is defined as: score(i) = prefixSum(i) - suffixMin(i) Return an integer denoting the  maximum  score over all valid split indices.   Example 1: Input:   nums = [10,-1,3,-4,-5] Output:   17 Explanation: The optimal split is at  i = 2 ,  score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17 . Example ...

StringBuilder V

Image
One more problem which is simple but leads to TLE if you use String instead of StringBuilder. The entire code below was initially written with String and I got several time-limit exceeded errors. Switching most of the implementation to StringBuilder solved the problem. Code is down below, cheers, ACC. Reverse Words With Same Vowel Count - LeetCode You are given a string  s  consisting of lowercase English words, each separated by a single space. Determine how many vowels appear in the  first  word. Then, reverse each following word that has the  same vowel count . Leave all remaining words unchanged. Return the resulting string. Vowels are  'a' ,  'e' ,  'i' ,  'o' , and  'u' .   Example 1: Input:   s = "cat and mice" Output:   "cat dna mice" Explanation: ​​​​​​​ The first word  "cat"  has 1 vowel. "and"  has 1 vowel, so it is reversed to form  "dna" . "mice"  has 2 vowels, so it remains...