Posts

Showing posts from February, 2022

Memoization III: remembering the bad paths

Image
This problem was very interesting, I tried it without memoization and it ran into TLE (Time Limit Exceeded), adding memoization for the bad paths solved it. Let's see. Interleaving String - LeetCode 97. Interleaving String Medium 3605 193 Add to List Share Given strings  s1 ,  s2 , and  s3 , find whether  s3  is formed by an  interleaving  of  s1  and  s2 . An  interleaving  of two strings  s  and  t  is a configuration where they are divided into  non-empty  substrings such that: s = s 1  + s 2  + ... + s n t = t 1  + t 2  + ... + t m |n - m| <= 1 The  interleaving  is  s 1  + t 1  + s 2  + t 2  + s 3  + t 3  + ...  or  t 1  + s 1  + t 2  + s 2  + t 3  + s 3  + ... Note:   a + b  is the concatenation of strings  a  and  b .   Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc" Output: false Example 3: Input: s1 = "",

Maximal Square: using DP as a helper for an N^3 solution

Image
Problem is to find the max square in a matrix, take a look:  Maximal Square - LeetCode 221. Maximal Square Medium 6739 140 Add to List Share Given an  m x n  binary  matrix  filled with  0 's and  1 's,  find the largest square containing only   1 's  and return its area .   Example 1: Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] Output: 4 Example 2: Input: matrix = [["0","1"],["1","0"]] Output: 1 Example 3: Input: matrix = [["0"]] Output: 0   Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 300 matrix[i][j]  is  '0'  or  '1' . Accepted 468,284 Submissions 1,087,879 The idea is to use DP as a helper function. Imagine having

Math Problem on LC

Image
This problem is a simple math one on Leetcode:  Find Three Consecutive Integers That Sum to a Given Number - LeetCode " Given an integer  num , return  three consecutive integers (as a sorted array)  that  sum  to  num . If  num  cannot be expressed as the sum of three consecutive integers, return  an  empty  array. " Call the 3 consecutive numbers N, N+1 and N+2. Hence: N+N+1+N+2 = num 3N+3 = num N = (num-3)/3 With that, the solution becomes straightforward - one-liner: return (num - 3) % 3 != 0 ? new long[] { } : new long[] { (num - 3) / 3, (num - 3) / 3 + 1, (num - 3) / 3 + 2 }; Cheers, ACC

IBM Ponder This Jan'22: Backtracking, Miller-Rabin and Caches

Image
IBM Ponder This challenges take place once a month. It has been going on for many years. Most of the challenges, IMO, are really hard and I can't solve them. Every month I do take a look to see if it is solvable, and last month was a doable challenge. Since I caught covid last month, and it hit me really hard, I stayed in isolation for several days. During that time I decided to take a crack at this problem. The problem is listed down below. The way that I approached the problem was the following: 1/ Brute-force. Using backtracking to generate all the possible candidates. This is a standard Depth-First-Search (DFS) backtracking with search space pruning. It will take several minutes since there are just too many candidates. For the "*" bonus question, it does take several hours (~9h), so you need to run it overnight (standard average laptop) 2/ For the primarily testing, I'm a big fan of Miller-Rabin primality test:  Miller–Rabin primality test - Wikipedia . The test

Even Odd: NLogN-Time, N-Space

Image
Easy-LC problem today:  Sort Even and Odd Indices Independently - LeetCode 2164. Sort Even and Odd Indices Independently Easy 74 3 Add to List Share You are given a  0-indexed  integer array  nums . Rearrange the values of  nums  according to the following rules: Sort the values at  odd indices  of  nums  in  non-increasing  order. For example, if  nums = [4, 1 ,2, 3 ]  before this step, it becomes  [4, 3 ,2, 1 ]  after. The values at odd indices  1  and  3  are sorted in non-increasing order. Sort the values at  even indices  of  nums  in  non-decreasing  order. For example, if  nums = [ 4 ,1, 2 ,3]  before this step, it becomes  [ 2 ,1, 4 ,3]  after. The values at even indices  0  and  2  are sorted in non-decreasing order. Return  the array formed after rearranging the values of   nums .   Example 1: Input: nums = [4,1,2,3] Output: [2,3,4,1] Explanation: First, we sort the values present at odd indices (1 and 3) in non-increasing order. So, nums changes from [4, 1 ,2, 3 ] to [4,

Linked List into Graph

Image
What is interesting about this problem is that despite the fact of the input be a Linked List, it is actually easier to solve the problem if you transform the Linked List into a Graph. The idea being is that you want to find the connected components in the Linked List. By transforming it into a Graph, we can perform a DFS just marking the connected components, which makes the solution very simple and running in linear time. Since there is the drawback of parsing the list and creating the extra graph data structure (which as I mentioned before, I’m a big fan of a Hashtable(key, Hashtabls()) as my favorite graph data structure these days), it incurs some penalties in terms of execution time as well as space used, but still enough for a pass. Code is down below, cheers, ACC. Linked List Components - LeetCode You are given the  head  of a linked list containing unique integer values and an integer array  nums  that is a subset of the linked list values. Return  the number of connected c