topic:
answer:
I won’t
Code:
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 }}
I won’t
1 | class Solution: |
maintain 3 indivual multiset:lower(Minimum kkk indivual数)、middle(Number in the middle)、upper(Most kkk indivual数)。
·if num≤max(lower),Then lowerInsert num
·if num≥min(upper),Then upper Insert num
·otherwise,exist middle Insert num
if插入后,lower or upper There are more elements than k indivual,Then middle middle Transfer element
操作过程middlemaintain middle 的element和 sum
·设删除的element为 d
·d 一定存exist于 lower ormiddle or upper middle的一indivualor多indivual集合middle
·Choose one delete
if删除后,lower or upper middle的element少于 k indivual,Then from middle middle Obtain element
操作过程middlemaintain middle 的element和 sum
average value = sum/(m−2⋅k)sum / (m - 2\cdot k)sum/(m−2⋅k) (Take down)。
Code with problems:
1 | class MKAverage: |
1828. Statistics the number of a circle mid -point
Today’s question is very simple,I wonder if it is against yesterday’s question bidding。
Askqueries
There are a few in the circlepoints
Points in an array。
To be honest, I was scared,I think this question is the question of the picture again。不过仔细读题了后发现只是一道简单的math题,
We can use European -style distance to solve(Time complexity isOn^2,I thought I had a better solution,At first glance everyone is like this())
The formula of the European -style distance is as follows:
$\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$
We look at the code for specific operations:
1 | class Solution: |
Take a look at someone who can’t understandpythonCode:
1 | class Solution: |
Give you a linked list,Delete the countdown of the linked list n
Node,And return to the head point of the linked list。
Exemplary example 1:
enter:head = [1,2,3,4,5], n = 2 Output:[1,2,3,5]
Exemplary example 2:
enter:head = [1], n = 1 Output:[]
Exemplary example 3:
enter:head = [1,2], n = 1 Output:[1]
hint:
sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Advance:Can you try to use a scanning implementation??
19.Delete the countdown of the linked listNNode.md
It is definitely possible to solve the problem by taking dual traversal,但topic要求我们一次遍历解决问题,Then we have to diverge on our ideas。
我们可以设想假设设定了Double pointer p and q if,when q Pointed to the end NULL,p and q The number of elements separated from each other is n hour,Then delete p The next pointer completes the requirements。
Set up virtual nodes dummyHead direction head
设定Double pointer p and q,初始都direction虚拟节点 dummyHead
move q,until p and q The number of elements separated from each other is n
同hourmove p and q,until q direction的为 NULL
Will p 的下一Nodedirection下下Node
1 | class Solution: |
1 | class Solution { |
Course:
1 | def rearrangeArray(self, nums: List[int]) -> List[int]: |
Given n
The array of a positive integer and a positive integer target
。
Find out the array to satisfy it ≥ target
The smalle Continuous array [numsl, numsl+1, ..., numsr-1, numsr]
,And return its length。If there is no conditional sub -array,return 0
。
Exemplary example 1:
enter:target = 7, nums = [2,3,1,2,4,3]
Output:2
explain:Sub -array [4,3]
是该条件下的Small length array。
Exemplary example 2:
enter:target = 4, nums = [1,4,4] Output:1
Exemplary example 3:
enter:target = 11, nums = [1,1,1,1,1,1,1,1] Output:0
hint:
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105
Advance:
O(n)
Time complexity solution, Please try to design a O(n log(n))
Time complexity solution。Originally planned to use double pointers,But my double pointer is over time。
1 | class Solution: |
217. Existing duplicate elements
For the first time**C++**Write a question
Two methods:
nums
in usevector
,So it can be used directlysort()
function。unordered_set<int>
function新建Hash table,Re -usefind()
andend()
判断是否已经存exist。1 | class Solution { |
1 | class Solution { |
1 | class Solution: |
217. Existing duplicate elements
For the first time**C++**Write a question
Two methods:
nums
in usevector
,So it can be used directlysort()
function。unordered_set<int>
function新建Hash table,Re -usefind()
andend()
判断是否已经存exist。1 | class Solution { |
1 | class Solution { |
1 | class Solution: |
This question was done on the phone at first,直接用的是双Hash table,An element,One depositindex,When a certain element exceeds2Judging whether there are two of them when they areindexSubtission is equal tokCase。So the complexity of time isO(n^3)
。
1 | class Solution: |
look@Gongshui SanyeGongshui Sanye的解法后,Only think of,这道题的目的是为了练习双指针在Hash table中的应用,TAThe original words are sorting the meaning:Whether the existence does not exceed k+1k + 1k+1 window,window内有相同元素。
1 | #Sort out the meaning:Whether the existence does not exceed k+1k + 1k+1 window,window内有相同元素。 |
可以使用Dynamic planning降低时间复杂度。we use dp(i,j) Express (i,j) The lower right corner,Only contain 1 The maximum value of the side length of the square。If we can calculate all dp(i,j) Value,Then the maximum value is that it is only included in the matrix 1 The maximum value of the side length of the square,其平方即为Maximum square的面积。
So how to calculate dp Each element value?For each position (i,j),检查在矩阵中该位置Value:
if该位置Value是 0,but dp(i,j)=0,Because the current location cannot 1 Folk formation of composition;
if该位置Value是 1,but dp(i,j) Value由其上方、Three adjacent locations on the left and upper left dp Value decision。in particular,The element value of the current position is equal to the minimum value of the three adjacent elements 1,The state transfer equation is as follows:
dp(i,j)=min(dp(i−1,j),dp(i−1,j−1),dp(i,j−1))+1
also,You also need to consider boundary conditions。if i and j At least one of them is 0,but以位置(i,j) The lower right corner的Maximum square的边长只能是 1,therefore dp(i,j)=1
1 | package main |