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第215場周賽LeetCode
- leetcode 第 217 場周賽(vivo)LeetCode
- Leetcode 第136場周賽解題報告LeetCode
- 前端工程師的 LeetCode 之旅 -- 周賽 182前端工程師LeetCode
- Leetcode第 217 場周賽(思維量比較大)LeetCode
- LeetCode 第 196 場周賽 (題目:5452-5455,這是參加過最坑的周賽,暴力n^2居然可以過)LeetCode
- 十六週周賽總結
- 牛客周賽 Round 38
- 牛客周賽 Round 40
- 每天一道LeetCode--119.Pascal's Triangle II(楊輝三角)LeetCode
- 119 C++中的引用&C++
- Firefox 119 正式釋出Firefox
- LeetCode 周賽上分之旅 #47 前字尾分解結合單調棧的貢獻問題LeetCode
- LeetCode 周賽上分之旅 #45 精妙的 O(lgn) 掃描演算法與樹上 DP 問題LeetCode演算法
- DBCA建庫報錯ORA-119
- 牛客周賽 Round 36 (小白練習記)
- Mozilla Firefox 119 現已可供下載Firefox
- toMyCar:119 Uncaught ReferenceError: toJS is not defined at HTMLAnchorElement.onclickErrorJSHTML
- Oracle 11G OCP 1Z0-052 119Oracle
- Oracle 11G OCP 1Z0-053 119Oracle
- The COMPATIBLE Initialization Parameter and Irreversible Compatibility (119)
- NYNU_ACM 實驗室招新 第二週周賽ACM
- PostgreSQL DBA(119) - pgAdmin(LIMIT:Index Scan vs Bitmap Index Scan)SQLMITIndex
- APP開發實戰119-APP程式碼優化APP優化
- ORA-07445: exception encountered: core dump [kglpnp()+119] [SIGSEGV]ExceptionGse
- 老司機 iOS 週報 #119 | 2020-07.13iOS
- RS:與AI相關工作崗位需求比例上升了119%AI
- PostgreSQL 原始碼解讀(119)- MVCC#4(啟動事務)SQL原始碼MVCC#
- 2.12 それは命の證 ——ARC119~121
- NYNU_ACM 實驗室 招新第一週 周賽題解ACM
- 牛客周賽 Round 40 (小白醬的被虐之旅嗚嗚嗚)
- SuperData:預計2020年全球VR遊戲收入增長119%VR遊戲
- ORA-07445: Failing Function指的是 [kglpnp()+119] 中的kglpnpAIFunction
- CSDN周賽第48期:贏《MongoDB核心原理與實踐》和定製周邊MongoDB
- CSDN周賽第34期:贏《MongoDB核心原理與實踐》和定製周邊MongoDB
- CF 119D String Transformation(KMP,雜湊,列舉,各種優化)ORMKMP優化
- O2:英國使用者每天在智慧手機上花119分鐘
- 牛客IOI周賽20-普及組-D 夾縫中求和(線性優化)優化