Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
""" DP Time: O(n^2) Space: O(n) """defcanJump(self,nums: List[int]) ->bool: n =len(nums) dp = [False] * n dp[0]=Truefor i inrange(n-1):if dp[i]:for jump inrange(1, nums[i]+1):if i+jump < n: dp[i+jump]=Truereturn dp[-1]""" Greedy Time: O(n) Space: O(1) """defjump(self,nums: List[int]) ->int: n =len(nums) max_pos = nums[0]for i inrange(1, n):if max_pos < i:returnFalse max_pos =max(max_pos, i+nums[i])returnTrue
Jump game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
""" DP Time: O(n^2) Space: O(n) """defjump(self,nums: List[int]) ->int: n =len(nums) dp = [float('inf')] * n dp[0]=0for i inrange(n-1):if i ==0or dp[i]!=float('inf'):for jump inrange(1, nums[i]+1):if i+jump < n: dp[i+jump]=min(dp[i+jump], 1+ dp[i])return dp[-1]if dp[-1]!=float('inf')else0""" Greedy Time: O(n) Space: O(1) """defjump(self,nums: List[int]) ->int: n =len(nums)if n <=1:return0# max position reachable from index 0 pos_max = nums[0]# max position reachable from current jump cur_max = nums[0] count =1for i inrange(1, n):if cur_max < i: count +=1 cur_max = pos_max pos_max =max(pos_max, i + nums[i])return count
Magicl Vowel
A magical sub-sequence of a string S is a sub-sequence of S that contains all five vowels in order. Find the length of largest magical sub-sequence of a string S. For example, if S = aeeiooua, then aeiou and aeeioou are magical sub-sequences but aeio and aeeioua are not.
State: S到index i, char到index j目前longest voewl string number