Terrible LeetCode in Kotlin: #228 Product of Array Except Self

18 Просмотры
Издатель
Another day, another LeetCode question (for now). Today, I'm running through #228 on LeetCode, Product of Array Except Self. The problem provides an array of integers, with an expected output of an array where each value is the product of all numbers from the input except the number at that index. The brute force solution involves calculating each one for each iteration, with a runtime of O(n^2). The first optimization can be made by trading space for time, and creating both a prefix and postfix array of products and calculating the product using that. This brings the runtime down to O(n) (3 linear passes), with a space of O(n). The second optimization can be made by removing one or the other of the arrays, instead using a value as an accumulator to multiply each time.

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, [52/169] (51 will be uploaded eventually)
(https://www.techinterviewhandbook.org/grind75)

Chapters:
00:00 - Initial attempt, problem explanation.
09:30 - Coding begins
Категория
Язык программирования Kotlin
Комментариев нет.