diff --git a/Python/jump-game-ii.py b/Python/jump-game-ii.py index 61223b114..571c405d4 100644 --- a/Python/jump-game-ii.py +++ b/Python/jump-game-ii.py @@ -14,7 +14,7 @@ # # not pass on leetcode because of time limit -class Solution: +class Solution(object): # @param A, a list of integers # @return an integer def jump(self, A): @@ -29,47 +29,7 @@ def jump(self, A): jump_count += 1 reachable = max(reachable, i + length) return jump_count - -# Time: O(n^2) -# Space: O(1) -class Solution2: - # @param A, a list of integers - # @return an integer - def jump(self, A): - result, prev_reachable, reachable = 0, -1, 0 - while reachable > prev_reachable: - if reachable >= len(A) - 1: - return result - result += 1 - prev_reachable = reachable - for i, length in enumerate(A[:reachable + 1]): - reachable = max(reachable, i + length) - return -1 - -# when you on an index of nums, move to next index which can move farthest in range of this index reachable -# Time: O(log(n)) -# Space: O(1) -class Solution3(object): - def jump(self, nums): - """ - :type nums: List[int] - :rtype: int - """ - nums[-1] = 2 ** 31 - nums2, l = [i + j for i, j in enumerate(nums)], len(nums) - 1 - - def find_max_index(index): - tmp = nums2[index:index + nums[index] + 1] - return index + tmp.index(max(tmp)) - - index, steps = 0, 0 - while True: - index = find_max_index(index) - if index: - steps += 1 - if index == l: - break - return steps + if __name__ == "__main__": print Solution().jump([2,3,1,1,4])