当前位置:网站首页>Leetcode notes: biweekly contest 69
Leetcode notes: biweekly contest 69
2022-06-12 07:53:00 【Espresso Macchiato】
1. Topic 1
The link to question 1 is as follows :
1. Their thinking
It's nothing , It is to divide words according to the meaning of the title and then convert the format of each word .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def capitalizeTitle(self, title: str) -> str:
words = title.split()
for i, w in enumerate(words):
if len(w) <= 2:
words[i] = w.lower()
else:
words[i] = w[0].upper() + w[1:].lower()
return " ".join(words)
The submitted code was evaluated : Time consuming 33ms, Take up memory 14.1MB.
2. Topic two
The link to question 2 is as follows :
1. Their thinking
My thinking on this question is very violent , Just use one first list Re write down all val, Then you can quickly find all pair 了 , You can find the maximum value .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def pairSum(self, head: Optional[ListNode]) -> int:
vals = []
while head:
vals.append(head.val)
head = head.next
n = len(vals)
return max(vals[i] + vals[n-1-i] for i in range(n//2))
The submitted code was evaluated : Time consuming 1161ms, Take up memory 55MB.
3. Topic three
The link to question 3 is as follows :
1. Their thinking
This question is actually simple , Just use one first counter Count all the characters .
We want to form a palindrome , All you need is the opposite string , So we just have to find all the opposite combinations of strings , Then count the number of pair The number of .
The only thing to notice is that , about aa String of type , We need to look at it alone , Because for such characters , His opposite string is itself , Therefore, it can constitute pair Of its total 1/2.
More special , because aa The class string can be placed in the center to form itself pair, therefore , If there are odd numbers aa Type character , Then the result has to add 2.
2. Code implementation
give python The code implementation is as follows :
class Solution:
def longestPalindrome(self, words: List[str]) -> int:
cnt = Counter(words)
res = 0
mid = False
for w in cnt.keys():
if w == w[::-1]:
k = cnt[w]
res += 4*(k//2)
if k % 2 == 1:
mid = True
else:
k = min(cnt[w], cnt[w[::-1]])
res += 4 * k
cnt[w] -= k
if w[::-1] in cnt.keys():
cnt[w[::-1]] -= k
return res+2 if mid else res
The submitted code was evaluated : Time consuming 1384ms, Take up memory 38.5MB.
4. Topic four
The link to question 4 is as follows :
1. Their thinking
In fact, my thinking on this question is quite direct , Is to traverse all nodes first , Find all the places where stamps can be affixed and make a mark ; Then go through each position again , Check whether all nodes can be covered by at least one stamp .
however , The difficulty here is to say how to O ( 1 ) O(1) O(1) Confirm whether a certain position can be stamped and whether a certain position can be covered by a certain stamp within the time complexity of .
Fortunately, the two methods are unified , Is to examine the sum of elements within a rectangle , This can be achieved quickly through a cumulative array .
2. Code implementation
give python The code implementation is as follows :
class Solution:
def possibleToStamp(self, grid: List[List[int]], stampHeight: int, stampWidth: int) -> bool:
n, m = len(grid), len(grid[0])
cumsum = [[0 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n):
for j in range(m):
cumsum[i+1][j+1] = grid[i][j] + cumsum[i+1][j] + cumsum[i][j+1] - cumsum[i][j]
status = [[0 for _ in range(m)] for _ in range(n)]
for i in range(n-stampHeight+1):
for j in range(m-stampWidth+1):
if cumsum[i+stampHeight][j+stampWidth] + cumsum[i][j] - cumsum[i+stampHeight][j] - cumsum[i][j+stampWidth] != 0:
continue
status[i][j] = 1
for i in range(n-1):
status[i+1][0] += status[i][0]
for j in range(m-1):
status[0][j+1] += status[0][j]
for i in range(n-1):
for j in range(m-1):
status[i+1][j+1] += status[i+1][j] + status[i][j+1] - status[i][j]
def is_cover(i, j):
if i < stampHeight and j < stampWidth:
return status[i][j]
elif i < stampHeight:
return status[i][j] - status[i][j-stampWidth]
elif j < stampWidth:
return status[i][j] - status[i-stampHeight][j]
else:
return status[i][j] - status[i][j-stampWidth] - status[i-stampHeight][j] + status[i-stampHeight][j-stampWidth]
return all(is_cover(i, j) + grid[i][j] > 0 for i in range(n) for j in range(m))
The submitted code was evaluated : Time consuming 4392ms, Take up memory 55.8MB.
边栏推荐
- Voice assistant -- Qu -- query error correction and rewriting
- 2021.10.24-25 scientific research log
- R language uses rstudio to save visualization results as PDF files (export--save as PDF)
- LeetCode笔记:Biweekly Contest 79
- Ecmascript6 interview questions
- 数值计算方法 Chapter6. 解线性方程组的迭代法
- Seeking for a new situation and promoting development, the head goose effect of Guilin's green digital economy
- R语言使用neuralnet包构建神经网络回归模型(前馈神经网络回归模型),计算模型在测试集上的MSE值(均方误差)
- Windows10 configuration database
- 石油储运生产 2D 可视化,组态应用赋能工业智慧发展
猜你喜欢

2021.11.3-7 scientific research log

Voice assistant - Introduction and interaction process

Unity uses shaders to highlight the edges of ugu I pictures

Process terminated

Topic 1 Single_ Cell_ analysis(4)

Dynamic simulation method of security class using Matlab based Matpower toolbox

20220526 yolov1-v5

Summary of machine learning + pattern recognition learning (IV) -- decision tree

Chapter 3 - Fundamentals of cryptography

Improvement of hash function based on life game
随机推荐
NaiveBayes function of R language e1071 package constructs naive Bayes model, predict function uses naive Bayes model to predict and reason test data, and table function constructs confusion matrix
@Datetimeformat @jsonformat differences
Process terminated
Exposure compensation, white increase and black decrease theory
移动端、安卓、IOS兼容性面试题
Utilize user behavior data
Work summary of the week from November 22 to 28, 2021
Generalized semantic recognition based on semantic similarity
2021.10.27-28 scientific research log
2021.11.3-7 scientific research log
Vs2019 MFC IP address Control Control inherit cipaddressctrl class redessine
20220526 损失函数
Visual studio code batch comment and uncomment
最新hbuilderX编辑uni-app项目运行于夜神模拟器
10 lessons from the recommended system
Compiling principle on computer -- function drawing language (II): lexical analyzer
Utilize user tag data
Preliminary knowledge next New concepts such as isr/rsc/edge runtime/streaming in JS
Voice assistant -- Qu -- semantic role annotation and its application
R语言rnorm函数生成正太分布数据、使用epiDisplay包的summ函数计算向量数据的描述性统计汇总信息并可视化有序点图(名称、有效值个数、均值、中位数、标准差、最大值、最小值)