当前位置:网站首页>Leetcode daily question 2022/2/28-2022/3/6
Leetcode daily question 2022/2/28-2022/3/6
2022-07-22 19:13:00 【alphaTao】
The preliminary problem-solving ideas are recorded And local implementation code ; Not necessarily optimal I also hope you can discuss Progress together
Catalog
2/28 1601. The maximum number of transfer requests that can be reached
because requests Only up to 16 individual
We can enumerate all requests
Use one 16 The binary number of bits represents the case of fetching several requests 1 On behalf of the implementation request
After simulating each request If the situation is satisfied Then the maximum number of update requests
def maximumRequests(n, requests):
""" :type n: int :type requests: List[List[int]] :rtype: int """
ans = 0
m = len(requests)
def onenum(num):
one = 0
while num:
num&=(num-1)
one+=1
return one
for mask in range(1<<m):
num = onenum(mask)
if num<ans:
continue
l = [0]*n
for i,(x,y) in enumerate(requests):
if mask&(1<<i):
l[x]-=1
l[y]+=1
tag = True
for i in range(n):
if l[i]!=0:
tag = False
break
if tag:
ans = num
return ans
3/1 6. Z Font conversion
Location 0 Start calculating
The first j That's ok The first number is j There are two types of intervals step1=2*(n-j-1) step2 = 2*j
def convert(s, numRows):
""" :type s: str :type numRows: int :rtype: str """
if numRows==1:
return s
ans = ""
num = len(s)
for j in range(numRows):
step1 = 2*(numRows-j-1)
step2 = 2*j
if step1==0 or step2==0:
step1,step2=max(step1,step2),max(step1,step2)
steps = [step1,step2]
loc = j
v = 0
while loc<num:
ans += s[loc]
loc +=steps[v%2]
v+=1
return ans
3/2 564. Looking for the latest palindromes
If the number of digits changes The closest one is definitely 100…001 One less must be 99…99
Two cases can be specially considered
If the number of digits does not change In order to minimize the difference Definitely change the second half
The first half remains unchanged num=len(n) The first half is x=(num+1)//2 position
consider x-1,x,x+1 In three cases
def nearestPalindromic(n):
""" :type n: str :rtype: str """
num = len(n)
more = 10**num+1
less = 10**(num-1)-1
ans = ""
if len(n)==1:
return str(int(n)-1)
ori = int(n)
diff = min(more-ori,ori-less)
if more-ori<ori-less:
ans = str(more)
else:
ans = str(less)
half = int(n[:(num+1)//2])
for x in range(half-1,half+2):
if num%2==0:
y = x
else:
y = x//10
while y:
x = x*10+y%10
y//=10
if x==ori:
continue
print(x,abs(x-ori),diff,ans)
if abs(x-ori)<diff or(abs(x-ori)==diff and len(ans)>len(str(x))):
diff = abs(x-ori)
ans = str(x)
return ans
3/3 258. Members add
simulation Take everyone and
def addDigits(num):
""" :type num: int :rtype: int """
while num>9:
tmp = num
num = 0
while tmp>0:
num += tmp%10
tmp = tmp//10
return num
3/4 2104. Subarray range and
1. violence Traverse all subarrays
2. Monotonic stack
Contribution as a maximum :
Maintain a monotonic decreasing stack x,y (nums[x]>nums[y])
If at this time z The value of position is greater than y Need to deal with y
y Number of subarrays as the maximum (y-x)*(z-y)
Contribution: nums[y]* Number
As a minimum contribution similar Only the contribution is negative
def subArrayRanges(nums):
""" :type nums: List[int] :rtype: int """
n = len(nums)
ans = 0
for i in range(n-1):
maxv,minv = nums[i],nums[i]
for j in range(i+1,n):
maxv = max(maxv,nums[j])
minv = min(minv,nums[j])
ans += maxv-minv
return ans
def subArrayRanges2(nums):
""" :type nums: List[int] :rtype: int """
ans=0
stack = []
for i,num in enumerate(nums+[float('inf')]):
while stack and nums[stack[-1]]<num:
j = stack.pop()
left = stack[-1] if stack else -1
ans += nums[j]*(i-j)*(j-left)
stack.append(i)
stack = []
for i,num in enumerate(nums+[float('-inf')]):
while stack and nums[stack[-1]]>num:
j = stack.pop()
left = stack[-1] if stack else -1
ans -= nums[j]*(i-j)*(j-left)
stack.append(i)
return ans
3/5 521. The longest special sequence Ⅰ
If two strings are equal There is no
Otherwise, the longest string subsequence takes itself It must not be a subsequence of another
def findLUSlength( a, b):
""" :type a: str :type b: str :rtype: int """
if a==b:
return -1
return max(len(a),len(b))
3/6 2100. Suitable for bank robbery
front time Day non increasing after time Days are not decreasing
Consider the two conditions separately
qian[i] Used to record the position from front to back i The longest non progressive growth
hou[i] Used to record from i The longest non decreasing length in the future
def goodDaysToRobBank(security, time):
""" :type security: List[int] :type time: int :rtype: List[int] """
n = len(security)
qian,hou = [0]*n,[0]*n
for i in range(1,n):
if security[i]<=security[i-1]:
qian[i] = qian[i-1]+1
if security[n-i-1]<=security[n-i]:
hou[n-i-1]=hou[n-i]+1
ans = []
for i in range(time,n-time):
if qian[i]>=time and hou[i]>=time:
ans.append(i)
return ans
边栏推荐
- Kindling the Darkness: A Practical Low-light Image Enhancer
- Wechat scans the QR code of the website, but only displays the link address, and cannot jump to the web page
- 为什么重写equels方法一定要重写hashCode方法
- Tcpdump 简单用法
- IT外包服务业各领域的未来前景和趋势
- Programmer interview golden code interview question 01.05. primary editing
- 逻辑回归中的损失函数
- Rongyun x Xingzhi: exclusive Post-00 self social plot (including lottery)
- Cross entropy loss function
- Constructor
猜你喜欢
Kindling the Darkness: A Practical Low-light Image Enhancer
grafana面板-关于转换
Flink learning notes (IV) Flink runtime architecture
Oracle容器数据库的安装和使用
Design of hydraulic system for power slide of horizontal single side multi axis drilling combined machine tool
Programmer interview golden code interview question 01.04. palindrome arrangement
[QT source code reuse] simulate the pop-up mode of qcompleter
Rongyun x Xingzhi: exclusive Post-00 self social plot (including lottery)
Idea construit le projet jfinal + génération automatique de code + test de fonctionnement de la base de données (trois méthodes)
Numpy.reshape finish image cutting
随机推荐
MySQL execution process and sequence
Leetcode daily question 2021/12/13-2021/12/19
grafana面板-关于转换
【链表技巧汇总】141.环形链表(简单)
河北、浙江的气候变化了?
Design of hydraulic system for power slide of horizontal single side multi axis drilling combined machine tool
Help brand insight -- Analysis of consumers' emotional behavior
服务器磁盘IO性能调优
nacos持久化连接mysql数据库sm4加密方案
融云首席科学家任杰:历练出人才,职场「经历>经验」
助力品牌洞察——消费者情绪行为分析
LeetCode 每日一题 2021/11/22-2021/11/28
融云办政事: “小网格”也能实现“大治理”
JUC synchronizer
When pytorch customizes the dataloder, it returns parameters
IT外包服务业各领域的未来前景和趋势
Transformer, another city! The top of many low-level tasks was occupied, and Peking University Huawei and others jointly proposed the pre training model IPT
fucking-algorithm
[FatFs] porting FatFs file system based on STM32 SD card
Programmer interview golden code interview question 01.05. primary editing