Posts

Project Euler: 2025

Image
In this problem, we need to find numbers N such that N can be decomposed into the concatenations of two positive integers A and B, hence N = AB, in such a way that N = (A+B)^2. All the way to N ~ 10^16. Project Euler's rules do not allow us to share our solutions or ideas about how to solve the problems (problems beyond the first 100) outside of their platform, so I'll abide to their rules. What I'll say though is that it is clear that you can't write an algorithm to solve it in 10^16, but since we're dealing with squared numbers, you should strongly think about going to 10^8 instead (perhaps slightly more than that). The code is down below in an image with the key parts omitted for the reasons mentioned above. Cheers, ACC. #932 $2025$ - Project Euler

Standard Priority Queue VI: Max Heap

Image
Although this problem's hint #1 mentions that you should sort the rows, in reality you don't need to. Creating a max heap, adding all elements there, and removing them according to the limits given, should do the trick. Code is down below, cheers, ACC. Maximum Sum With at Most K Elements - LeetCode You are given a 2D integer matrix  grid  of size  n x m , an integer array  limits  of length  n , and an integer  k . The task is to find the  maximum sum  of  at most   k  elements from the matrix  grid  such that: The number of elements taken from the  i th  row of  grid  does not exceed  limits[i] . Return the  maximum sum .   Example 1: Input:   grid = [[1,2],[3,4]], limits = [1,2], k = 2 Output:   7 Explanation: From the second row, we can take at most 2 elements. The elements taken are 4 and 3. The maximum possible sum of at most 2 selected elements is  4 + 3 = 7 . Ex...

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