Posts

Showing posts from September, 2022

Follow the hints! - Part 2

Image
If you follow the hints on the problem, it really becomes super straightforward. Calculate the max chain of good indices towards the left and towards the right in two different O(N)-time loops. Iterate a third time to create the return list. Rest is history. Code is down below, cheers, ACC. Find All Good Indices - LeetCode 2420. Find All Good Indices Medium 251 19 Add to List Share You are given a  0-indexed  integer array  nums  of size  n  and a positive integer  k . We call an index  i  in the range  k <= i < n - k   good  if the following conditions are satisfied: The  k  elements that are just  before  the index  i  are in  non-increasing  order. The  k  elements that are just  after  the index  i  are in  non-decreasing  order. Return  an array of all good indices sorted in  increasing  order .   Example 1: Input: nums = [2,1,1,1,3,4,1], k = 2 Output: [2,3] Explanation: There are two good indices in the array: - Index 2. The subarray [2,1] is in non-increasing order, and

Trie-Trie-Trie!!! - Part 2

Image
A so-called hard problem but in reality a straightforward Trie implementation. Typical Trie where the very first element (root) is ignored and you use a hashtable for the children. In this case here keep track of the score as you add all the words. Works super fast in execution time O(sizeWord * numberWords) or 1M (max) steps. Code is down below, cheers, ACC. Sum of Prefix Scores of Strings - LeetCode 2416. Sum of Prefix Scores of Strings Hard 226 20 Add to List Share You are given an array  words  of size  n  consisting of  non-empty  strings. We define the  score  of a string  word  as the  number  of strings  words[i]  such that  word  is a  prefix  of  words[i] . For example, if  words = ["a", "ab", "abc", "cab"] , then the score of  "ab"  is  2 , since  "ab"  is a prefix of both  "ab"  and  "abc" . Return  an array  answer  of size  n  where  answer[i]  is the  sum  of scores of every  non-empty  prefi

Reversing odd levels of a Binary Tree - standard BFS

Image
Standard BFS for this problem: keep a list of all the odd elements and whenever you switch state (meaning moving to an even level - reminds me of a little bit of FSM), then you can "reverse the list". Make sure to do one more time outside of the main queue loop for the case where the last level is an odd one. Code is down below, cheers, ACC. Reverse Odd Levels of Binary Tree - LeetCode 2415. Reverse Odd Levels of Binary Tree Medium 150 3 Add to List Share Given the  root  of a  perfect  binary tree, reverse the node values at each  odd  level of the tree. For example, suppose the node values at level 3 are  [2,1,3,4,7,11,29,18] , then it should become  [18,29,11,7,4,3,1,2] . Return  the root of the reversed tree . A binary tree is  perfect  if all parent nodes have two children and all leaves are on the same level. The  level  of a node is the number of edges along the path between it and the root node.   Example 1: Input: root = [2,3,5,8,13,21,34] Output: [2,5,3,8,13,21,34

Simple NLogN to warm up your day!

Image
Sorting both arrays (NLogN), then a linear scan looking for the matching pairs. Doesn't get more NLogN than this. Code is down below friends, cheers, ACC. Maximum Matching of Players With Trainers - LeetCode 2410. Maximum Matching of Players With Trainers Medium 77 1 Add to List Share You are given a  0-indexed  integer array  players , where  players[i]  represents the  ability  of the  i th  player. You are also given a  0-indexed  integer array  trainers , where  trainers[j]  represents the  training capacity  of the  j th  trainer. The  i th  player can  match  with the  j th  trainer if the player's ability is  less than or equal to  the trainer's training capacity. Additionally, the  i th  player can be matched with at most one trainer, and the  j th  trainer can be matched with at most one player. Return  the  maximum  number of matchings between  players  and  trainers  that satisfy these conditions.   Example 1: Input: players = [4,7,9], trainers = [8,2,5,8] Outpu

LC Hard Problem: Follow the Hints - Part II

Image
From the get-go one can see that this problem is either a DP or BFS, likely the latter rather than the former, as confirmed by the hints. At the end it is a straightforward BFS with the only caveat of allowing for a cell that contains an obstacle IIF we still have enough "k"s to remove it. Keep an eye on the key to mark the cell as visited, noticed the formula to guarantee an unique number off of the 3-tuple {row, col, k). Code is down below, cheers, ACC. Shortest Path in a Grid with Obstacles Elimination - LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination Hard 2736 48 Add to List Share You are given an  m x n  integer matrix  grid  where each cell is either  0  (empty) or  1  (obstacle). You can move up, down, left, or right from and to an empty cell in  one step . Return  the minimum number of  steps  to walk from the upper left corner  (0, 0)  to the lower right corner  (m - 1, n - 1)  given that you can eliminate  at most   k  obstacles . If it is not pos

Sliding Window Technique - Part 6 (Bitwise)

Image
In order to assure that a&b, b&c and a&c are all zero, keep track of the cumulative value of the bits for the 3 numbers. You can do it by using "|" operand (basically you're adding up all bits in a smart and linear way). The sliding window will be applied using this rule, but there is one caveat: when you move the left pointer, you'll have to remove it from the cumulative. You can do it by "&" the not of the element pointed by the left pointer. The code is down below, linear time, cheers, ACC. You are given an array  nums  consisting of  positive  integers. We call a subarray of  nums   nice  if the bitwise  AND  of every pair of elements that are in  different  positions in the subarray is equal to  0 . Return  the length of the  longest  nice subarray . A  subarray  is a  contiguous  part of an array. Note  that subarrays of length  1  are always considered nice.   Example 1: Input: nums = [1,3,8,48,10] Output: 3 Explanation: The longest

The power of pruning in backtracking II

Image
This problem is a little confusing to understand, but it fit with backtracking especially given the small constraints (numbers are small). Backtrack the set of columns to choose, and calculate the rows that fit the coverage. The only pruning is just the beginning of the inner loop where we pass the startIndex  in order to avoid starting all over all the time. Code is down below, cheers, ACC. Maximum Rows Covered by Columns - LeetCode 2397. Maximum Rows Covered by Columns Medium 72 177 Add to List Share You are given a  0-indexed   m x n  binary matrix  mat  and an integer  cols , which denotes the number of columns you must choose. A row is  covered  by a set of columns if each cell in the row that has a value of  1  also lies in one of the columns of the chosen set. Return  the  maximum  number of rows that can be  covered  by a set of  cols  columns.   Example 1: Input: mat = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], cols = 2 Output: 3 Explanation: As shown in the diagram above, one poss