Posts

Showing posts from 2025

Geometric Algorithms IV

Image
Geometric problems are hard (even when they are labeled as “medium” in LC). The complications arise primarily from the floating-point calculations, over and underflow, as well as the precise equations that one must come up with. This problem  Separate Squares I - LeetCode  requires a binary search in space 10^9. For each iteration, we calculate the areas above and below. Hence the total time is of the order LogN * N. But there are a few things to keep in mind: 1/ Start by transforming all the input from int to double. This will avoid any over and underflow calculation 2/ Break down into smaller subroutines, for debugging 3/ Remember that when dealing with floating-points, to say that A == B, what you really need to do is ensure that |A – B| <= T where T is some threshold (in our case T = 0.00001) You can use LLM such as OpenAI ChatGPT o1 (with advanced reasoning) for debugging. For example, you can ask it to plot some input in a Cartesian System for debugging purp...

Diagonals

Image
We need to sort the diagonals of a matrix. The approach that I use here is the following: 1. Create a Hashtable where the key is the diagonal index. The diagonal index is just the row-col 2. Traverse the matrix adding the elements as a list to the corresponding Hashtable entry 3. Traverse the Hashtable and sort each list (you need to store in a different Hashtable) 4. Traverse the matrix again. Now that you have the diagonal index, get the corresponding element using the Hashtable 5. Remove the corresponding element from the list, depending whether you're sorting up or down Code is down below, cheers, ACC Sort Matrix by Diagonals - LeetCode You are given an  n x n  square matrix of integers  grid . Return the matrix such that: The diagonals in the  bottom-left triangle  (including the middle diagonal) are sorted in  non-increasing order . The diagonals in the  top-right triangle  are sorted in  non-decreasing order .   Example 1: Input: ...

A 10-year-old problem

Image
Apparently back in 2015 (10 years ago!) I tried to solve this problem several times, unsuccessfully. I was young and inexperienced, probably. I gave it another try now. The idea is to not have any sort of matrix. Basically, just keep track of the rows and have a hash table storing the strings in each row. As you move the row index, and you append the characters to the end of the strings, everything falls into place quite well. There is one odd case, when the number of rows is one, which doesn't really work in my calculations, hence I handle it separately in the beginning. Problem solved, only took 10 years. Code is down below, cheers, ACC. Zigzag Conversion - LeetCode The string  "PAYPALISHIRING"  is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line:  "PAHNAPLSIIGYIR" Write the code that will take a string a...

Prefix Sum VII

Image
Very easy problem which could have been solved in N^2 since N = 100, but you can also use Prefix Sum technique here, which beats 100% in speed and memory. Code is down below, cheers, ACC. Count Partitions with Even Sum Difference - LeetCode You are given an integer array  nums  of length  n . A  partition  is defined as an index  i  where  0 <= i < n - 1 , splitting the array into two  non-empty  subarrays such that: Left subarray contains indices  [0, i] . Right subarray contains indices  [i + 1, n - 1] . Return the number of  partitions  where the  difference  between the  sum  of the left and right subarrays is  even .   Example 1: Input:   nums = [10,10,3,7,6] Output:   4 Explanation: The 4 partitions are: [10] ,  [10, 3, 7, 6]  with a sum difference of  10 - 26 = -16 , which is even. [10, 10] ,  [3, 7, 6]  with a sum difference of  20 - 1...