Posts

Connected Components in a Graph II

LeetCode Problem 3532 is an interesting one that tests understanding of graph theory and connected components. The problem gives us: An integer n representing nodes in a graph (labeled 0 to n-1) An array nums of length n in non-decreasing order A value maxDiff A series of queries asking if a path exists between two nodes Two nodes i and j have an edge between them if |nums[i] - nums[j]| <= maxDiff . For each query [u, v] , we need to determine if there's a path from node u to node v . The Key Insight: Connected Components This problem is essentially asking to determine if two nodes belong to the same connected component in an undirected graph. What makes this problem particularly approachable is that there's no need to perform a BFS or DFS for each query. Since nums is already sorted, this property can be exploited to pre-compute all connected components in linear time! public bool[] PathExistenceQueries(int n, int[] nums, int maxDiff, int[][] queries) { ...

Roulette Odds

Image
Roulette odds are the closest to 50-50 if it wasn't for the zero. Inevitably, the player loses. A simple simulation goes as follows: 1. Start with $1000 2. In each round, play $5 3. If you get the number right, you get back the original amount played, plus five times it 4. If you get the parity right (even or odd), you get back the original amount played, plus two times it 5. Rule #4 doesn't apply to zero. If the tossed number is zero, only rule #3 applies 6. Play until you run out of money In my simulation (code down below), I got few insights: A. On average you lose all your money after 200 rounds B. Maximum that I could get was ~400,000 rounds (and near $250,000) before losing it all (see chart below) C. If you remove the zero restriction, there is a possibility of convergence and stability where you end up with somewhere between $30,000 and $100,000 Code is down below, cheers, ACC. public void PlayRoulette() { int amount = 1000; int playAmount = 5; int rounds =...

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...