LeetCode 每日一题 2023/12/25-2023/12/31

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


目录

      • 12/25 1276. 不浪费原料的汉堡制作方案
      • 12/26 1349. 参加考试的最大学生数
      • 12/27 2660. 保龄球游戏的获胜者
      • 12/28 2735. 收集巧克力
      • 12/29 2706. 购买两块巧克力
      • 12/30 1185. 一周中的第几天
      • 12/31 1154. 一年中的第几天


12/25 1276. 不浪费原料的汉堡制作方案

一个奶酪就是一个汉堡
所以变成cheeseSlices个汉堡能否消耗掉tomatoslices片番茄
假设巨无霸x个 小皇堡y个
x+y=cheese
4x+2y = tomato
2x+2y=2cheese
2x = tomato-2
cheese
x = (tomato-2cheese)/2
y = 2
cheese-tomato/2
判断两个是否是整数

def numOfBurgers(tomatoSlices, cheeseSlices):
    """
    :type tomatoSlices: int
    :type cheeseSlices: int
    :rtype: List[int]
    """
    x = (tomatoSlices-2*cheeseSlices)//2
    y = cheeseSlices-x
    
    if x>=0 and y>=0 and 4*x+2*y==tomatoSlices:
        return [x,y]
    return []




12/26 1349. 参加考试的最大学生数

0代表坏座位 1代表座位可选
state[i]保存第i行的坐位状态
dfs(seat,i) 表示第i行开始坐位状态为seat能够容纳的最多学生

def maxStudents(seats):
    """
    :type seats: List[List[str]]
    :rtype: int
    """
    n = len(seats[0])
    state = []
    for s in seats:
        mask = 0
        for i,c in enumerate(s):
            if c=='.':
                mask |=1<<i
        state.append(mask)
    mem = {}
    
    def dfs(seat,i):
        ans = 0
        if (seat,i) in mem:
            return mem[(seat,i)]
        for mask in range(1<<n):
            if (seat|mask)!=seat or (mask&mask<<1):
                continue
            cnt = mask.bit_count()
            if i==len(state)-1:
                ans = max(ans,cnt)
            else:
                nxt = state[i+1]
                nxt &=~(mask<<1)
                nxt &=~(mask>>1)
                ans = max(ans,cnt+dfs(nxt,i+1))
        mem[(seat,i)]=ans
        return ans
    return dfs(state[0],0)



12/27 2660. 保龄球游戏的获胜者

分别计算两个玩家分数

def isWinner(player1, player2):
    """
    :type player1: List[int]
    :type player2: List[int]
    :rtype: int
    """
    s1=s2=0
    pre1,pre2 = 0,0
    for v in player1:
        if pre1==10 or pre2==10:
            s1+=2*v
        else:
            s1+=v
        pre1,pre2 = pre2,v
    pre1,pre2 = 0,0
    for v in player2:
        if pre1==10 or pre2==10:
            s2+=2*v
        else:
            s2+=v
        pre1,pre2 = pre2,v
    if s1>s2:
        return 1
    elif s1<s2:
        return 2
    else:
        return 0



12/28 2735. 收集巧克力

最多移动n次 判断所有情况

def minCost(nums, x):
    """
    :type nums: List[int]
    :type x: int
    :rtype: int
    """
    n = len(nums)
    minl = nums[:]
    ans = sum(minl)
    for t in range(1,n):
        for i in range(n):
            minl[i] = min(minl[i],nums[(i-t)%n])
        ans = min(ans,t*x+sum(minl))
    return ans
    



12/29 2706. 购买两块巧克力

找到最便宜的两块巧克力

def buyChoco(prices, money):
    """
    :type prices: List[int]
    :type money: int
    :rtype: int
    """
    one,two = min(prices[:2]),max(prices[:2])
    for v in prices[2:]:
        if v<=one:
            one,two = v,one
        elif v<two:
            two = v
    return money if one+two>money else money-one-two




12/30 1185. 一周中的第几天

1970年12月31日是周四 根据week 起始位置为4
计算当前年份之前所有天数 每四年闰年+1
判断当前年之前月份的天数 如果当年为闰年+1
加上天数

def dayOfTheWeek(day, month, year):
    """
    :type day: int
    :type month: int
    :type year: int
    :rtype: str
    """
    week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
    start = 4
    days =start+ 365 * (year - 1971) + (year - 1969) // 4
    days += sum(monthDays[:month-1])
    if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) and month >= 3:
        days += 1
    days += day
    return week[days%7]




12/31 1154. 一年中的第几天

分别获取年月日 判断是否是润年
将月份之前的天数累加 再加上当前月份天数

def dayOfYear(date):
    """
    :type date: str
    :rtype: int
    """
    monthday = [31,28,31,30,31,30,31,31,30,31,30]
    l = date.split("-")
    year = int(l[0])
    if year%4==0 and (year%100!=0 or year==2000):
            monthday[1]=29
    ans =0
    month = int(l[1])
    if month>1:
        ans += sum(monthday[:month-1])
    day = int(l[2])
    ans +=day
    return ans



你可能感兴趣的:(Exercise,leetcode,算法)