Leetcode周赛119

本文记录了作者参加LeetCode周赛119的体验,完成两道题目,包括排序数组的平方和最长湍流子数组问题。作者对比赛中的解题策略进行了反思,并分享了部分题目解法和后续学习计划。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已经累计打了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值