Leetcode周賽119
已經累計打了10幾場比賽了,之前忘記總結了,就從這一場開始總結吧。
本次比賽做出來2/4,前兩個題,第三個題沒想出思路,排名1700/3400,話說那些大佬真的是厲害,本次有400+大佬ak,強啊,最快的那個大佬在我剛做完第一題的時候就已經ak了,emmmmmm。
第一個:Squares of a Sorted Array(這個題相當簡單,就是簡單的做一個運算之後排個序就行了)
Given an array of integers A
sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
Example 1:
Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Example 2:
Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Note:
1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A
is sorted in non-decreasing order.
//程式碼呈上
class Solution {
public int[] sortedSquares(int[] A) {
//Arrays.sort(A);
int[] B = new int[A.length];
for(int i = 0 ; i < A.length ; i++){
B[i] = A[i] * A[i];
}
Arrays.sort(B);
return B;
}
}
class Solution:
def sortedSquares(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
return list(sorted(x * x for x in A))
//人生苦短啊
第二個:Longest Turbulent Subarray(這個題是一個簡單的dp)
A subarray A[i], A[i+1], ..., A[j]
of A
is said to be turbulent if and only if:
- For
i <= k < j
,A[k] > A[k+1]
whenk
is odd, andA[k] < A[k+1]
whenk
is even; - OR, for
i <= k < j
,A[k] > A[k+1]
whenk
is even, andA[k] < A[k+1]
whenk
is odd.
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
Return the length of a maximum size turbulent subarray of A.
Example 1:
Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])
Example 2:
Input: [4,8,12,16]
Output: 2
Example 3:
Input: [100]
Output: 1
Note:
1 <= A.length <= 40000
0 <= A[i] <= 10^9
class Solution {
public int maxTurbulenceSize(int[] A) {
if(A.length == 1){
return 1;
}
Queue<Integer> queue = new ArrayDeque<>();
for(int i = 1 ; i < A.length ; i++){
if(A[i] > A[i - 1]){
queue.add(1);
}else if(A[i] < A[i - 1]){
queue.add(-1);
}else{
queue.add(2);
}
}
int max = 0;
int[] dp = new int[A.length];
//while(!queue.isEmpty()){
int a = queue.poll();
for(int i = 0 ; i < A.length;i++){
dp[i] = 2;
}
for(int i = 1; i < A.length;i++){
//System.out.println("a:"+a);
if(queue.peek() != null && a + queue.peek() == 0){
//System.out.println("queue.peek():"+queue.peek());
dp[i] = dp[i - 1] + 1;
//System.out.println("dp[i]:"+dp[i]);
}
if(queue.peek() != null){
a = queue.poll();
}
//System.out.println("queue.size()"+queue.size());
max = Math.max(max,dp[i]);
//System.out.println("max:"+max);
//System.out.println("-------------");
}
//}
return max;
}
}
看了一下大神的程式碼:(看來有必要以後用多種語言做了emmm)
class Solution:
def maxTurbulenceSize(self, A):
"""
:type A: List[int]
:rtype: int
"""
ans=1
cur=[1,1]
for i in range(1,len(A)):
if A[i]==A[i-1]:
cur=[1,1]
elif A[i]>A[i-1]:
cur=[1,cur[0]+1]
else:
cur=[cur[1]+1,1]
ans=max(ans,max(cur))
return ans
第三題:Distribute Coins in Binary Tree(賽後補題)
Given the root
of a binary tree with N
nodes, each node
in the tree has node.val
coins, and there are N
coins total.
In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from child to parent.)
Return the number of moves required to make every node have exactly one coin.
Example 1:
Input: [3,0,0]
Output: 2
Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
Example 2:
Input: [0,3,0]
Output: 3
Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
Example 3:
Input: [1,0,2]
Output: 2
Example 4:
Input: [1,0,0,null,3]
Output: 4
Note
1<= N <= 100
0 <= node.val <= N
class Solution {
public int distributeCoins(TreeNode root) {
int[] move = new int[1];//類似於定義一個全域性變數
distribute(root, move);//move為總共移動的次數
return move[0];
}
// return the number of extra / deficit at each subtree.
// move is the number of moves required
int distribute(TreeNode node, int[] move) {
if(node == null) return 0;
node.val += distribute(node.left, move);
node.val += distribute(node.right, move);//將帶過來的值與原來的值相加
move[0] += Math.abs(node.val-1);//值與1進行相減,餘數為總共需要move的次數
return node.val - 1;//返回的是當前的價值
}
}
4.最後一個題太難了,暫時放棄emmm
相關文章
- leetcode周賽 - 406LeetCode
- 【leetcode 399 周賽】【題解】LeetCode
- LeetCode第215場周賽LeetCode
- 20241027LeetCode421周賽LeetCode
- leetcode 第 217 場周賽(vivo)LeetCode
- [leetcode 第 400 場周賽]題解LeetCode
- 前端工程師的 LeetCode 之旅 -- 周賽 182前端工程師LeetCode
- Leetcode 第136場周賽解題報告LeetCode
- Leetcode第 217 場周賽(思維量比較大)LeetCode
- LeetCode 第 196 場周賽 (題目:5452-5455,這是參加過最坑的周賽,暴力n^2居然可以過)LeetCode
- 第191場周賽
- 第 210 場周賽
- 牛客周賽48
- 牛客小白周賽9
- 牛客周賽 Round 40
- 牛客周賽 Round 38
- 牛客周賽 Round 57
- 牛客周賽 Round 56
- 牛客周賽 Round 47
- 牛客周賽Ronud 46
- 牛客周賽 Round 8
- 牛客周賽 Round 63
- 牛客周賽 Round 1
- 牛客周賽 Round 3
- 牛客周賽 Round 7
- AcWing第85場周賽
- 牛客周賽 Round 67 F
- 牛客周賽 Round 67 A~F
- 牛客周賽 Round 66 G
- 牛客周賽 Round 65(D)
- CSDN周賽第35期:贏《阿里雲天池大賽賽題解析―深度學習篇》和定製周邊阿里深度學習
- LeetCode 周賽上分之旅 #47 前字尾分解結合單調棧的貢獻問題LeetCode
- 119 路由注意點路由
- LeetCode 463. 島嶼的周長 JAVALeetCodeJava
- 牛客周賽 Round 66 題解
- LeetCode 周賽上分之旅 #45 精妙的 O(lgn) 掃描演算法與樹上 DP 問題LeetCode演算法
- 119 Pascal's Triangle II
- Firefox 119 正式釋出Firefox