仅以此纪录每日LeetCode所刷题目(这个周末有一场力扣杯、一场周赛、一场双周赛,真是猪脑过载)。
题目描述:
示例:
思路:
这道题我们直接模拟即可,需要注意在计算宝石的一半时,需要向下取整。
代码:
class Solution:
def giveGem(self, gem: List[int], operations: List[List[int]]) -> int:
for i in range(len(operations)):
a = gem[operations[i][0]] // 2
gem[operations[i][0]] -= a
gem[operations[i][1]] += a
a = sorted(gem)
return a[-1] - a[0]
题目描述:
示例:
思路:
这道题测试用例的范围是0-8,而且题目也明确提示了一道菜只能使用一遍,不能重复使用。因此我们可以使用暴力解法,将所有的可能依次进行枚举,在满足饱腹的情况下返回最大的美味度。
这是我在之前的博客中写到的一个库函数,作用是用来进行枚举。在本题中使用过。?
代码:
class Solution:
def perfectMenu(self, materials: List[int], cookbooks: List[List[int]], attribute: List[List[int]], limit: int) -> int:
length = len(cookbooks)
data = []
b_arr = []
max_arr = []
for i in range(length):
data.append(i)
for i in range(len(data)):
for j in combinations(data, i+1):
count = [0]*len(materials)
b = 0
m = 0
for w in j:
b += attribute[w][1]
m += attribute[w][0]
for k in range(len(materials)):
count[k] += cookbooks[w][k]
for a in range(len(count)):
if count[a] > materials[a]:
break
if a == len(count)-1:
max_arr.append(m)
b_arr.append(b)
if max_arr == []:
return -1
max_return = 0
for i in range(len(b_arr)):
if b_arr[i] >= limit and max_arr[i] > max_return:
max_return = max_arr[i]
if max_return == 0:
return -1
else:
return max_return
题目描述:
示例:
思路:
真不太会 以后再研究 要睡觉了
代码:
class Solution:
def getNumber(self, root: Optional[TreeNode], ops: List[List[int]]) -> int:
lst=[]
def order(root):
if not root:
return
order(root.left)
lst.append(root.val)
order(root.right)
order(root)
red_set=0
for i in ops[::-1]:
start,end=i[1],i[2]
b1=bisect_left(lst,start)
b2=bisect_right(lst,end)
if i[0]==1:
red_set+=b2-b1
lst=lst[:b1]+lst[b2:]
return red_set
题目描述:
示例:
思路:
这道题直接进行枚举即可,排除一种铅笔和钢笔都买不起的情况,我们返回1。其余的我们依次计算买不同的钢笔情况下能买铅笔的个数,要注意买0支也是一种情况,因此不要少算1。返回结果即可。
代码:
class Solution:
def waysToBuyPensPencils(self, total: int, cost1: int, cost2: int) -> int:
if cost1 > total and cost2 > total:
return 1
else:
a = total // cost1
list1 = []
for i in range(a+1):
b = (total - (i*cost1))//cost2
list1.append(b+1)
count = 0
for j in list1:
count += j
return count
题目描述:
示例:
思路:
我在类中存储了一个整数变量来表示ATM机器中的余额,这样可以快速判断是否能取钱。同时存放了一个长度为五的一维数组,里面表示不同面值钞票的剩余数量。在取钱函数中我们需要注意的是:即使余额够,但是因为钞票的数额可能不能满足我们想取出来的数额,所以有的取钱操作不能正常的进行。
代码:
class ATM:
def __init__(self):
self.count = 0
self.a = [0,0,0,0,0]
def deposit(self, banknotesCount: List[int]) -> None:
for i in range(5):
self.a[i] += banknotesCount[i]
self.count += 20*banknotesCount[0] + 50*banknotesCount[1] + 100*banknotesCount[2] + 200*banknotesCount[3] + 500*banknotesCount[4]
def withdraw(self, amount: int) -> List[int]:
if self.count >= amount:
self.count -= amount
a4 = amount // 500
if self.a[4] >= a4:
self.a[4] -= a4
amount -= a4*500
else:
a4 = self.a[4]
amount -= self.a[4]*500
self.a[4] = 0
a3 = amount // 200
if self.a[3] >= a3:
self.a[3] -= a3
amount -= a3*200
else:
a3 = self.a[3]
amount -= self.a[3]*200
self.a[3] = 0
a2 = amount // 100
if self.a[2] >= a2:
self.a[2] -= a2
amount -= a2*100
else:
a2 = self.a[2]
amount -= self.a[2]*100
self.a[2] = 0
a1 = amount // 50
if self.a[1] >= a1:
self.a[1] -= a1
amount -= a1*50
else:
a1 = self.a[1]
amount -= self.a[1]*50
self.a[1] = 0
a0 = amount // 20
if self.a[0] >= a0:
self.a[0] -= a0
amount -= a0*20
else:
a0 = self.a[0]
amount -= self.a[0]*20
self.a[0] = 0
if amount > 0:
self.a[0] += a0
self.a[1] += a1
self.a[2] += a2
self.a[3] += a3
self.a[4] += a4
self.count += a0*20+a1*50+a2*100+a3*200+a4*500
return [-1]
return [a0,a1,a2,a3,a4]
else:
return [-1]
题目描述:
示例:
思路:
这道题直接模拟即可。
代码:
class Solution:
def digitSum(self, s: str, k: int) -> str:
while True:
length = len(s)
if length <= k:
return s
list1 = []
if length % k == 0:
for i in range(length // k):
list1.append(s[k*i:k*i+k])
else:
for i in range((length // k) + 1):
list1.append(s[k*i:k*i+k])
list2 = []
for j in list1:
count = 0
for a_k in j:
count += int(a_k)
list2.append(str(count))
s = ""
for a in list2:
s += a
题目描述:
示例:
思路:
这道题首先使用字典将每个任务对应的个数进行存储,若出现只有一次的任务,则返回-1,依次遍历字典中的任务,如下代码是比较容易理解的。
代码:
class Solution:
def minimumRounds(self, tasks: List[int]) -> int:
dict1 = {}
for i in tasks:
if i in dict1:
dict1[i] += 1
else:
dict1[i] = 1
count = 0
for i in dict1:
if dict1[i] == 1:
return -1
while dict1[i] > 0:
if dict1[i] == 2:
dict1[i] -= 2
count += 1
break
if dict1[i] % 3 == 1:
dict1[i] -= 2
count += 1
else:
a = dict1[i] // 3
dict1[i] -= a*3
count += a
return count
|