Posts

Blocks and Long

Image
Long in C# can store numbers up to 9,223,372,036,854,775,807. This problem requires counting the number of blocks in a grid. Not a lot of challenges other than using hashtables and calculations with longs. Code is down below, ACC. Number of Black Blocks - LeetCode 2768. Number of Black Blocks Medium 5 0 Add to List Share You are given two integers  m  and  n  representing the dimensions of a  0-indexed   m x n  grid. You are also given a  0-indexed  2D integer matrix  coordinates , where  coordinates[i] = [x, y]  indicates that the cell with coordinates  [x, y]  is colored  black . All cells in the grid that do not appear in  coordinates  are  white . A block is defined as a  2 x 2  submatrix of the grid. More formally, a block with cell  [x, y]  as its top-left corner where  0 <= x < m - 1  and  0 <= y < n - 1  contains the coordinates ...

Geometric Algorithms II

Image
Not a super complicated one: given four points determine whether they make a square. Notice that the square can be in any orientation, not only parallel to the axis. The way that I solved this one was using the following algorithm: 1/ Calculate the square of the distances amongst the 4 points. There are 6 distances in total. Use long. Don't use square root, just square 2/ You want to make sure that you have 4 distances the same (call it A), and 2 distances the same (call it B) 3/ You also want to make sure that the B = 2*A as per Pythagoras  If those conditions are met, you've got a square. Otherwise, no square. Code is down below, cheers, ACC. Valid Square - LeetCode 593. Valid Square Medium 967 880 Add to List Share Given the coordinates of four points in 2D space  p1 ,  p2 ,  p3  and  p4 , return  true   if the four points construct a square . The coordinate of a point  p i  is represented as  [x i , y i ] . The input is...

Permutation with no Repetition but Randomized

Image
Interesting problem from a friend: permutation with no repetition but random without shuffling at the end (without extra memory). Same concept as standard permutation with no repetition, just randomly swap the direction of the sweep. Code is down below, cheers, ACC private void PermutationNoRepetitionRandomOrder(string str, string currentStr, Hashtable indexVisited, Random rdObj) { if (currentStr.Length == str.Length) { Console.WriteLine(currentStr); return; } Hashtable localCharUsedAtPosition = new Hashtable(); int left = 0; int right = str.Length - 1; while (left <= right) { if (rdObj.Next(0, 2) == 0) { if (!indexVisited.ContainsKey(left) && !localCharUsedAtPosition.ContainsKey(str[left])) { localCharUsedAtPosition.Add(str[left]...

Saturday Night Dijkstra's Algorithm II

Image
Another problem that requires use of Dijkstra's Algorithm for efficiency's purposes. Basically we have a directed weighted graph, and want to find the min distance between a certain node and any marked node. Approach goes as follows: 1/ Build a quick-access graph using a hashtable. Remember that you can have multiple edges [u,v,w] where u and v are the same. Handle that in the creation method 2/ Make a quick-access look-up table for the marked nodes 3/ Use a ascending priority queue to speed up the BFS algorithm 4/ As you do the BFS, unfortunately there is no easy way to stop it since you may always find a min route. Instead, rely on the pruning to not add to the queue any path larger than the min so far 5/ Handle some edge cases here and there, such as no-path found Code is down below, cheers, ACC Find the Closest Marked Node - LeetCode 2737. Find the Closest Marked Node Medium 10 0 Add to List Share You are given a positive integer  n  which is the number of nodes of a...

StringBuilder

Image
The beauty of StringBuilder is just the ability of performing character-level operations in-situ without the need to concatenate strings which is expensive and in some cases can lead to TLE. This example, although simple, requires attention to corner-cases (like a string that starts with "a"s) as well as the use of StringBuilder for fast manipulation. Code is down below, cheers, ACC. Lexicographically Smallest String After Substring Operation - LeetCode 2734. Lexicographically Smallest String After Substring Operation Medium 83 106 Add to List Share You are given a string  s  consisting of only lowercase English letters. In one operation, you can do the following: Select any non-empty substring of  s , possibly the entire string, then replace each one of its characters with the previous character of the English alphabet. For example, 'b' is converted to 'a', and 'a' is converted to 'z'. Return  the  lexicographically smallest  string you can ob...

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