Posts

Frequency counting for a linear-time algorithm

Frequency counting is a powerful technique in algorithms design. It comes often when we're dealing with a small subset of data, such as lower-case alphabet letters. The problem below exemplifies it. Instead of removing characters one by one, the idea is to do a frequency counting and subsequently create the final string based on the max frequency. Code is down below, cheers, ACC. Apply Operations to Make String Empty - LeetCode 3039. Apply Operations to Make String Empty Medium 32 4 Add to List Share You are given a string  s . Consider performing the following operation until  s  becomes  empty : For  every  alphabet character from  'a'  to  'z' , remove the  first  occurrence of that character in  s  (if it exists). For example, let initially  s = "aabcbbca" . We do the following operations: Remove the underlined characters  s = " a a bc bbca" . The resulting string is  s = "abbca" . Remove the underl...

Grid and Hashes

In this problem, we just need to iterate thru the grid and calculate the average for each region, adding that to a hash table for each pixel, then iterate thru each key in the hash and create the new returning grid accordingly. Process takes O(9NM) where N and M are the dimensions of the grid. Code is down below, cheers, ACC. Find the Grid of Region Average - LeetCode 3030. Find the Grid of Region Average Medium 35 92 Add to List Share You are given a  0-indexed   m x n  grid  image  which represents a grayscale image, where  image[i][j]  represents a pixel with intensity in the range [0..255] . You are also given a  non-negative  integer  threshold . Two pixels  image[a][b]  and  image[c][d]  are said to be  adjacent  if  |a - c| + |b - d| == 1 . A  region  is a  3 x 3  subgrid where the  absolute difference  in intensity between any two  adjacent  pixels is...