We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Interesting. For anyone who doesn't understand, let's assume that we know a maximum is at position i in the list. We know that the intervals that don't include i will not contribute to the calculation of i. If we add a spike at the start of a interval, and subtract the spike from right after the interval, then when you add the two spikes together, you will get zero, which is what we want to happen if i is not in that interval. As intervals overlap, there will be multiple positive spikes followed by multiple negative spikes. When you add the positive spikes together, you find a local maximum, and then when you add the negative spikes, you descend from that maximum. The global maximum can thus be found by traveling along the spikes and comparing local maximums. It's almost like you're adding the impulse response of each interval. This solution is not obvious.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Interesting. For anyone who doesn't understand, let's assume that we know a maximum is at position i in the list. We know that the intervals that don't include i will not contribute to the calculation of i. If we add a spike at the start of a interval, and subtract the spike from right after the interval, then when you add the two spikes together, you will get zero, which is what we want to happen if i is not in that interval. As intervals overlap, there will be multiple positive spikes followed by multiple negative spikes. When you add the positive spikes together, you find a local maximum, and then when you add the negative spikes, you descend from that maximum. The global maximum can thus be found by traveling along the spikes and comparing local maximums. It's almost like you're adding the impulse response of each interval. This solution is not obvious.