Leetcode周賽119

HowieLee59發表於2019-01-20

已經累計打了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. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 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 < jA[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
  • OR, for i <= k < jA[k] > A[k+1] when k is even, and A[k] < A[k+1] when k 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. 1 <= A.length <= 40000
  2. 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. 1<= N <= 100
  2. 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

相關文章