From 65ec9ddc53335e3da4c1bf654fbfcef24f6b0282 Mon Sep 17 00:00:00 2001 From: PK-100 Date: Mon, 31 Oct 2022 01:01:23 +0530 Subject: [PATCH] Add Jump_Game_VI in python --- Python/Jump_Game_VI.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Python/Jump_Game_VI.py diff --git a/Python/Jump_Game_VI.py b/Python/Jump_Game_VI.py new file mode 100644 index 0000000..5eba890 --- /dev/null +++ b/Python/Jump_Game_VI.py @@ -0,0 +1,20 @@ +from heapq import heappop, heappush + +class Solution: + def maxResult(self, nums: List[int], k: int) -> int: + n = len(nums) + if n == 1: return nums[0] + + maxHeap = [(-nums[0], 0)] + max_so_far = None + + for i in range(1, n): + # print(sorted([ (-i[0], i[1]) for i in maxHeap])) + + while maxHeap[0][1] < i - k: + heappop(maxHeap) + + max_so_far = maxHeap[0][0] + heappush(maxHeap, (max_so_far - nums[i], i)) + + return -(max_so_far - nums[i])