Leetcode刷题第一天-贪心

 

455-分饼干

链接:455. 分发饼干 - 力扣(LeetCode)

优先使用最小饼干满足最小胃口,一个娃只能分一个饼干T_T不能加

 1 class Solution:
 2     def findContentChildren(self, g: List[int], s: List[int]) -> int:
 3         if not g or not s:  return 0
 4         g.sort()
 5         s.sort()
 6         i,j,re=0,0,0
 7         while True:
 8             if(i==len(g) or j==len(s)): break
 9             if(s[j]>=g[i]):
10                 re+=1
11                 i+=1
12                 j+=1
13             else:
14                 j+=1
15         return  re

135-分糖果

链接:135. 分发糖果 - 力扣(LeetCode)

所有孩子糖果默认为1,从左往右找,右>左,右+1;再从右往左找,左>右,而且左孩糖<=右孩糖,左+1

 1 class Solution:
 2     def candy(self, ratings: List[int]) -> int:
 3         if(not ratings):    return 0
 4         sweet=[1 for i in range(len(ratings))]
 5         for i in range(len(ratings)-1):
 6             if(ratings[i]<ratings[i+1]):
 7                 sweet[i+1]=sweet[i]+1
 8         for i in range(len(ratings)-1,0,-1):
 9             if(ratings[i]<ratings[i-1] and sweet[i]>=sweet[i-1]):
10                 sweet[i-1]=sweet[i]+1
11         return sum(sweet)

435-无重复区间

链接:435. 无重叠区间 - 力扣(LeetCode)

所有区间有边界排序,留右边界最小值,才能使剩下的区间个数最大,依次检查区间的左边界是否大于最小右边界,小于,移除

 1 class Solution:
 2     def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
 3         if(not intervals):  return 0
 4         lens=len(intervals)
 5         re=0
 6         flage=[0 for i in range(lens)]
 7         intervals.sort(key=lambda x:x[1])
 8         for i in range(lens-1):
 9             if(flage[i]):   continue
10             flage[i]=1
11             for j in range(i,lens):
12                 if(flage[j]):   continue
13                 if(intervals[j][0]<intervals[i][1]):
14                     flage[j]=1
15                     re+=1
16         return re

605-种花问题

链接:605. 种花问题 - 力扣(LeetCode)

当前是1,上下两个坑无论是啥都不能种花,当前是0,下一个是1,不能种,当前是0,上一个是1下一个是0,不能种,当前是0,上一个是0下一个是0,能种

 1 class Solution:
 2     def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
 3         if(not flowerbed):  return False
 4         if(n>flowerbed.count(0)):   return False
 5         next_fl=True
 6         nums=0
 7         for i in range(len(flowerbed)-1):
 8             if(flowerbed[i]==1):    next_fl=False
 9             else:
10                 if(next_fl and not flowerbed[i+1]):
11                     nums+=1
12                     next_fl=False
13                 else:    next_fl=True
14             if(n<=nums):    return True
15         if(next_fl and not flowerbed[len(flowerbed)-1]):    nums+=1
16         if(n<=nums):    return True
17         return False

452-最少箭

链接:452. 用最少数量的箭引爆气球 - 力扣(LeetCode)

 1 class Solution:
 2     def findMinArrowShots(self, points: List[List[int]]) -> int:
 3         if(not points): return 0
 4         points.sort(key=lambda x:x[1])
 5         flage=[0 for i in range(len(points))]
 6         nums=0
 7         for i in range(len(points)):
 8             if(flage[i]):   continue
 9             flage[i]=1
10             for j in range(i,len(points)):
11                 if(flage[j]):   continue
12                 if(points[j][0]<=points[i][1]):
13                     flage[j]=1
14             nums+=1
15         return nums

736-划分字母区间

链接:763. 划分字母区间 - 力扣(LeetCode)

找到字母起始位置和终止位置,范围内的字符和范围后的字符串对比,在后面字符串中,终止位置变更为当前字符的终止位置

 1 class Solution:
 2     def partitionLabels(self, s: str) -> List[int]:
 3         if(not s):  return [0]
 4         new_strs=s[::-1]
 5         re=[]
 6         last,i,lens=0,0,len(s)
 7         flage=True
 8         first_id=s.find(s[0])
 9         end_id=lens-new_strs.find(s[0])
10         while i<=lens:
11             if(end_id==lens):  
12                 re.append(lens-first_id)
13                 break
14             for j in s[first_id:end_id]:
15                 if(j in s[end_id:]):
16                     end_id=lens-new_strs.find(j)
17                     flage=False
18                     break
19             if(flage):  
20                 re.append(end_id-first_id)
21                 i=end_id
22                 first_id=s.find(s[i])
23                 end_id=lens-new_strs.find(s[i])
24             else: 
25                 i=end_id
26                 flage=True
27         return re

122-买卖股票

链接:122. 买卖股票的最佳时机 II - 力扣(LeetCode)

收集每一天的最大利润

1 class Solution:
2     def maxProfit(self, prices: List[int]) -> int:
3         if(not prices): return 0
4         money,i=0,0
5         lens=len(prices)
6         for i in range(1,lens):
7             money+=max(0,prices[i]-prices[i-1])
8         return money

406-身高排序

链接:406. 根据身高重建队列 - 力扣(LeetCode)

先按照身高排序,保证高个前没有比它高的,相同高度,按照个数排,个数少的在后面

1 class Solution:
2     def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
3         if(not people): return people
4         people.sort(key=lambda x:[x[0],-x[1]],reverse=True)
5         lens=len(people)
6         re=[]
7         for p in people:
8             re.insert(p[1],p)
9         return re

 

posted @ 2024-01-22 17:56  小小小茹茹  阅读(46)  评论(0编辑  收藏  举报