Hi there 馃憢

This blog is intended to share my knowledge about software architecture, system design, tools, and techniques for producing high-quality products.

LeetCode - 150 - Median of Two Sorted Arrays

The problem Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Examples Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. Constraints nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 -10^6 <= nums1[i], nums2[i] <= 10^6 Explanation From our given examples we can learn that we could potentially have an even or odd number of total elements after we merge two input arrays. If we just try to merge both inputs and find the median, it will take us O(m + n) time, but we know that is not what we were asked to do. ...

July 11, 2025 路 6 min 路 Dmytro Chumakov

LeetCode - 150 - Time Based Key-Value Store

The problem Design a time-based key-value data structure that can store multiple values for the same key at different timestamps and retrieve the key鈥檚 value at a certain timestamp. Implement the聽TimeMap聽class: TimeMap()聽Initializes the object of the data structure. void set(String key, String value, int timestamp)聽Stores the key聽key聽with the value聽value聽at the given time聽timestamp. String get(String key, int timestamp)聽Returns a value such that聽set聽was called previously, with聽timestamp_prev <= timestamp. If there are multiple such values, it returns the value associated with the largest聽timestamp_prev. If there are no values, it returns聽"". Examples Input ["TimeMap", "set", "get", "get", "set", "get", "get"] [[], ["foo", "bar", 1], ["foo", 1], ["foo", 3], ["foo", "bar2", 4], ["foo", 4], ["foo", 5]] Output [null, null, "bar", "bar", null, "bar2", "bar2"] Explanation TimeMap timeMap = new TimeMap(); timeMap.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1. timeMap.get("foo", 1); // return "bar" timeMap.get("foo", 3); // return "bar", since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 is "bar". timeMap.set("foo", "bar2", 4); // store the key "foo" and value "bar2" along with timestamp = 4. timeMap.get("foo", 4); // return "bar2" timeMap.get("foo", 5); // return "bar2" Constraints 1 <= key.length, value.length <= 100 key聽and聽value聽consist of lowercase English letters and digits. 1 <= timestamp <= 10^7 All the timestamps聽of聽set聽are strictly increasing. At most聽2 * 10^5聽calls will be made to聽set聽and聽get. Explanation Our objective for this task is to design a key-value store. We are going to have a key, and a list of values associated with that key, and each value is going to have a timestamp associated with it. As for operations, we are going to support only set and get operations. Now, let鈥檚 look at our example ...

July 6, 2025 路 5 min 路 Dmytro Chumakov

LeetCode - 150 - Koko Eating Bananas

The problem Koko loves to eat bananas. There are聽n聽piles of bananas, the聽ith聽pile has聽piles[i]聽bananas. The guards have gone and will come back in聽h聽hours. Koko can decide her bananas-per-hour eating speed of聽k. Each hour, she chooses some pile of bananas and eats聽k聽bananas from that pile. If the pile has less than聽k聽bananas, she eats all of them instead and will not eat any more bananas during this hour. ...

July 2, 2025 路 5 min 路 Dmytro Chumakov

LeetCode - 150 - Search a 2D Matrix

The problem You are given an聽m x n聽integer matrix聽matrix聽with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer聽target, return聽true聽if聽target聽is in聽matrix聽or聽false聽otherwise. You must write a solution in聽O(log(m * n))聽time complexity. Examples Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true ...

June 29, 2025 路 4 min 路 Dmytro Chumakov

LeetCode - 150 - Binary Search

The problem Given an array of integers聽nums聽which is sorted in ascending order, and an integer聽target, write a function to search聽target聽in聽nums. If聽target聽exists, then return its index. Otherwise, return聽-1. You must write an algorithm with聽O(log n)聽runtime complexity. Examples Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1 Constraints 1 <= nums.length <= 10^4 -10^4 < nums[i], target < 10^4 All the integers in聽nums聽are聽unique. nums聽is sorted in ascending order. Explanation Before we jump into the solution, let鈥檚 figure out what the requirements for a binary search algorithm are and how it is going to work. The main requirement for binary search is that the input must be sorted. ...

June 27, 2025 路 3 min 路 Dmytro Chumakov