LeetCode 每日一题 2025/10/20-2025/10/26
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
- 10/20 2011. 执行操作后的变量值
- 10/21 3346. 执行操作后元素的最高频率 I
- 10/22 3347. 执行操作后元素的最高频率 II
- 10/23 3461. 判断操作后字符串中的数字是否相等 I
- 10/24 2048. 下一个更大的数值平衡数
- 10/25 1716. 计算力扣银行的钱
- 10/26 2043. 简易银行系统
10/20 2011. 执行操作后的变量值
遍历 检查第2位可以判断是加还是减
def finalValueAfterOperations(operations):""":type operations: List[str]:rtype: int"""ans = 0for op in operations:if op[1]=="+":ans+=1else:ans-=1return ans
10/21 3346. 执行操作后元素的最高频率 I
从小到大排序 统计每个数x出现的频率cnt[x]
遍历操作后元素的值t 可以变为t的数值为[t-k,t+k]
二分统计可以变为t的元素区间内元素总数 r-l+1
因为操作次数有限 每次操作可以得到一个t加上原有的t Operations+cnt[t]
两者取较小值
def maxFrequency(nums, k, numOperations):""":type nums: List[int]:type k: int:type numOperations: int:rtype: int"""import bisectnums.sort()ans=0cnt={}ind=0n=len(nums)for i in range(n):if nums[i]!=nums[ind]:cnt[nums[ind]]=i-indans=max(ans,i-ind)ind=icnt[nums[ind]]=n-indans=max(ans,i-ind)for t in range(nums[0],nums[-1]+1):l=bisect.bisect_left(nums,t-k)r=bisect.bisect_right(nums, t+k)-1cur=min(r-l+1,cnt.get(t,0)+numOperations)ans=max(ans,cur)return ans
10/22 3347. 执行操作后元素的最高频率 II
https://leetcode.cn/problems/maximum-frequency-of-an-element-after-performing-operations-ii/solutions/3803631/zhi-xing-cao-zuo-hou-yuan-su-de-zui-gao-n4uko/?envType=daily-question&envId=2025-10-22
def maxFrequency(nums, k, numOperations):""":type nums: List[int]:type k: int:type numOperations: int:rtype: int"""from collections import defaultdictimport bisectnums.sort()ans = 0num_count = defaultdict(int)modes = set()def add_mode(value):modes.add(value)if value - k >= nums[0]:modes.add(value - k)if value + k <= nums[-1]:modes.add(value + k)last_num_index = 0for i in range(len(nums)):if nums[i] != nums[last_num_index]:num_count[nums[last_num_index]] = i - last_num_indexans = max(ans, i - last_num_index)add_mode(nums[last_num_index])last_num_index = inum_count[nums[last_num_index]] = len(nums) - last_num_indexans = max(ans, len(nums) - last_num_index)add_mode(nums[last_num_index])for mode in sorted(modes):l = bisect.bisect_left(nums, mode - k)r = bisect.bisect_right(nums, mode + k) - 1if mode in num_count:temp_ans = min(r - l + 1, num_count[mode] + numOperations)else:temp_ans = min(r - l + 1, numOperations)ans = max(ans, temp_ans)return ans
10/23 3461. 判断操作后字符串中的数字是否相等 I
遍历
def hasSameDigits(s):""":type s: str:rtype: bool"""l=[int(s[i]) for i in range(len(s))]while len(l)>2:tmp=[]for i in range(len(l)-1):tmp.append((l[i]+l[i+1])%10)l=tmp[:]return l[0]==l[1]
10/24 2048. 下一个更大的数值平衡数
朴实无华 打表
在范围内 平衡数是有限的
二分查找比n大的值
def nextBeautifulNumber( n):""":type n: int:rtype: int"""values = [1, 22, 122, 212, 221, 333, 1333, 3133, 3313, 3331, 4444,14444, 22333, 23233, 23323, 23332, 32233, 32323, 32332,33223, 33232, 33322, 41444, 44144, 44414, 44441, 55555,122333, 123233, 123323, 123332, 132233, 132323, 132332,133223, 133232, 133322, 155555, 212333, 213233, 213323,213332, 221333, 223133, 223313, 223331, 224444, 231233,231323, 231332, 232133, 232313, 232331, 233123, 233132,233213, 233231, 233312, 233321, 242444, 244244, 244424,244442, 312233, 312323, 312332, 313223, 313232, 313322,321233, 321323, 321332, 322133, 322313, 322331, 323123,323132, 323213, 323231, 323312, 323321, 331223, 331232,331322, 332123, 332132, 332213, 332231, 332312, 332321,333122, 333212, 333221, 422444, 424244, 424424, 424442,442244, 442424, 442442, 444224, 444242, 444422, 515555,551555, 555155, 555515, 555551, 666666, 1224444]l,r=0,len(values)-1while l<r:mid = (l+r)>>1if values[mid]<=n:l=mid+1else:r=midreturn values[l]
10/25 1716. 计算力扣银行的钱
第一周存 1+2+…+7=28
第二周存 2+3+…+8=35
第i周存 i+(i+1)+…+(i+6) = 21+7i
共有i周 总数=21*i+(1+2+…+i)i//27
剩余天数 第一天为i+1块
def totalMoney(n):""":type n: int:rtype: int"""week = n//7day = n%7ans = 0ans = 21*week+(1+week)*week*7//2ans += (week+1+week+day)*day//2return ans
10/26 2043. 简易银行系统
模拟
class Bank(object):def __init__(self, balance):""":type balance: List[int]"""self.num = len(balance)self.bl = balancedef transfer(self, account1, account2, money):""":type account1: int:type account2: int:type money: int:rtype: bool"""if max(account1,account2)>self.num or self.bl[account1-1]<money:return Falseself.bl[account1-1]-=moneyself.bl[account2-1]+=moneyreturn Truedef deposit(self, account, money):""":type account: int:type money: int:rtype: bool"""if account>self.num:return Falseself.bl[account-1]+=moneyreturn Truedef withdraw(self, account, money):""":type account: int:type money: int:rtype: bool"""if account>self.num or self.bl[account-1]<money:return Falseself.bl[account-1]-=moneyreturn True
