Terrible LeetCode in Kotlin: #981 Time Based Key Value Store

preview_player
Показать описание
Another day, another LeetCode question (for now). Today, I'm running through #981 Time Based Key Value Store. The problem provides a set of inputs, and asks for you to design a system in which we can add items by key into a data structure, and a get operation that retrieves the item with the closest timestamp without being over the current timestamp given. The difficulty in the problem really came in the form of choosing the right internal data structure. A hashmap is relatively obvious for the outer structure, but there are many ways that the value-timestamp pair can be stored within each key. I decided to go with a list, and then further optimized the get operation by performing a binary search by timestamp. This allows for us to retrieve the item in O(logn) time (where n is the number of items within our list) rather than the O(n) that it would take with a simple linear scan.

I work in Kotlin as my LeetCode programming language because my current focus is on Android app development. Kotlin is interoperable with Java, so the solution should be relatively similar albeit with some more boilerplate checks for nulls and such.

Current Grind:
- Grind75 list, 22 weeks/7 hours per week, [61/169] (51 will be uploaded eventually)

Chapters:
00:00 - Problem discussion
15:59 - Coding begins, hashmap of lists
Рекомендации по теме