LeetCode - 150 - Kth Largest Element in an Array

The problem Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Can you solve it without sorting? Examples Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4 Constraints 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 Explanation From the description of the problem we learn that we need to find the kth largest element from the given array. The kth element means if, for example, we had k = 1 then we would return the first largest element, or if we had k = 3 then we would return the third largest element. ...

August 15, 2025 · 2 min · Dmytro Chumakov

LeetCode - 150 - K Closest Points to Origin

The problem Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0). The distance between two points on the X-Y plane is the Euclidean distance (i.e., √(x1 - x2)^2 + (y1 - y2)^2). You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in). ...

August 13, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Last Stone Weight

The problem You are given an array of integers stones where stones[i] is the weight of the ith stone. We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is: If x == y, both stones are destroyed, and If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x. At the end of the game, there is at most one stone left. ...

August 11, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Kth Largest Element in a Stream

The problem You are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determine cut-off marks for interviews and admissions dynamically as new applicants submit their scores. You are tasked to implement a class which, for a given integer k, maintains a stream of test scores and continuously returns the kth highest test score after a new score has been submitted. More specifically, we are looking for the kth highest score in the sorted list of all scores. ...

August 9, 2025 · 4 min · Dmytro Chumakov

LeetCode - 150 - Count Good Nodes in Binary Tree

The problem Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree. Examples Input: root = [3,1,4,3,null,1,5] Output: 4 Explanation: Nodes in blue are good. Root Node (3) is always a good node. Node 4 -> (3,4) is the maximum value in the path starting from the root. Node 5 -> (3,4,5) is the maximum value in the path Node 3 -> (3,1,3) is the maximum value in the path. ...

August 7, 2025 · 3 min · Dmytro Chumakov