Posts

Showing posts from August, 2022

Gauss' 10y-old formula at work - Part II

Image
In order to count the number of contiguous subarrays meeting the below constraints, we make use one more time of the famous Gauss summation formula when the dude was 10y old. Linear time. Code is below, cheers, ACC. Count Strictly Increasing Subarrays - LeetCode 2393. Count Strictly Increasing Subarrays Medium 3 0 Add to List Share You are given an array  nums  consisting of  positive  integers. Return  the number of  subarrays  of  nums  that are in  strictly increasing  order. A  subarray  is a  contiguous  part of an array.   Example 1: Input: nums = [1,3,5,4,4,6] Output: 10 Explanation: The strictly increasing subarrays are the following: - Subarrays of length 1: [1], [3], [5], [4], [4], [6]. - Subarrays of length 2: [1,3], [3,5], [4,6]. - Subarrays of length 3: [1,3,5]. The total number of subarrays is 6 + 3 + 1 = 10. Example 2: Input: nums = [1,2,3,4,5] Output: 15 Explanation: Every subarray is strictly increasing. There are 15 possible subarrays that we can take. public lo

Using Finite-State Machine (FSM) to model and solve string problems - Part II

Image
We use FSM to model when we're inside stars or outside, keep track of the count of stars, and manipulate an index of the final string. Avoid string manipulation, it will lead to time-limit-exceeded. Instead, keep track of the index and use a StringBuilder which allows you to modify each cell in place, only switching back to string at the very last step. Code is down below, cheers, ACC. Removing Stars From a String - LeetCode 2390. Removing Stars From a String Medium 198 9 Add to List Share You are given a string  s , which contains stars  * . In one operation, you can: Choose a star in  s . Remove the closest  non-star  character to its  left , as well as remove the star itself. Return  the string after  all  stars have been removed . Note: The input will be generated such that the operation is always possible. It can be shown that the resulting string will always be unique.   Example 1: Input: s = "leet**cod*e" Output: "lecoe" Explanation: Performing the rem

Sliding Window Technique - Part 5

Image
Standard sliding window technique here, my implementation was a little more convoluted this time around (I'm sure there is a clearer way to detect the window) but did the work. Notice that the constraints are very generous, hence even an N^2 solution (2-nested loops) would be able to solve this problem (the sliding window solves it in linear time). Code is down below, cheers, ACC. Minimum Recolors to Get K Consecutive Black Blocks - LeetCode 2379. Minimum Recolors to Get K Consecutive Black Blocks Easy 208 9 Add to List Share You are given a  0-indexed  string  blocks  of length  n , where  blocks[i]  is either  'W'  or  'B' , representing the color of the  i th  block. The characters  'W'  and  'B'  denote the colors white and black, respectively. You are also given an integer  k , which is the desired number of  consecutive  black blocks. In one operation, you can  recolor  a white block such that it becomes a black block. Return  the  minimum  num

This is a Graph Problem - Not a Tree Problem! Part II

Image
Another deceiving problem: don't try to solve it as a Tree problem, you'll get stuck. Transform the tree into a graph first. Then, find the longest path starting from 'start' node, using DFS. Code is down below, cheers, ACC. Amount of Time for Binary Tree to Be Infected - LeetCode 2385. Amount of Time for Binary Tree to Be Infected Medium 338 2 Add to List Share You are given the  root  of a binary tree with  unique  values, and an integer  start . At minute  0 , an  infection  starts from the node with value  start . Each minute, a node becomes infected if: The node is currently uninfected. The node is adjacent to an infected node. Return  the number of minutes needed for the entire tree to be infected.   Example 1: Input: root = [1,5,3,null,4,10,6,9,2], start = 3 Output: 4 Explanation: The following nodes are infected during: - Minute 0: Node 3 - Minute 1: Nodes 1, 10 and 6 - Minute 2: Node 5 - Minute 3: Node 4 - Minute 4: Nodes 9 and 2 It takes 4 minutes for the w

Using a temporary wild card

Image
This problem should be more in the easy category. The only caveat is to use a temporary wild card to do the first transform, then use it again for the second one. Can be solved in four lines . Code is down below, cheers, ACC. Time Needed to Rearrange a Binary String - LeetCode You are given a binary string  s . In one second,  all  occurrences of  "01"  are  simultaneously  replaced with  "10" . This process  repeats  until no occurrences of  "01"  exist. Return  the number of seconds needed to complete this process.   Example 1: Input: s = "0110101" Output: 4 Explanation: After one second, s becomes "1011010". After another second, s becomes "1101100". After the third second, s becomes "1110100". After the fourth second, s becomes "1111000". No occurrence of "01" exists any longer, and the process needed 4 seconds to complete, so we return 4. Example 2: Input: s = "11100" Output:

Palindromic Number - devil is in the details

Image
This problem is more labor intensive than hard. You got to just keep an eye on the details. Using the bucket count approach, create the largest palindrome, but keep an eye on the "0"s. Code is below, cheers, ACC. Largest Palindromic Number - LeetCode 2384. Largest Palindromic Number Medium 212 139 Add to List Share You are given a string  num  consisting of digits only. Return  the  largest palindromic  integer (in the form of a string) that can be formed using digits taken from  num . It should not contain  leading zeroes . Notes: You do  not  need to use all the digits of  num , but you must use  at least  one digit. The digits can be reordered.   Example 1: Input: num = "444947137" Output: "7449447" Explanation: Use the digits "4449477" from " 44494 7 13 7 " to form the palindromic integer "7449447". It can be shown that "7449447" is the largest palindromic integer that can be formed. Example 2: Input: num =

One line to save them all!

Image
This problem, given the constraints, could be solved via brute-force since we're dealing with at the most 9! permutations, which is a tiny number. However, it was leading to TLE. The one liner which saved them all was the one here . Basically, I just needed to know if the current number was already larger than the smallest one that I have already found. If so, it is pointless to continue. Code is down below, cheers, ACC. Construct Smallest Number From DI String - LeetCode 2375. Construct Smallest Number From DI String Medium 84 8 Add to List Share You are given a  0-indexed  string  pattern  of length  n  consisting of the characters  'I'  meaning  increasing  and  'D'  meaning  decreasing . A  0-indexed  string  num  of length  n + 1  is created using the following conditions: num  consists of the digits  '1'  to  '9' , where each digit is used  at most  once. If  pattern[i] == 'I' , then  num[i] < num[i + 1] . If  pattern[i] == 'D

This is a Graph Problem - Not a Tree Problem!

Image
Don't be fooled by the problem description! Although it does say "tree", what you really have here is a graph and you want to do a Depth First Search pruning based on the given restrictions. Graph can be coded using a Hashtable of Hashtables. Code is down below, cheers, ACC. Reachable Nodes With Restrictions - LeetCode 2368. Reachable Nodes With Restrictions Medium 208 7 Add to List Share There is an undirected tree with  n  nodes labeled from  0  to  n - 1  and  n - 1  edges. You are given a 2D integer array  edges  of length  n - 1  where  edges[i] = [a i , b i ]  indicates that there is an edge between nodes  a i  and  b i  in the tree. You are also given an integer array  restricted  which represents  restricted  nodes. Return  the  maximum  number of nodes you can reach from node  0  without visiting a restricted node. Note that node  0  will  not  be a restricted node.   Example 1: Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5] Outp