当前位置:   article > 正文

2024.6.2力扣刷题记录

2024.6.2力扣刷题记录

目录

一、力扣每日一题-575. 分糖果

模拟

二、100307. 候诊室中的最少椅子数

模拟

三、100311. 无需开会的工作日

1.差分(超内存)

2.合并数组


一、力扣每日一题-575. 分糖果

模拟

  1. class Solution:
  2. def distributeCandies(self, candyType: List[int]) -> int:
  3. # 模拟
  4. # 在n // 2和不同种类数中选择最小值
  5. return min(len(candyType) // 2, len(set(candyType)))

二、100307. 候诊室中的最少椅子数

模拟

  1. class Solution:
  2. def minimumChairs(self, s: str) -> int:
  3. ans = 0
  4. cnt = 0
  5. for c in s:
  6. if c == 'E':
  7. cnt += 1
  8. ans = max(ans, cnt)
  9. else:
  10. cnt -= 1
  11. return ans

三、100311. 无需开会的工作日

1.差分(超内存)

days范围太大了。

  1. class Solution:
  2. def countDays(self, days: int, meetings: List[List[int]]) -> int:
  3. # 差分
  4. ans = 0
  5. nums = [0] * (days + 1)
  6. for a, b in meetings:
  7. nums[a - 1] += 1
  8. nums[b] -= 1
  9. cur = 0
  10. for i in range(days):
  11. cur += nums[i]
  12. if cur == 0:
  13. ans += 1
  14. return ans

2.合并数组

来自灵神题解(. - 力扣(LeetCode))。

  1. class Solution:
  2. def countDays(self, days: int, meetings: List[List[int]]) -> int:
  3. # 合并数组
  4. meetings.sort(key = lambda p: p[0])
  5. start, end = 1, 0
  6. for s, e in meetings:
  7. if s > end: # 新的区间
  8. days -= end - start + 1 # 更新
  9. start = s
  10. end = max(end, e)
  11. days -= end - start + 1 # 更新最后一个区间
  12. return days

感谢你看到这里!一起加油吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/728870
推荐阅读
相关标签
  

闽ICP备14008679号