Wednesday, July 5th
Find the Longest Subarray of 1's After Deleting One Element. The problem is listed as a medium, and works on array skills.
The idea behind my solution was track the number of 1's before and after a 0. For every 1 in the array, increment the after variable, and when a 0 is found, make before = after and after=0. Check when after+before>max_total, and ultimately return max_total. Finding the solution took about 20 minutes, which seems slow once all the ideas are clear and together.
This approach differed from the solution in the LeetCode Editorial, which used a sliding window. While both my approach and the Editorial ran in linear time with constant space, my approach only goes through the array once, while the sliding window solution did twice.
Comments
Post a Comment