class Solution(object):
def minPatches(self, nums, n):
i, far, re = 0, 0, 0
nums = sorted(nums)
while far < n :
if i < len(nums) and nums[i] <= far + 1:
far += nums[i]
i += 1
else:
re += 1
far += far+1
return re
先按end排序,每次射在end的位置。如果下一个气球的start< end, 那么下一个气球也会被射破。
class Solution(object):
def findMinArrowShots(self, points):
if not points: return 0
points = sorted(points, key = lambda x: x[1])
res = 1
end = points[0][1]
for ball in points[1:]:
if ball[0] > end:
res += 1
end = ball[1]
return res
每次找最小的字母
class Solution(object):
def removeDuplicateLetters(self, s): # O(n)
if not s:
return ''
count = collections.Counter(s)
pos = 0
for i, c in enumerate(s):
if c < s[pos]: pos = i # find the leftmost smallest letter
count[c] -= 1
if count[c] == 0:
break
return s[pos] + self.removeDuplicateLetters(s[pos+1:].replace(s[pos], ''))
另外一种方法
看完直接DP做, 倒数第二个test OTL。后改为greedy, 思路其实差不多。