LeetCode 347: Top K Frequent Elements

Let’s solve LeetCode problem 347: Top K Frequent Elements. The instructions are as follows: Given an integer array nums and an integer \(k\), return the \(k\) most frequent elements. You may return the answer in any order. Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 \(k\) is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. I solved this problem on NeetCode as well. The goal was to find a solution in \(O(n)\) time and space, where \(n\) is the length of nums. I’ll be showing this solution here. Even though some solutions that take \(O(n\log n)\) time are faster for LeetCode leaderboards, this solution will scale a bit better. Let’s dive in! ...

January 2, 2026 · 5 min · David Nabergoj

LeetCode 55: Jump Game

Let’s solve LeetCode problem 55: Jump Game. The instructions are as follows: You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. Constraints: 1 <= nums.length <= 10^4 0 <= nums[i] <= 10^5 I solved this in two ways: a less efficient approach using dynamic programming, and a very fast greedy method. Let’s dive in! ...

January 1, 2026 · 4 min · David Nabergoj

LeetCode 122: Best Time to Buy and Sell Stock II

Let’s solve LeetCode problem 122: Best Time to Buy and Sell Stock II. The instructions are as follows: You are given an integer array prices where prices[i] is the price of a given stock on the \(i\)-th day. On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can sell and buy the stock multiple times on the same day, ensuring you never hold more than one share of the stock. Find and return the maximum profit you can achieve. ...

January 1, 2026 · 5 min · David Nabergoj

LeetCode 189: Rotate Array

Happy new year! 🥳🎉🎉🎉 Let’s make 2026 awesome! Let’s solve LeetCode problem 189: Rotate Array. The instructions are as follows: Given an integer array nums, rotate the array to the right by \(k\) steps, where \(k\) is non-negative. Constraints: 1 <= nums.length <= 10^5 -2^31 <= nums[i] <= 2^31 - 1 0 <= k <= 10^5 This problem is fairly straightforward. I’ll present three solutions in C++. Let’s dive in! Note: rotations have a sort of cyclical property. If \(k\) is greater or equal to \(n\) (the length of nums), then rotating nums by with \(k\) is the same as rotating it with \(k\) modulo \(n\) – the remainder after dividing \(k\) by \(n\). We will always work with the remainder instead of the original \(k\). ...

December 31, 2025 · 4 min · David Nabergoj

LeetCode 80: Remove Duplicates from Sorted Array II

Today, let’s look at LeetCode problem 80: Remove Duplicates from Sorted Array II. The instructions are as follows: Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are \(k\) elements after removing the duplicates, then the first \(k\) elements of nums should hold the final result. It does not matter what you leave beyond the first \(k\) elements. ...

December 30, 2025 · 3 min · David Nabergoj