Posts

Showing posts from April, 2025

String manipulation ought to be avoided II

Image
First attempt to solving this problem led to time limit exceeded, too much string manipulation. One option to work around it is to make use of the String constructor that lets you create a string of length N composed of the same character. This problem is also a great example of frequency-counting strategy, common with string problems where the constraint is to use lower case characters only. I also nowadays like to use OpenAI ChatGPT 4.5+ to help me analyze the time complexity of some of these algorithms. Fos this one, for example, the analysis is clear that the time complexity is linear. Code is down below, cheers, ACC. Smallest Palindromic Rearrangement I - LeetCode You are given a  palindromic  string  s . Return the  lexicographically smallest  palindromic  permutation  of  s .   Example 1: Input:   s = "z" Output:   "z" Explanation: A string of only one character is already the lexicographically smallest palindrome. Example 2:...

Don't be ashamed of N^2 III

Image
Remember that for smaller Ns, an N^2 solution works just fine. This is an example of it. N in this case is 50, hence an N^2 solution is trivial, almost linear. One function to check if we're done, and then the main function to perform the operations, resulting in N^2. Code is down below, cheers, ACC. Minimum Pair Removal to Sort Array I - LeetCode Given an array  nums , you can perform the following operation any number of times: Select the  adjacent  pair with the  minimum  sum in  nums . If multiple such pairs exist, choose the leftmost one. Replace the pair with their sum. Return the  minimum number of operations  needed to make the array  non-decreasing . An array is said to be  non-decreasing  if each element is greater than or equal to its previous element (if it exists).   Example 1: Input:   nums = [5,2,3,1] Output:   2 Explanation: The pair  (3,1)  has the minimum sum of 4. After replacement,  ...