Posts

Showing posts from 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,  ...

Connected Components in a Graph

Image
This problem is an interesting one—it initially looks daunting, but upon closer inspection, it turns out to be quite approachable. The solution involves two main steps: building the graph and counting the connected components. I prefer to represent graphs using a simple Hashtable, as it makes accessing and traversing the graph straightforward and efficient. Since this problem involves an undirected graph, it's important to remember to add edges in both directions, meaning both {i, j} and {j, i} must be included. Counting connected components is a classic graph problem that can be efficiently solved using Depth-First Search (DFS). The DFS approach systematically marks all vertices belonging to the same component, ensuring that each vertex is visited at most twice—resulting in a linear-time algorithm. If you're curious about the detailed analysis of the algorithm, ChatGPT 4.5 provides a thorough complexity breakdown, confirming that, given the constraints, the solution is quite...

The power of pruning in backtracking IV

Image
Although this is marked as an easy problem, solving generically would make it a medium one. Standard backtracking but the key it to prune it: as soon as the number exceeds the min allowed, cut short the search space. Code is down below, cheers, ACC. Unique 3-Digit Even Numbers - LeetCode You are given an array of digits called  digits . Your task is to determine the number of  distinct  three-digit even numbers that can be formed using these digits. Note : Each  copy  of a digit can only be used  once per number , and there may  not  be leading zeros.   Example 1: Input:   digits = [1,2,3,4] Output:   12 Explanation:  The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2. Example 2: Input:   digits = [0,2,2] Output:   2 Explanation:  The only 3-digit even numbers that ca...