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

LeetCode 2462: Total Cost to Hire K Workers

Today, let’s look at LeetCode problem 2462: Total Cost to Hire K Workers. The instructions are as follows: You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the \(i\)-th worker. You are also given two integers \(k\) and candidates. We want to hire exactly \(k\) workers according to the following rules: You will run \(k\) sessions and hire exactly one worker in each session. In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index. For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,**1**,2]. In the second hiring session, we will choose 1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,**2**,7,7,2]. Please note that the indexing may be changed in the process. If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index. A worker can only be chosen once. Return the total cost to hire exactly \(k\) workers. ...

December 29, 2025 · 6 min · David Nabergoj

LeetCode 88: Merge Sorted Array

Let’s solve LeetCode problem 88: Merge Sorted Array. This problem is quite short and straightforward. The instructions are as follows: You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers \(m\) and \(n\), representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of \(m\) + \(n\), where the first \(m\) elements denote the elements that should be merged, and the last \(n\) elements are set to 0 and should be ignored. nums2 has a length of \(n\). ...

December 29, 2025 · 3 min · David Nabergoj