Posts

Showing posts from November, 2022

Removing nodes from linked list

Image
For this one, I certainly didn't implement in the most optimal way. I moved the initial list into a proper List<int> structure to be able to run it backwards. Then, keep track of the max and remove the items properly. Then you move the resulting list back into the ListNode structure. There is a 3N time complexity here, plus the extra storage, but works. Code is down below, cheers, ACC. Remove Nodes From Linked List - LeetCode 2487. Remove Nodes From Linked List Medium 213 3 Add to List Share You are given the  head  of a linked list. Remove every node which has a node with a  strictly greater  value anywhere to the right side of it. Return  the  head  of the modified linked list.   Example 1: Input: head = [5,2,13,3,8] Output: [13,8] Explanation: The nodes that should be removed are 5, 2 and 3. - Node 13 is to the right of node 5. - Node 13 is to the right of node 2. - Node 8 is to the right of node 3. Example 2: Input: head = [1,1,...

In-Order Traversal and Binary Search

Image
This problem requires an in-order traversal of a BST (Binary Search Tree), which keeps the elements of the tree in the same order (sorted), followed by a slight variation of a standard binary search in an array. Code is down below, cheers, ACC. Closest Nodes Queries in a Binary Search Tree - LeetCode 2476. Closest Nodes Queries in a Binary Search Tree Medium 137 59 Add to List Share You are given the  root  of a  binary search tree  and an array  queries  of size  n  consisting of positive integers. Find a  2D  array  answer  of size  n  where  answer[i] = [min i , max i ] : min i  is the  largest  value in the tree that is smaller than or equal to  queries[i] . If a such value does not exist, add  -1  instead. max i  is the  smallest  value in the tree that is greater than or equal to  queries[i] . If a such value does not exist, add  -1  instead. ...

On a quick implementation of GCD - Greatest Common Divisor - Part III

Image
Although this problem doesn't necessarily ask for GCD, it requires it to calculate LCM (Least Common Multiple), which can be done via the formula LCM(a,b) = a*b/GCD(a,b). With that in hands we can solve this problem in N^2 given that N=10^3. Notice that once we calculate LCD(a,b) we can then use it as the carrier for the next number. Code is down below, cheers, ACC. Number of Subarrays With LCM Equal to K - LeetCode 2470. Number of Subarrays With LCM Equal to K Medium 202 14 Add to List Share Given an integer array  nums  and an integer  k , return  the number of  subarrays  of  nums  where the least common multiple of the subarray's elements is  k . A  subarray  is a contiguous non-empty sequence of elements within an array. The  least common multiple of an array  is the smallest positive integer that is divisible by all the array elements.   Example 1: Input: nums = [3,6,2,7,1], k = 6 Output: 4 Explanation: ...

Sliding Window Technique - Part 7

Image
There is usually one caveat on sliding window approaches: usually after the main loop, you still need to make one extra check for the very last case. Treat it as a corner case, albeit critical for the correctness of the code. Talking about this line here . Code is down below, cheers, ACC. Maximum Sum of Distinct Subarrays With Length K - LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K Medium 305 3 Add to List Share You are given an integer array  nums  and an integer  k . Find the maximum subarray sum of all the subarrays of  nums  that meet the following conditions: The length of the subarray is  k , and All the elements of the subarray are  distinct . Return  the maximum subarray sum of all the subarrays that meet the conditions .  If no subarray meets the conditions, return  0 . A  subarray  is a contiguous non-empty sequence of elements within an array.   Example 1: Input: nums = [1,5,4,2,9,9,9], k...

Trie-Trie-Trie!!! - Part 3

Image
This problem is another instance of Trie, with a simple recursive function to check for the partial matches. Straightforward. Code is below, cheers, ACC. Words Within Two Edits of Dictionary - LeetCode 2452. Words Within Two Edits of Dictionary Medium 132 5 Add to List Share You are given two string arrays,  queries  and  dictionary . All words in each array comprise of lowercase English letters and have the same length. In one  edit  you can take a word from  queries , and change any letter in it to any other letter. Find all words from  queries  that, after a  maximum  of two edits, equal some word from  dictionary . Return  a list of all words from  queries ,  that match with some word from  dictionary  after a maximum of  two edits . Return the words in the  same order  they appear in  queries .   Example 1: Input: queries = ["word","note","ants","wood"], dictionary = ["woo...