数据规模->时间复杂度
<=10^4 😮(n^2)
<=10^7:o(nlogn)
<=10^8:o(n)
10^8<=:o(logn),o(1)
内容
lc 976 :三角形的最大周长
https://leetcode.cn/problems/largest-perimeter-triangle/
提示:
3 <= nums.length <= 10^4
1 <= nums[i] <= 10^6
class Solution:
def largestPerimeter(self, nums: List[int]) -> int:
nums.sort()
for i in range(len(nums)-1,-1,-1):
if i>1 and nums[i-2]+nums[i-1]>nums[i]:
return nums[i-2]+nums[i-1]+nums[i]
return 0
lc 674 :最长连续递增序列
https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
提示:
1 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
max_num=0
slow=fast=0
while fast<len(nums):
if fast>0 and nums[fast-1]>=nums[fast]:
slow=fast
max_num=max(max_num,fast-slow+1)
fast+=1
return max_num
lc 738 :单调递增的数字
https://leetcode.cn/problems/monotone-increasing-digits/
提示:
0 <= n <= 10^9
class Solution:
def monotoneIncreasingDigits(self, n: int) -> int:
#例如:2332->2299
strn=[int(i) for i in str(n)]#区别:list(str(n)) ["1","0"]
#
i=1
while i<len(strn) and strn[i-1]<=strn[i]:i+=1 #指向233“2”
#
if i<len(strn):
while i>0 and strn[i-1]>strn[i]:#指向2‘2’
strn[i-1]-=1
i-=1
#
for j in range(i+1,len(strn)):
strn[j]=9
#
res=0
for i in strn:
res=res*10+i
return res
#
else:return n
lc 134 : 加油站
https://leetcode.cn/problems/gas-station/
提示:
gas.length == n
cost.length == n
1 <= n <= 10^5
0 <= gas[i], cost[i] <= 10^4
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
sum_gas=curr_gas=0
start_index=0
for i in range(len(gas)):
sum_gas+=gas[i]-cost[i]
#key
curr_gas+=gas[i]-cost[i]
if curr_gas<0:
start_index=i+1
curr_gas=0 #key
return start_index if sum_gas>=0 else -1
lc 767 :重构字符串
https://leetcode.cn/problems/reorganize-string/
提示:
1 <= s.length <= 500
s 只包含小写字母
class Solution:
def reorganizeString(self, s: str) -> str:
#统计
cnt=[0]*26
for c in s:
cnt[ord(c)-ord('a')]+=1
if cnt[ord(c)-ord('a')]>(len(s)+1)//2:
return ''
#寻找最大
max_cnt_index=0
for i in range(26):
if cnt[i]>cnt[max_cnt_index]:
max_cnt_index=i
#填充1
res=['']*len(s)
idx=0
while cnt[max_cnt_index]>0:
cnt[max_cnt_index]-=1
res[idx]=chr(max_cnt_index+ord('a'))
idx+=2
#填充2
for j in range(26):
while cnt[j]>0:
if idx>=len(s):idx=1 #key
res[idx]=chr(j+ord('a'))
idx+=2
#
cnt[j]-=1
return ''.join(res)
lc 621【top100】: 任务调度器
https://leetcode.cn/problems/task-scheduler/
提示:
1 <= task.length <= 10^4
tasks[i] 是大写英文字母
n 的取值范围为 [0, 100]
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
cnt_tasks=[0]*26
#
max_task_length=tasks_num=0
for task in tasks:
cnt_tasks[ord(task)-ord('A')]+=1
#key:寻找最大任务长度"AAA"3及数量"AAABBB"2
if cnt_tasks[ord(task)-ord('A')]==max_task_length:
tasks_num+=1
elif cnt_tasks[ord(task)-ord('A')]>max_task_length:
max_task_length=cnt_tasks[ord(task)-ord('A')]
tasks_num=1
#
empty_num=(max_task_length-1)*(n-(tasks_num-1))#gap_num*gap_len
avail_num=len(tasks)-max_task_length*tasks_num
idle=max(0,empty_num-avail_num)
return len(tasks)+idle
lc 670 :最大交换
https://leetcode.cn/problems/maximum-swap/
注意:
给定数字的范围是 [0, 10^8]
class Solution:
#贪心;拿高位后面比高位大的值进行交换
def maximumSwap(self, num: int) -> int:
list_num=list(str(num))#区别list_num=[int(i) for i in str(num)]
last_appear=[0]*10 #key:记录最后出现的位置
for i in range(len(list_num)):
last_appear[ord(list_num[i])-ord('0')]=i
#
for i in range(len(list_num)):
for j in range(9,ord(list_num[i])-ord('0'),-1):
if last_appear[j]>i:#key:说明后面存在比前面大的数
list_num[i],list_num[last_appear[j]]=list_num[last_appear[j]],list_num[i]
#
return int(''.join(list_num))
return num
lc 861 :翻转矩阵后的得分
https://leetcode.cn/problems/score-after-flipping-matrix/
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 20
grid[i][j] 为 0 或 1
class Solution:
def matrixScore(self, grid: List[List[int]]) -> int:
rows,cols=len(grid),len(grid[0])
#行变换
for i in range(rows):
if grid[i][0]==0:
for j in range(cols):
grid[i][j]^=1#0->1,1->0
#统计与累计数
res=0
for j in range(cols):
#按列,统计1个数
cnt=0
for i in range(rows):
if grid[i][j]==1:
cnt+=1
#按列,累加数
max_cnt=max(cnt,rows-cnt)
res+=max_cnt*(1<<(cols-j-1))#1000->2^(4-0-1)
return res
lc 1029 :两地调度
https://leetcode.cn/problems/two-city-scheduling/
提示:
2 * n == costs.length
2 <= costs.length <= 100
costs.length 为偶数
1 <= aCosti, bCosti <= 1000
class Solution:
#选出 price_A - price_B 最小的 N 个人,让他们飞往 A 市,其余人飞往 B 市
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
costs.sort(key=lambda x:x[0]-x[1])#key
#
res=0
n=len(costs)//2
for i in range(n):
res+=costs[i][0]+costs[i+n][1]
return res
lc 330 :按要求补齐数组
https://leetcode.cn/problems/patching-array/
提示:
1 <= nums.length <= 1000
1 <= nums[i] <= 10^4
nums 按 升序排列
1 <= n <= 2^31 - 1
class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
res,x=0,1
i=0
while x<=n:
if i<len(nums) and nums[i]<=x:
x+=nums[i] #更新2:[1,x+x-1]已覆盖->[1,x+nums[i]-1]也覆盖
i+=1
else:#更新1
res+=1 #等价于:[1,x-1]已有->add x->[1,2*x-1]已覆盖
x*=2 #开始考虑2*x
return res