Created
October 10, 2015 03:52
-
-
Save ejamesc/bc4ab0132ac07c9e282b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_valleys(arr): | |
| peaks = [] | |
| for i in xrange(0, len(arr)): | |
| if i == 0 and arr[i] > arr[i + 1]: | |
| peaks.append((i, arr[i])) | |
| elif i == (len(arr) - 1) and arr[i] > arr[i - 1]: | |
| peaks.append((i, arr[i])) | |
| elif arr[i] > arr[i - 1] and arr[i] > arr[i + 1]: | |
| peaks.append((i, arr[i])) | |
| # reduce peaks | |
| has_merged = False | |
| while has_merged: | |
| to_remove = [] | |
| for i in xrange(0, len(peaks)): | |
| if i != 0 or i != len(peaks) - 1: | |
| curr, next, prev = peaks[i], peaks[i + 1], peaks[i - 1] | |
| if curr[1] < next[1] and curr[1] < prev[1]: | |
| has_merged = True | |
| to_remove.append(i) | |
| for i in to_remove: | |
| peaks.pop(i) | |
| res_sum = 0 | |
| for i in xrange(0, len(peaks) - 1): | |
| # print res_sum | |
| res_sum += calculate_area(peaks[i], peaks[i + 1], arr) | |
| return res_sum | |
| def calculate_area(start, end, arr): | |
| # print "starting from", start[0], "to", end[0] | |
| mm = min(start[1], end[1]) | |
| s = 0 | |
| for i in xrange(start[0] + 1, end[0]): | |
| curr_sum = mm - arr[i] | |
| s += curr_sum if curr_sum > 0 else 0 | |
| return s | |
| if __name__ == "__main__": | |
| input = [4, 3, 2, 1, 5, 6, 3, 2, 3, 4] | |
| res = find_valleys(input) | |
| print res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment