Posts

Showing posts from May, 2023

O(N^3) solution for 50x50 matrix

Image
In this problem we need to not only traverse the matrix, but also each diagonal, bringing the total complexity to N^3 or 125K iterations... code is below, cheers, ACC Difference of Number of Distinct Values on Diagonals - LeetCode 2711. Difference of Number of Distinct Values on Diagonals Medium 53 125 Add to List Share Given a  0-indexed  2D  grid  of size  m x n , you should find the matrix  answer  of size  m x n . The value of each cell  (r, c)  of the matrix  answer  is calculated in the following way: Let  topLeft[r][c]  be the number of  distinct  values in the top-left diagonal of the cell  (r, c)  in the matrix  grid . Let  bottomRight[r][c]  be the number of  distinct  values in the bottom-right diagonal of the cell  (r, c)  in the matrix  grid . Then  answer[r][c] = |topLeft[r][c] - bottomRight[r][c]| . Return  the matrix   answ...

One liner using BigInteger and String.Reverse

Image
I overcomplicated the solution here just to come up with an one-liner: reverse the string using String.Reverse, convert to number (BigInteger), to string, then reverse again. Code is below, cheers, ACC. Remove Trailing Zeros From a String - LeetCode 2710. Remove Trailing Zeros From a String Easy 68 1 Add to List Share Given a  positive  integer  num  represented as a string, return  the integer  num  without trailing zeros as a string .   Example 1: Input: num = "51230100" Output: "512301" Explanation: Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301". Example 2: Input: num = "123" Output: "123" Explanation: Integer "123" has no trailing zeros, we return integer "123".   Constraints: 1 <= num.length <= 1000 num  consists of only digits. num  doesn't have any leading zeros. Accepted 20,066 Submissions 25,307 public string RemoveTrailingZeros(string num) { ...

Caching

Image
One of the most important concepts in Computer Science and Computer Engineering is caching. In fact, at work a lot of the improvements that we work on when it comes to performance improvements has to do with caching. Simply put, caching things on memory using a data structure such as hash tables can really go a long way in saving precious (milli)seconds. The problem below exemplifies the caching technique. Notice the use of a static (class-level instead of object-level variable) hash table designed to keep the results of calculations for each n. Then before re-calculating it, I was checking whether the calculation has already been done. Check out how fast is runs. Hope you enjoy, code is down below, cheers, ACC. Find the Punishment Number of an Integer - LeetCode 2698. Find the Punishment Number of an Integer Medium 222 16 Add to List Share Given a positive integer  n , return  the  punishment number  of  n . The  punishment number  of  n ...

Simple Traversals in Binary Trees II

Image
This one requires a standard pre-order, keeping track of the concatenated strings, until we reach the kth element, in which case it is safe to save the result, and return. Code is down below, cheers, ACC. Extract Kth Character From The Rope Tree - LeetCode 2689. Extract Kth Character From The Rope Tree Easy 7 6 Add to List Share You are given the  root  of a binary tree and an integer  k . Besides the left and right children, every node of this tree has two other properties, a  string  node.val  containing only lowercase English letters (possibly empty) and a non-negative integer  node.len . There are two types of nodes in this tree: Leaf : These nodes have no children,  node.len = 0 , and  node.val  is some  non-empty  string. Internal : These nodes have at least one child (also at most two children),  node.len > 0 , and  node.val  is an  empty  string. The tree describ...

Classic Dynamic Programming V

Image
Another classic DP, this time a standard traversal in a graph. Just need to follow the rules to move from column c-1 to c. Also, need to ensure that we can only proceed from where we are at if there has been a valid path to here. That's why there is a bit of calculation first to get the proper values v1,v2,v3, but after that it is pretty straightforward. Code is down below, cheers, ACC. Maximum Number of Moves in a Grid - LeetCode 2684. Maximum Number of Moves in a Grid Medium 173 3 Add to List Share You are given a  0-indexed   m x n  matrix  grid  consisting of  positive  integers. You can start at  any  cell in the first column of the matrix, and traverse the grid in the following way: From a cell  (row, col) , you can move to any of the cells:  (row - 1, col + 1) ,  (row, col + 1)  and  (row + 1, col + 1)  such that the value of the cell you move to, should be  strictly  bigger than the value o...

Marking Edges and Nodes in a graph

Image
Many times we're accustomed to counting or marking the number of nodes in a graph. But this problem not only requires you to do so for the Nodes, but also for the Edges. In an indirect graph, the search can lead to edges that are adjacent to an already-visited node. The best way that I find to deal with this situation is to have another hash table, this time for the edges, and mark them using the following approach: key = (large number) * Max(a,b) + Min(a,b) You can see this approach in use here . Once you count the number of edges and nodes in a connected subgraph, it becomes straightforward to determine whether the subgraph is complete or not. Code is down below, cheers, ACC. Count the Number of Complete Components - LeetCode 2685. Count the Number of Complete Components Medium 116 1 Add to List Share You are given an integer  n . There is an  undirected  graph with  n  vertices, numbered from  0  to  n - 1 . You are given a 2D integer arra...

Sorting in Linear Time

Image
Famous algorithms like QuickSort and MergeSort are well-known for their nLogn time performance and indeed for general purposes they are the best. However, sorting can be done faster than nLogn if certain conditions apply. For example, the key to this problem is the fact that the numbers in the matrix are in between [0..1000]. This allows you to use CountingSort  to perform a linear sort. Doing this you can actually solve this problem in NxM time. Code is below, cheers, ACC. Sum in a Matrix - LeetCode 2679. Sum in a Matrix Medium 33 12 Add to List Share You are given a  0-indexed  2D integer array  nums . Initially, your score is  0 . Perform the following operations until the matrix becomes empty: From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen. Identify the highest number amongst all those removed in step 1. Add that number to your  score . Return  the final...