LeetCode 每日一题 2023/9/25-2023/10/1

这篇具有很好参考价值的文章主要介绍了LeetCode 每日一题 2023/9/25-2023/10/1。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步




9/25 460. LFU 缓存

freqMap 以频率为索引 存放一个双向链表 每个节点存放key,value,freq
keyMap 以key为索引存放在freqMap中的位置

from collections import defaultdict 
class Node:
    def __init__(self, key, val, pre=None, nex=None, freq=0):
        self.pre = pre
        self.nex = nex
        self.freq = freq
        self.val = val
        self.key = key
        
    def insert(self, nex):
        nex.pre = self
        nex.nex = self.nex
        self.nex.pre = nex
        self.nex = nex
    
def create_linked_list():
    head = Node(0, 0)
    tail = Node(0, 0)
    head.nex = tail
    tail.pre = head
    return (head, tail)

class LFUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.size = 0
        self.minFreq = 0
        self.freqMap = defaultdict(create_linked_list)
        self.keyMap = {}

    def delete(self, node):
        if node.pre:
            node.pre.nex = node.nex
            node.nex.pre = node.pre
            if node.pre is self.freqMap[node.freq][0] and node.nex is self.freqMap[node.freq][-1]:
                self.freqMap.pop(node.freq)
        return node.key
        
    def increase(self, node):
        node.freq += 1
        self.delete(node)
        self.freqMap[node.freq][-1].pre.insert(node)
        if node.freq == 1:
            self.minFreq = 1
        elif self.minFreq == node.freq - 1:
            head, tail = self.freqMap[node.freq - 1]
            if head.nex is tail:
                self.minFreq = node.freq

    def get(self, key: int) -> int:
        if key in self.keyMap:
            self.increase(self.keyMap[key])
            return self.keyMap[key].val
        return -1

    def put(self, key: int, value: int) -> None:
        if self.capacity != 0:
            if key in self.keyMap:
                node = self.keyMap[key]
                node.val = value
            else:
                node = Node(key, value)
                self.keyMap[key] = node
                self.size += 1
            if self.size > self.capacity:
                self.size -= 1
                deleted = self.delete(self.freqMap[self.minFreq][0].nex)
                self.keyMap.pop(deleted)
            self.increase(node)





9/26 2582. 递枕头

n个人 经过2n-2次回到开始的人

def passThePillow( n, time):
    """
    :type n: int
    :type time: int
    :rtype: int
    """
    time = time%(2*n-2)
    
    ans = 1+time
    if ans>n:
        ans = n-(ans-n)
    return ans




9/27 1333. 餐厅过滤器

先按照rating和id排序
然后筛选符合条件的餐馆

def filterRestaurants(restaurants, veganFriendly, maxPrice, maxDistance):
    """
    :type restaurants: List[List[int]]
    :type veganFriendly: int
    :type maxPrice: int
    :type maxDistance: int
    :rtype: List[int]
    """
    restaurants.sort(key=lambda x:(-x[1],-x[0]))
    ans = []
    for idx,_,v,p,dist in restaurants:
        if v>=veganFriendly and p<=maxPrice and dist<=maxDistance:
            ans.append(idx)
    return ans



9/28 2251. 花期内花的数目

差分数组 开花时+1 凋谢时-1

def fullBloomFlowers(flowers, people):
    """
    :type flowers: List[List[int]]
    :type people: List[int]
    :rtype: List[int]
    """
    from collections import Counter
    diff = Counter()
    for s,e in flowers:
        diff[s]+=1
        diff[e+1]-=1
    t = sorted(diff.keys())
    
    n = len(people)
    j=0
    s=0
    ans = [0]*n
    for p,i in sorted(zip(people,range(n))):
        while j<len(t) and t[j]<=p:
            s+=diff[t[j]]
            j+=1
        ans[i] = s
    return ans



9/29 605. 种花问题

从头遍历 查看最多能种多少

def canPlaceFlowers(flowerbed, n):
    """
    :type flowerbed: List[int]
    :type n: int
    :rtype: bool
    """
    cur = 0
    m=len(flowerbed)
    if m==1:
        if flowerbed[0]==0:
            cur+=1
        return cur>=n
    if flowerbed[0]==flowerbed[1]==0:
        cur+=1
        flowerbed[0]=1
    
    if flowerbed[m-1]==flowerbed[m-2]==0:
        cur+=1
        flowerbed[m-1]=1    
    if cur>=n:
        return True
    for i in range(1,len(flowerbed)-1):
        if flowerbed[i-1]==flowerbed[i]==flowerbed[i+1]==0:
            cur+=1
            flowerbed[i]=1
        if cur>=n:
            return True
    return False



9/30 2136. 全部开花的最早一天

无论播种顺序 种花时间总和是不变的
先种生长时间最长的花

def earliestFullBloom(plantTime, growTime):
    """
    :type plantTime: List[int]
    :type growTime: List[int]
    :rtype: int
    """
    ans=0
    t=0
    for p,g in sorted(zip(plantTime,growTime),key=lambda x:-x[1]):
        t+=p
        ans = max(ans,t+g)
    return ans



