LeetCode #977 - Squares of a Sorted Array (Easy)

56 Просмотры
Издатель
ALMOST got the right solution!
Used an object-oriented, idomatic Kotlin solution, using PriorityQueue.
Runs in O(n) time, with O(n) memory.
I should have used a plain old PriorityQueue. Instead, I got ahead of myself, and created a Comparator sorting by { it * it }, THEN adding num * num to the array, resulting in comparison by num^4, which overflowed when sorting, resulting in unpredictable result.

Still, my code compiled with no syntax errors, and no runtime errors! I'm getting better at this!
Категория
Язык программирования Kotlin
Комментариев нет.