LeetCode冲刺:Day37

发布于 2022-12-14  1072 次阅读


1. 转化时间需要的最少操作数

难度

  • Easy
  • Medium
  • Hard

完成情况

  • 独立完成
  • 参考思路
  • 参考答案

思路

本来想到有后者时间早于前者的情况,但提示中排除了,那么就比较简单了。

考虑两种情况,一种前者分钟小于后者,直接计算;一种前者分钟大于后者,那么在负数基础上加60,然后小时数减一。

代码

class Solution:
    def convertTime(self, current: str, correct: str) -> int:
        ans = int(correct[0:2]) - int(current[0:2])
        diff = int(correct[3:5]) - int(current[3:5])
        if diff < 0:
            diff += 60
            ans -= 1
        while diff:
            if diff >= 15:
                cnt = int(diff / 15)
                diff -= cnt * 15
                ans += cnt
            elif diff >= 5:
                cnt = int(diff / 5)
                diff -= cnt * 5
                ans += cnt
            else:
                ans += diff
                diff = 0
        return ans

2. 找出输掉零场或一场比赛的玩家

难度

  • Easy
  • Medium
  • Hard

完成情况

  • 独立完成
  • 参考思路
  • 参考答案

思路

图省事用了数组,应该用字典会快很多。

代码

class Solution:
    def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
        winner = [0 for i in range(100001)]
        loser = [0 for i in range(100001)]
        ans = [[], []]
        for match in matches:
            winner[match[0]] += 1
            loser[match[1]] += 1
        for i in range(100001):
            if winner[i] > 0 and loser[i] == 0:
                ans[0].append(i)
            elif loser[i] == 1:
                ans[1].append(i)
        return ans

3. 每个小孩最多能分到多少糖果

难度

  • Easy
  • Medium
  • Hard

完成情况

  • 独立完成
  • 参考思路
  • 参考答案

思路

本来没什么思路。看到题解标题“二分答案”,马上就做出来了。说明嗅觉还不够灵敏。

代码

class Solution:
    def maximumCandies(self, candies: List[int], k: int) -> int:
        total = sum(candies)
        if total < k: return 0
        elif total == k: return 1
        low, high = 1, max(candies)
        ans = 0
        while low <= high:
            mid = low + int((high - low) / 2)
            cnt = 0
            for candy in candies:
                cnt += int(candy / mid)
            if cnt >= k:
                ans = mid
                low = mid + 1
            else: high = mid - 1
        return ans

4. 加密解密字符串

难度

  • Easy
  • Medium
  • Hard

完成情况

  • 独立完成
  • 参考思路
  • 参考答案

思路

构造函数和加密函数没什么说的,关键在解密函数。

顺着逻辑太慢且麻烦,逆向思维,直接统计字典中加密后的密文频率,返回即可。

代码

class Encrypter:
    __en = {}
    __dictionary = {}
    def __init__(self, keys: List[str], values: List[str], dictionary: List[str]):
        self.__en = {}
        self.__dictionary = {}
        n = len(keys)
        for i in range(n):
            self.__en[keys[i]] = values[i]
        for word in dictionary:
            s = self.encrypt(word)
            if s not in self.__dictionary: self.__dictionary[s] = 1
            else: self.__dictionary[s] += 1
    def encrypt(self, word1: str) -> str:
        res = ""
        for ch in word1:
            if ch not in self.__en: return ""
            res += self.__en[ch]
        return res

    def decrypt(self, word2: str) -> int:
        if word2 not in self.__dictionary: return 0
        else: return self.__dictionary[word2]