当前位置:网站首页>Leetcode notes: Weekly contest 298
Leetcode notes: Weekly contest 298
2022-06-23 06:40:00 【Espresso Macchiato】
- Match Links :https://leetcode.com/contest/weekly-contest-298
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
The idea of this question is relatively simple , Just record all the characters in the string first , Then traverse the characters in the reverse order of dictionary order , Just take a look at the first result where both case and case exist in the string .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def greatestLetter(self, s: str) -> str:
cnt = Counter(s)
for i in range(26):
l, u = chr(ord('a') + 25 - i), chr(ord('A') + 25 - i)
if l in cnt and u in cnt:
return u
return ""
The submitted code was evaluated : Time consuming 64ms, Take up memory 13.8MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
This question makes n Mantissa is k The sum of the numbers of must be exactly equal to num, Then there must be n*k And num Have the same mantissa , And the former is no greater than the latter .
Besides , If the above conditions are met , Then we can always construct a group with a length of n The mantissa of all numbers is k, And the sum is num.
in fact , Just allocate the remainder appropriately to this n The number can be increased .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
for i in range(1, 11):
a = i * k
if a <= num and a % 10 == num % 10:
return i
return -1
The submitted code was evaluated : Time consuming 75ms, Take up memory 13.8MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
The result of this question is in front of the train of thought 0 There can be any number of , But once you take one 1 after , Then the following possible permutations can only form one at most k, That is , hypothesis k The binary of is m position , Then the number of digits that can be used in the future can only be m position .
therefore , Sum up , We're just going to iterate through all the positions as the real valid start , under these circumstances , The result in each position is the one in front of it 0 The number of and the number that can be formed subsequently shall not be greater than k The maximum number of digits in the two-level system .
then , Let's look at how to find the maximum number of bits that can form a binary number from a certain position , This is actually simple , obviously , If the total number of digits is less than m, Then you can use all of them , If the number of digits is greater than m, There is also a need for classified discussion , If you can find n The number of substrings is less than k, So the result is k, conversely , The result is m-1.
2. Code implementation
give python The code implementation is as follows :
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
n = len(s)
kd = []
while k != 0:
kd.insert(0, k % 2)
k = k // 2
m = len(kd)
def get_max(idx):
if n - idx < m:
return n-idx
sub = s[idx:]
less, j = False, 0
for i, ch in enumerate(sub):
if j >= m:
break
if not less:
if int(sub[i]) > kd[j]:
continue
elif int(sub[i]) < kd[j]:
less = True
j += 1
return m if j >= m else m-1
res, cnt = 0, 0
for i, ch in enumerate(s):
if ch == "0":
cnt += 1
res = max(res, cnt)
else:
res = max(res, cnt + get_max(i))
return res
The submitted code was evaluated : Time consuming 429ms, Take up memory 14MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
My basic idea for this question is greedy Fill of , First , We put... According to the value of the unit square prices Sort , then greedy Use a square to fill .
The filling method is to say , Let's start with the current largest prices To fill the rectangle of the body , Then the filling of the remaining leftover materials .
The above solution can realize the functions we need , But there will be a timeout if it is not handled , Therefore, we need to prune on the basis of the above .
The idea of pruning is also relatively simple , First , If the row or column can be directly marked by the current largest price The rectangle completely covers , Then we can just use it directly , Only if it cannot be completely covered , It is possible for other rectangles to be filled to produce a larger total price The situation of .
therefore , We can reduce the complexity of the algorithm by pruning based on this , Pass the final test .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
prices = sorted(prices, key=lambda x: -x[2] / (x[0] * x[1]))
@lru_cache(None)
def dp(w, h, idx):
res = 0
for x, y, p in prices[idx:]:
if x > w or y > h:
continue
if p / (x * y) * (w * h) <= res:
break
if w % x == 0 and h % y == 0:
res = max(res, (w//x) * (h//y)*p)
break
elif w % x == 0:
i = w // x
for j in range(h//y, 0, -1):
s = i * j * p
wr, hr = w - x*i, h - y*j
res = max(res, s + dp(wr, h, idx+1) + dp(w-wr, hr, idx+1), s + dp(wr, h-hr, idx+1) + dp(w, hr, idx+1))
elif h % y == 0:
j = h // y
for i in range(w//x, 0, -1):
s = i * j * p
wr, hr = w - x*i, h - y*j
res = max(res, s + dp(wr, h, idx+1) + dp(w-wr, hr, idx+1), s + dp(wr, h-hr, idx+1) + dp(w, hr, idx+1))
else:
for i in range(w//x, 0, -1):
for j in range(h//y, 0, -1):
s = i * j * p
wr, hr = w - x*i, h - y*j
res = max(res, s + dp(wr, h, idx+1) + dp(w-wr, hr, idx+1), s + dp(wr, h-hr, idx+1) + dp(w, hr, idx+1))
return res
return dp(m, n, 0)
The submitted code was evaluated : Time consuming 1049ms, Take up memory 22MB.
边栏推荐
- C语言 获取秒、毫秒、微妙、纳秒时间戳
- Termux
- Leetcode topic resolution single number II
- Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
- Smart port: how to realize intelligent port supervision based on the national standard gb28181 protocol easygbs?
- I heard you want to embed ppt on WordPress website?
- 射频基础理论(dB)
- 业务逻辑安全思路总结
- Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)
- phpStudy设置301重定向
猜你喜欢

Programmers' real ideas | daily anecdotes

了解学习 JSX 的工作方式

剑指 Offer 42. 连续子数组的最大和

直播带货这么火,如何在小程序中实现视频通话及直播互动功能?

中台库存中的实仓与虚仓的业务逻辑设计

解读创客教育中的团结协作精神

QT中的item views与Item widgets控件的用法总结

Home address exchange

Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean

射频内容学习
随机推荐
Numerical calculation method chapter7 Calculating eigenvalues and eigenvectors of matrices
SAP execution transaction code mrrl error -no message was found for partner 100065-
Illuminate\Support\Collection 去重 unique 列表去重
vs+qt项目转qt creator
Test of ers function under the supplier consignment purchase mode of SAP mm
2.17 haas506 2.0 development tutorial system (only versions above 2.2 are supported)
Haas506 2.0 development tutorial - Advanced Component Library -modem Net (only supports versions above 2.2)
业务逻辑安全思路总结
There are so many code comments! I laughed
MySQL ON DUPLICATE KEY 和 PgSQL ON CONFLICT(主键) 处理主键冲突
How to build a data application system based on overall value for energy enterprises
Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean
Topic35——34. Find the first and last positions of elements in a sorted array
疫情下的传媒产业,小程序生态驱动数字化转型探索
English语法_副词 - ever / once
从 WAN 到 SD-WAN 边缘设备的网络架构
How to maintain secure encryption of email communication with FDA?
将TensorFlow1.x改写为pytorch
下载oss文件并修改文件名
Day_ 10 smart health project - permission control, graphic report