Leet Code 第 132 場周賽

5024. 除數博弈

題目鏈接

題解:分析Alice和Bob的操作我們可以知道:

N 1 2 3 4
先手勝負

同時我們可以發現,當N爲奇數時先手要麼取1要麼取一個N的因數x, N-x爲偶數

當N爲偶數時,此時的先手則可以取1,N-1爲奇數

所以先手拿到奇數必輸,拿到偶數必贏。

例如,N=5,則先手必輸,因爲其只能取1,則N1 = N-1 = 4,爲當前先手必勝態(即後手Bob必勝)

N = 6,則先手可以拿1,則N1 = N-1 = 5, 則此時爲當前先手必敗態(即後手Bob必敗)

...

N = 9,則先手只能拿1或者3,但是N-1 = 8, N-3 = 6皆爲偶數,爲當前先手必勝態(即後手Bob必勝),所以先手Alice必敗。

 

代碼:

class Solution {
    public boolean divisorGame(int N) {
        return N%2 == 0;
    }
}

 

5030. 節點與其祖先之間的最大差值

題目鏈接

題解:直接dfs即可。

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    public int dfs(TreeNode root, int x,int y)
    {
        if(root == null) return 0;
        x = Math.max(x,root.val);
        y = Math.min(y,root.val);
        return Math.max(x-y,Math.max(dfs(root.left,x,y),dfs(root.right,x,y)));
    }
    public int maxAncestorDiff(TreeNode root) {
        return dfs(root,root.val,root.val);
    }
}

 

5025. 最長等差數列

題目鏈接

題解:可以視作特殊的區間DP,DP方程爲 dp[i][j] = dp[k][i] + 1, k爲小於且距離i最近的前一項的下標, k = b[2*A[i]-A[j]] , b[A[i]] = i;

代碼:

class Solution {
    public int b[] = new int[10010];
    public int dp[][] = new int[2010][2010];
    public int longestArithSeqLength(int[] A) {
        int n = A.length;
        int ans = 1;
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
                dp[i][j] = 2;
        Arrays.fill(b,-1);
        
        for(int i = 0; i < n; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                int k = 2*A[i]-A[j];
                if(k >= 0 && k <= 10000 && b[k] != -1)
                dp[i][j] = dp[b[k]][i] + 1;
            }
            b[A[i]] = i;
         }
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
                ans = Math.max(ans,dp[i][j]);
        return ans;
    }
}

5031. 從先序遍歷還原二叉樹

題目鏈接

題解:線性掃描字符串S,並結合深度進行遞歸調用,有點考驗代碼功底。

代碼:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class Inte{
    public int x;
    Inte(int v) {x = v;}
}

class Solution {
        
    public TreeNode dfs(TreeNode root, String S,  Inte x, Inte y)
    {
        //System.out.println("*"+ x.x);
        int ty = y.x;
        y.x = 0;
        while(x.x < S.length() && S.charAt(x.x) == '-')  {x.x ++;y.x ++;}
        int res = 0;
        while(x.x < S.length() && S.charAt(x.x) != '-')
              {
                  res *= 10;
                  res += (S.charAt(x.x) - '0');
                  x.x++;
              }
        TreeNode node = new TreeNode(res);
        if(ty+1 == y.x)
        {
            //System.out.println("*"+ res);
            if(root.left == null) 
            {
                //System.out.println("*"+ res);
                root.left = node;
                TreeNode temp = dfs(node,S,x,y);
                if(ty+1 == y.x)
                {
                    root.right = temp;
                    return dfs(temp,S,x,y);
                }
                else return temp;
            }
            else {
                root.right = node;
                return dfs(node,S,x,y);
              }   
        }
        else return new TreeNode(res);
    }
    
    public TreeNode recoverFromPreorder(String S) {
        int x = 0,y = 0,res = 0;
         while(x < S.length() && S.charAt(x) != '-')
              {
                  res *= 10;
                  res += (S.charAt(x) - '0');
                  x++;
              }
        TreeNode root = new TreeNode(res);
        dfs(root,S,new Inte(x),new Inte(y));
        return root;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章