Posts

Showing posts from August, 2021

Sorting and sliding window approach

Image
Good morning folks, easy LC problem today, requiring a sorting (NLogN) and sliding window (N). Problem and solution down below - cheers, ACC. Minimum Difference Between Highest and Lowest of K Scores - LeetCode 1984. Minimum Difference Between Highest and Lowest of K Scores Easy 45 7 Add to List Share You are given a  0-indexed  integer array  nums , where  nums[i]  represents the score of the  i th  student. You are also given an integer  k . Pick the scores of any  k  students from the array so that the  difference  between the  highest  and the  lowest  of the  k  scores is  minimized . Return  the  minimum  possible difference .   Example 1: Input: nums = [90], k = 1 Output: 0 Explanation: There is one way to pick score(s) of one student: - [ 90 ]. The difference between the highest and lowest score is 90 - 90 = 0. The minimum possible difference is 0. Example 2...

I absolutely love tree problems!

Image
It had been a while since I had seen a fresh tree problem on Leetcode. This is a Medium one, but fairly straightforward - a direct implementation of a post-order depth-first-search traversal. Code is down below, cheers, ACC. Count Nodes Equal to Sum of Descendants - LeetCode 1973. Count Nodes Equal to Sum of Descendants Medium 2 0 Add to List Share Given the  root  of a binary tree, return  the number of nodes where the value of the node is equal to the  sum  of the values of its descendants . A  descendant  of a node  x  is any node that is on the path from node  x  to some leaf node. The sum is considered to be  0  if the node has no descendants.   Example 1: Input: root = [10,3,4,2,1] Output: 2 Explanation: For the node with value 10: The sum of its descendants is 3+4+2+1 = 10. For the node with value 3: The sum of its descendants is 2+1 = 3. Example 2: Input: root = [2,3,null,2,null] Output: 0 Explanatio...