记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
-
-
- [12/22 960. 删列造序 III](#12/22 960. 删列造序 III)
- [12/23 2054. 两个最好的不重叠活动](#12/23 2054. 两个最好的不重叠活动)
- [12/24 3074. 重新分装苹果](#12/24 3074. 重新分装苹果)
- [12/25 3075. 幸福值最大化的选择方案](#12/25 3075. 幸福值最大化的选择方案)
- [12/26 2483. 商店的最少代价](#12/26 2483. 商店的最少代价)
- [12/27 2402. 会议室 III](#12/27 2402. 会议室 III)
- 12/28
-
12/22 960. 删列造序 III
dp求最多能保留的列数
python
def minDeletionSize(strs):
"""
:type strs: List[str]
:rtype: int
"""
n=len(strs[0])
dp=[1]*n
for i in range(n-2,-1,-1):
for j in range(i+1,n):
if all(row[i]<=row[j] for row in strs):
dp[i] = max(dp[i],1+dp[j])
return n-max(dp)
12/23 2054. 两个最好的不重叠活动
将活动结束时间从小到大排序
递增栈保留当前活动结束前的活动(结束时间,价值)
python
def maxTwoEvents(events):
"""
:type events: List[List[int]]
:rtype: int
"""
import bisect
events.sort(key=lambda x:x[1])
ans=0
st=[(0,0)]
for s,e,v in events:
i = bisect.bisect_left(st, (s,))-1
ans = max(ans,st[i][1]+v)
if v>st[-1][1]:
st.append((e,v))
return ans
12/24 3074. 重新分装苹果
根据苹果总数 按照箱子从大到小选择
python
def minimumBoxes(apple, capacity):
"""
:type apple: List[int]
:type capacity: List[int]
:rtype: int
"""
s = sum(apple)
capacity.sort(reverse=True)
i=0
while s>0 and i<len(capacity):
s-=capacity[i]
i+=1
return i
12/25 3075. 幸福值最大化的选择方案
从大到小选择
python
def maximumHappinessSum(happiness, k):
"""
:type happiness: List[int]
:type k: int
:rtype: int
"""
happiness.sort(reverse=True)
ans=0
for i in range(k):
v = happiness[i]-i
if v<=0:
break
ans += v
return ans
12/26 2483. 商店的最少代价
关门前N的个数和关门后Y的个数
先统计Y的个数 从头遍历
cur为当前最低代价
python
def bestClosingTime(customers):
"""
:type customers: str
:rtype: int
"""
y,n=0,0
for c in customers:
if c=='Y':
y+=1
ans = 0
cur = y
for i,c in enumerate(customers):
if c=='Y':
y-=1
else:
n+=1
if cur>y+n:
cur=y+n
ans = i+1
return ans
12/27 2402. 会议室 III
最小堆ava,used分别存储当前可用和正在使用的会议室
将会议排序
依次遍历
当前会议[s,e) 当前时间需要调到s,
将s前结束的所有使用会议变更为可用
如果此时没有可用会议室 那么需要把当前时间调到 最先用完的会议室结束时间
取出排序最前的可用会议室 记录使用次数
python
def mostBooked(n, meetings):
"""
:type n: int
:type meetings: List[List[int]]
:rtype: int
"""
import heapq
meetings.sort()
ava=list(range(n))
heapq.heapify(ava)
used=[]
cnt=[0]*n
cur=0
for s,e in meetings:
cur=max(cur,s)
while used and used[0][0]<=cur:
_,r = heapq.heappop(used)
heapq.heappush(ava, r)
if not ava:
cur = used[0][0]
while used and used[0][0]<=cur:
_.r = heapq.heappop(used)
heapq.heappush(ava,r)
r=heapq.heappop(ava)
cnt[r]+=1
heapq.heappush(used,(cur+e-s,r))
return max(range(n),key=lambda x:cnt[x])
12/28
python