文章目录
- [6060. 找到最接近 0 的数字](https://leetcode-cn.com/problems/find-closest-number-to-zero/)
- [2240. 买钢笔和铅笔的方案数](https://leetcode-cn.com/problems/number-of-ways-to-buy-pens-and-pencils/)
- [2241. 设计一个 ATM 机器](https://leetcode-cn.com/problems/design-an-atm-machine/)
- [6063. 节点序列的最大得分](https://leetcode-cn.com/problems/maximum-score-of-a-node-sequence/)
- 总结
6060. 找到最接近 0 的数字
一行
class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
return max([x if abs(x)==abs(min(nums,key=abs)) else -2e5 for x in nums])
2240. 买钢笔和铅笔的方案数
一行
class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
return sum((total-cost1*i)//cost2+1 for i in range(total//cost1+1))
2241. 设计一个 ATM 机器
numpy,十行
import numpy as np
a=[20,50,100,200,500]
class ATM:
def __init__(self):
self.nums=np.zeros(5).astype('int64')
def deposit(self, banknotesCount: List[int]) -> None:
self.nums+=np.array(banknotesCount)
def withdraw(self, amount: int) -> List[int]:
res=[0]*5
for i in range(4,-1,-1):
res[i]=min(amount//a[i],self.nums[i])
amount-=res[i]*a[i]
if amount>0:return [-1]
self.nums-=np.array(res)
return [*map(int,iter(res))]
# Your ATM object will be instantiated and called as such:
# obj = ATM()
# obj.deposit(banknotesCount)
# param_2 = obj.withdraw(amount)
6063. 节点序列的最大得分
灵神题解,改写了一行部分,七行代码👇
class Solution:
def maximumScore(self, scores: List[int], edges: List[List[int]]) -> int:
g = [[] for _ in range(len(scores))]
for x, y in edges:
g[x].append((scores[y], y))
g[y].append((scores[x], x))
for i, vs in enumerate(g):
g[i] = nlargest(3, vs)
return -1 if not edges else max(chain(max(score_a + scores[x] + scores[y] + score_b if y != a != b != x else -1 for (score_a, a), (score_b, b) in product(g[x], g[y])) for x, y in edges))
总结
推荐学习:
T1:min函数key参数
T2:sum……for
T3:numpy,unpack
T4:product
本次周赛一共1+1+10+7=19
行,完成【20行完成周赛】的目标!