Fail Leetcode

Опубликовано: 26 Октябрь 2024
на канале: Pass OR Fail: Coding Journey
8
1

Fail to Solve Leetcode 907 problem withing 20 minutes.
I managed to find O(N^3) brute force solution and optimize it to O(N^2). I coded O(N^2) solution but it was failing with leetcode judge due to TimeLimitExceeded error. The main explanation for the failure is that leetcode gives a bound for N less or equal 3x10^4, and O(N^2) ~= 10^9 which exceeds the current threshold for time complexity for modern CPUs to complete the task within seconds.
I managed to find O(N) solution using two mono stacks (first mono stack for contiguous elements that are greater or equal to the current one to the left, and second mono stack for .... to the right), but I failed on mono stack implementation stage. There is a important detail for applying mono stack here and I missed it.
Overall performance: Fail the interview