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

LeetCode笔记:Biweekly Contest 65

作者头像
codename_cys
发布2021-11-23 10:43:50
2490
发布2021-11-23 10:43:50
举报
文章被收录于专栏:我的充电站我的充电站

1. 题目一

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

1. 解题思路

这一题思路很直接,就是统计一下两个string种所有字母的个数,然后一一比较两者之间的差值。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Solution:
    def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
        cnt1 = Counter(word1)
        cnt2 = Counter(word2)
        for ch in string.ascii_lowercase:
            if abs(cnt1[ch] - cnt2[ch]) > 3:
                return False
        return True

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

2. 题目二

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

1. 解题思路

这一题事实上机器人只能走在最外围,因此,我们只需要计算出周长就能准确定位出任意时间其处在的位置,由此也就能定位其当时的方式。

唯一麻烦一点的就是一些边界条件的判断。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Robot:

    def __init__(self, width: int, height: int):
        self.is_begining = True
        self.width = width
        self.height = height
        self.loc = 0
        self.length = (width + height) * 2 - 4

    def move(self, num: int) -> None:
        self.is_begining = self.is_begining and num == 0
        self.loc = (self.loc + num) % self.length

    def getPos(self) -> List[int]:
        if self.loc <= self.width-1:
            return [self.loc, 0]
        elif self.width <= self.loc <= self.width + self.height-2:
            return [self.width-1, self.loc - self.width+1]
        elif self.width + self.height-1 <= self.loc <= 2*self.width + self.height-3:
            return [2*self.width + self.height-3 - self.loc, self.height-1]
        else:
            return [0, self.length - self.loc]

    def getDir(self) -> str:
        if self.is_begining or 0 < self.loc < self.width:
            return "East"
        elif self.width <= self.loc < self.width + self.height-1:
            return "North"
        elif self.width + self.height-1 <= self.loc <= 2*self.width + self.height-3:
            return "West"
        else:
            return "South"

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

3. 题目三

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

1. 解题思路

这一题我们只需要对item进行排序,然后对于每一个item,更新其value为前序中所有的item对应的value的最大值。

然后,我们对于任意一个query,只需要找到其在item当中对应的位置就能够获得最大的value。

2. 代码实现

给出python代码实现如下:

代码语言:javascript
复制
class Solution:
    def maximumBeauty(self, items: List[List[int]], queries: List[int]) -> List[int]:
        items = sorted(items)
        n = len(items)
        for i in range(n-1):
            items[i+1][1] = max(items[i][1], items[i+1][1])
        
        res = []
        for q in queries:
            idx = bisect.bisect_right(items, [q, math.inf])
            if idx == 0:
                res.append(0)
            else:
                res.append(items[idx-1][1])
        return res

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

4. 题目四

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

放弃了,当时没想出来,隔了一周,就不想再看了……

后面有空再说吧……

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

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

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

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

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