1 | 350. The intersection of two array II |
1 | class Solution: |
1 | # Sort out the meaning:Whether the existence does not exceed k+1k + 1k+1 window,window内有相同元素。 |
&&Operate
1 | class Solution: |
My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.
{{ date }}
{{ time }}
1 | 350. The intersection of two array II |
1 | class Solution: |
1 | # Sort out the meaning:Whether the existence does not exceed k+1k + 1k+1 window,window内有相同元素。 |
&&Operate
1 | class Solution: |
I have been with yesterday’s question,Find by the way
What is deducted is a very strange oneprecompiled.listnode.ListNode
,But mine is'__main__.ListNode'
。
Because:Repeatedly defined$ListNode$
不反转Linkedof方法可能就是昨天of那种,转化为math做。
The code is still0x3fof
and昨天ofanswer法一样
1 | class Solution: |
“””
Give two forks search Root node,The node value of the tree is different,Please convert it to a cumulative tree(Greater Sum Tree),Make each node node
The new value of the original tree is greater than or equal to the original tree node.val
The sum of the value。
remind,二叉searchTree满足下列约束条件:
Notice:This question 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ same
Exemplary example 1:
enter:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] Output:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Exemplary example 2:
enter:root = [0,null,1] Output:[1,null,1]
Exemplary example 3:
enter:root = [1,0,2] Output:[3,3,2]
Exemplary example 4:
enter:root = [3,2,4,1] Output:[7,9,4,10]
hint:
0
and 104
between。-104
and 104
between。method one:Travel in the preface
Thinking and algorithm
本题中要求我们将每个节点的值修改为原来的节点值加上所有more than the它的节点值之and。这样我们只需要Travel in the preface该二叉searchTree,记录过程中的节点值之and,Not continuously update the node value of the nodes you currently traversed,即可得到topic要求的累加Tree。
Method Two:Morris Traversal
Thinking and algorithm
There is a clever way to be online,只占用常数空间来实现中序Traversal。This method is caused by J. H. Morris exist 1979 Annual thesis「Traversing Binary Trees Simply and Cheaply」Proposed for the first time,Because it is called Morris Traversal。
我们以一个简单的Binary tree为例进行说明。假设我们要进行中序Traversal的 Morris Traversal。
1 | 1 |
Initialization The current node is the root node(current = 1)。
When the current node is not empty,Execute the following steps:
1 | 1 |
1 | 1 |
The left node of the current node is empty。Output当前节点的值(4)。
Move the current node to its right node(current = 5)。
The left node of the current node is empty。Output当前节点的值(5)。
Move the current node to its right node(current = 2)。
1 | 1 |
1 | 1 |
1 | 1 |
The current node is empty,Traversal结束。
According to the above steps,通过修改节点between的指针关系,我们完成了对Binary tree的中序Traversal。
1 | class Solution: |
1 | class Solution: |
6275. Make all the minimum operations of all elements in the array II
Weekly Two Questions!!!
·First use the list derivative to calculate the difference between each element。
·Then,Initialize a counter to track the required operations required。
·The difference between traversing,And check whether each difference can be removed。
·if not,Then return -1,Because the array cannot be equal to the given operation。
·If it is eliminated,It removes the absolute value of the difference to add to the counter。
·at last,It returns the required number of operations。
for example:
[3,0,-6,3]
It is every positive number divided by3In harmony,Tested many times,是因为at last一种特殊情况(When two list items, etc.)No consideration。
1 | class Solution: |
At first glance, this question is a double pointer,But I am not familiar with the double pointer,Most of the time was wasted when you set up the framework。
This question is the third question of Zhou Sai,The first two questions were killed quickly,But because I slept,It’s only half an hour left when the computer is turned on。
always theredebug,Watch this afternoon0x3fSolution。There is a set of explanations of his sliding window algorithm and double pointer。
1 | from collections import Counter |
I met for the first time:’2023/3/19-16:51’
6323. Child that divides money the most.md
“————–”
This question is the first half of the year3月份的时候的周赛topic,topic链接:6323
“————–”
I found that this is a mathematical problem at the beginning,I wrote for a long time, I don’t know how to deal with the last money left。
So I wrote a list,One child and one child gives money。
math:
If the remaining 0 people,and money>0,Then you must be divided into one that has been divided 8 Dollar的people,
ans minus one。
If the remaining 1 people,and money=3,To avoid allocation 4 Dollar,
Then you must be divided into one that has been divided 8 Dollar的people,ans minus one。
其它情况全部给一个people,if这个people分配到 4 Dollar,
他再给另一个people 1 Dollar,so ans constant。
The following isylbExplanation of big guys:(2023-9-22renew)
if money<children,Then there must be children who do not share money,return −1。
if money>8×children,So children−1 A child obtained 8 Dollar,剩下的一A child obtained money−8×(children−1) Dollar,return children−1。
if money=8×children−4,So children−2 A child obtained 8 Dollar,The remaining two children sharing the rest 12 Dollar(As long as not 4, 8 Dollar就行),return children−2。
if,We assumed that there are x A child obtained 8 Dollar,Then the rest of the money is money−8×x,As long as it is guaranteed to be greater than equal to the remaining number of children children−x,You can satisfy the meaning。therefore,We just need to ask x Maximum value,That is the answer。
1 | class Solution: |
1 | class Solution: |
1 | class Solution: |
Originally intending to use the meta group one by one to find one by one,But timeout。
Then useintersectionFunction inO(1)
Solution at time complexity
1 | class Solution: |
6324. Maximize the great value of the array.md
Give you a bidding 0 Started integer array nums 。You need to nums Re -arrange into a new array perm 。
definition nums of Great value To satisfy 0 <= i < nums.length and perm[i] > nums[i] of下标数目。
Please return to re -arrangement nums 后of maximum Great value。
Exemplary example 1:
enter:nums = [1,3,5,2,1,3,1]
Output:4
explain:A optimal arrangement scheme is perm = [2,5,1,3,3,1,1] 。
The bidding is 0, 1, 3 and 4 Place,All perm[i] > nums[i] 。So we return 4 。
Three ways to think about this question:
The first is Tian Ji horse racing(I am): InumsContinuously reappear,Advance numbers,Then compare(time out)
Then there is a double pointer-Hash table:
每个元素只能被bigof元素指向一次(for example5Compare3big,3Can’t follow4对Compare了)
Two pointers point to the tail element at the same time,whenleft not less than rightof时候,left–。
else left– right– Counter– count ++
Last returncount
灵神ofTian Ji horse racing:
sort后直接在原数组上面找Comparewhen前元素bigof元素
1 | class Solution: |
1 | class Solution: |
1 | class Solution: |
6338. Number of methods of monkey collision
纯math问题,that is$2^n - 2$。butnAt most$10^9$So big,Return directly to timeout。
所以用了Fast power算法,
使用Fast power算法来降低计算 2^n Time complexity。
it’s here,We use variables x To store 2^n,And in each cycle x square。when n When you are a miracle,We will take the result to take the result x。This can be calculated in each cycle2^n。
Before returning the result,We need to use(res-2)%mod Remove the border situation
1 | class Solution: |
6331. The most prizes won in the two lines
make
make() yes Go Built -in function of language memory distribution,There are three parameters default。
1 | make(Type, len, cap) |
Type:type of data,Necessary parameters,Type 的值只能yes slice、 map、 channel 这三种type of data。
len:type of data实际占用的内存空间长度,map、 channel yesOptional parameter,slice yesNecessary parameters。
cap:为type of data提前预留的内存空间长度,Optional parameter。
所谓的提前预留yes当前为type of data申请内存空间的时候,Apply for additional memory space in advance,This can avoid the overhead brought by the second allocation of memory,Greatly improve the performance of the program。
You have such doubts when you see here,Since the size of the data has been specified at the time of initialization,Then why do you still specify the reserved size??
这yes因为make()
使用的yes一种动态数组算法,At first apply to the operating system for a small piece of memory,
这个就yescap
,waitcap
quiltlen
After the occupation is full, you need to expand the capacity,
扩容就yes动态数组再去向操作系统申请当前长度的两倍的内存,Then copy the old data to the new memory space。
1 | func maximizeWin(prizePositions []int, k int) (ans int) { |