10/1 121. 买卖股票的最佳时机

1.笨办法 比较每个值和之后的最大值的差值
2.记录最低点的值 遍历数组获得当前值与最低值的差值
实时更新最低点文章来源地址https://www.toymoban.com/news/detail-728564.html

def maxProfit(prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    res = 0
    for i in range(len(prices)-1):
        if max(prices[i+1:])-prices[i]>res:
            res = max(prices[i+1:])-prices[i]
    return res

def maxProfit2(prices):
    """
    :type prices: List[int]
    :rtype: int
    """
    if len(prices)==0:
            return 0
    res = 0
    minvalue = prices[0]
    for i in range(1,len(prices)):
        minvalue = min(minvalue,prices[i])
        res = max(res,prices[i]-minvalue)
    return res



到了这里,关于LeetCode 每日一题 2023/9/25-2023/10/1的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 2023-08-10LeetCode每日一题(下降路径最小和 II)

    点击跳转到题目位置 给你一个 n x n 整数矩阵 grid ,请你返回 非零偏移下降路径 数字和的最小值。 非零偏移下降路径 定义为:从 grid 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。 示例 1: 示例 2: 提示: n == grid.length == grid[i].

    2024年02月13日
    浏览(34)
  • Leetcode每日一题:1289. 下降路径最小和 II(2023.8.10 C++)

    目录 1289. 下降路径最小和 II 题目描述: 实现代码与解析: 动态规划 原理思路:         给你一个  n x n  整数矩阵  grid  ,请你返回  非零偏移下降路径  数字和的最小值。 非零偏移下降路径  定义为:从  grid  数组中的每一行选择一个数字,且按顺序选出来的数字

    2024年02月13日
    浏览(29)
  • (链表) 剑指 Offer 25. 合并两个排序的链表 ——【Leetcode每日一题】

    难度:简单 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。 示例1: 输入:1-2-4, 1-3-4 输出:1-1-2-3-4-4 限制 : 0 = 链表长度 = 1000 注意:本题与 21. 合并两个有序链表 相同 💡思路: 法一:递归 将该问题可以分解成子链表,只比较当前 l1 链

    2024年02月15日
    浏览(32)
  • LeetCode 每日一题 2023/8/7-2023/8/13

    记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步 8/7 344. 反转字符串 双指针 8/8 1749. 任意子数组和的绝对值的最大值 记录最小值 最大值 8/9 1281. 整数的各位积和之差 按要求计算 8/10 1289. 下降路径最小和 II 从上到下遍历每一行 在每一

    2024年02月13日
    浏览(44)
  • LeetCode 每日一题 2023/7/24-2023/7/30

    记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步 7/24 771. 宝石与石头 将宝石类型放入set中 一次判断石头中宝石个数 7/25 2208. 将数组和减半的最少操作次数 大顶堆记录当前最大值 每次取最大值减半 7/26 2569. 更新数组后处理求和查询

    2024年02月15日
    浏览(36)
  • LeetCode 每日一题 2023/8/14-2023/8/20

    记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步 8/14 617. 合并二叉树 dfs深搜 8/15 833. 字符串中的查找与替换 op存放该位置能替换的数值 从头遍历每个位置 8/16 2682. 找出转圈游戏输家 模拟 8/17 1444. 切披萨的方案数 动态规划 dp[k][i][j] 表

    2024年02月12日
    浏览(39)
  • 2023-05-21 LeetCode每日一题(蓄水)

    LCP 33. 蓄水 点击跳转到题目位置 给定 N 个无限容量且初始均空的水缸,每个水缸配有一个水桶用来打水,第 i 个水缸配备的水桶容量记作 bucket[i]。小扣有以下两种操作: 升级水桶:选择任意一个水桶,使其容量增加为 bucket[i]+1 蓄水:将全部水桶接满水,倒入各自对应的水缸

    2024年02月05日
    浏览(31)
  • 2023-08-27 LeetCode每日一题(合并区间)

    点击跳转到题目位置 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 示例 1: 示例 2: 提示: 1 = intervals.length = 10 4 intervals[i].length == 2 0 = s

    2024年02月10日
    浏览(35)
  • 2023-08-28 LeetCode每日一题(插入区间)

    点击跳转到题目位置 给你一个 无重叠的 ,按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。 示例 1: 示例 2: 示例 3: 示例 4: 示例 5: 提示: 0 = intervals.length = 10 4 interval

    2024年02月11日
    浏览(35)
  • 【LeetCode每日一题合集】2023.7.3-2023.7.9

    445. 两数相加 II 这道题目考察的实际知识点是高精度加法。更多关于高精度计算的内容参见:【算法基础】1.4 高精度(模拟大数运算:整数加减乘除) 2679. 矩阵中的和 读懂题意,每次操作会删去每一行中的当前最大值,同时将这些行最大值中的最大值加入最后的分数。 为了

    2024年02月13日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包