前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode笔记:Biweekly Contest 57(补发)

LeetCode笔记:Biweekly Contest 57(补发)

作者头像
codename_cys
发布2021-08-05 10:09:19
2020
发布2021-08-05 10:09:19
举报
文章被收录于专栏:我的充电站

1. 题目一

给出题目一的试题链接如下:

1. 解题思路

这一题没啥好说的,搞个counter统计一下就行了。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Solution:
    def areOccurrencesEqual(self, s: str) -> bool:
        cnt = list(Counter(s).values())
        return all(x == cnt[0] for x in cnt)

提交代码评测得到:耗时32ms,占用内存14.3MB。

2. 题目二

给出题目二的试题链接如下:

1. 解题思路

这一题的思路也还好,我们首先将人员按照到场时间重新排序,然后用两个堆栈分别来记录当前椅子的空余情况已经椅子按照时间被释放的情况。

此后,每当一个人进场时,我们都根据进场时间来释放椅子,然后从空余的椅子列表当中选择最小序列的椅子分配给该客人即可。

当客人序号等于目标序号时,我们就可以直接返回答案了。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Solution:
    def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
        n = len(times)
        q = []
        seats = [i for i in range(n)]
        heapq.heapify(seats)
        times = sorted([x, y, i] for i, (x, y) in enumerate(times))
        for x, y, i in times:
            while q != [] and q[0][0] <= x:
                _, s = heapq.heappop(q)
                heapq.heappush(seats, s)
            s = heapq.heappop(seats)
            if i == targetFriend:
                return s
            heapq.heappush(q, (y, s))
        return -1

提交代码评测得到:耗时708ms,占用内存19.6MB。

3. 题目三

给出题目三的试题链接如下:

1. 解题思路

这一题思路其实也挺常见的,就是对首尾进行标记,然后最后累计求和,尤其题目中还限定了颜色不会重复出现,因此只要出现了内容的片段就必然不会是重复的,因此我们就可以通过一个标量的值直接来标记当前位置下所有的颜色增减情况。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Solution:
    def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
        n = max([x[1] for x in segments])
        colors = [[0, 0] for _ in range(n+1)]
        for st, ed, c in segments:
            colors[st][0] += c
            colors[ed][1] += c
        pre, mix, res = -1, 0, []
        for i, (add, minus) in enumerate(colors):
            if add == 0 and minus == 0:
                continue
            if pre != -1 and mix != 0:
                res.append([pre, i, mix])
            pre = i
            mix = mix + add - minus
        return res

提交代码评测得到:耗时1376ms,占用内存39.6MB。

4. 题目四

给出题目四的试题链接如下:

1. 解题思路

这一题就是一道单调队列的问题,不过坑爹的是虽然有思路不过想了半天各种bug,最后看了答案才给出了解答,这里就不多说了,直接给出官方的解答链接吧……

官方解答:队列中可以看到的人数

2. 代码实现

我们的代码实现如下:

代码语言:javascript
复制
class Solution:
    def canSeePersonsCount(self, heights: List[int]) -> List[int]:
        n = len(heights)
        res = [0 for _ in range(n)]
        insight = []
        
        for i in range(n-1, -1, -1):
            while insight:
                res[i] += 1
                if insight[0] <= heights[i]:
                    insight.pop(0)
                else:
                    break
            insight.insert(0, heights[i])
        return res

提交代码评测得到:耗时7460ms,占用内存29.8M。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目一
    • 1. 解题思路
      • 2. 代码实现
      • 2. 题目二
        • 1. 解题思路
          • 2. 代码实现
          • 3. 题目三
            • 1. 解题思路
              • 2. 代码实现
              • 4. 题目四
                • 1. 解题思路
                  • 2. 代码实现
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档