1025. Divisor Game

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number N on the chalkboard.  On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < N and N % x == 0.
  • Replacing the number N on the chalkboard with N - x.

Also, if a player cannot make a move, they lose the game.

Return True if and only if Alice wins the game, assuming both players play optimally.

Example 1:

Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.

Example 2:

Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Note:

  1. 1 <= N <= 1000

Approach #1: Math. [Java]

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

  

1026. Maximum Difference Between Node and Ancestor

Given the root of a binary tree, find the maximum value V for which there exists different nodes A and B where V = |A.val - B.val| and A is an ancestor of B.

(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)

Example 1:

Input: [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation:
We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.

Note:

  1. The number of nodes in the tree is between 2 and 5000.
  2. Each node will have value between 0 and 100000.

Approach #1: [Java]

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

  

1027. Longest Arithmetic Sequence

Given an array A of integers, return the length of the longest arithmetic subsequence in A.

Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).

Example 1:

Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:

Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].

Example 3:

Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].

Note:

  1. 2 <= A.length <= 2000
  2. 0 <= A[i] <= 10000

Approach #1: [Java]

class Solution {
public int longestArithSeqLength(int[] A) {
if (A.length <= 1) return A.length;
int longest = 0;
Map<Integer, Integer>[] dp = new HashMap[A.length];
for (int i = 0; i < A.length; ++i)
dp[i] = new HashMap<Integer, Integer>();
for (int i = 1; i < A.length; ++i) {
for (int j = 0; j < i; ++j) {
int d = A[i] - A[j];
int len = 2;
if (dp[j].containsKey(d)) {
len = dp[j].get(d) + 1;
}
int curr = dp[i].getOrDefault(d, 0);
dp[i].put(d, Math.max(len, curr));
longest = Math.max(longest, dp[i].get(d));
}
}
return longest;
}
}

Analysis:

We iteratively build the map for a new index i, by considering all elements to the left one-by-one. For each pair of indeces (i, j) and difference d = A[i] - A[j] considered, we check if there was an existing chain at the index j with difference d always.

If yes, we can then extend the existing chain length by 1.

Else, if not, then we can start a new chain of length 2 with this new difference d and (A[j], A[i]) as its elements.

At the end, we can then return the maximum chain length that we have seen so far. 

1028. Recover a Tree From Preorder Traversal

We run a preorder depth first search on the root of a binary tree.

At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  (If the depth of a node is D, the depth of its immediate child is D+1.  The depth of the root node is 0.)

If a node has only one child, that child is guaranteed to be the left child.

Given the output S of this traversal, recover the tree and return its root.

Example 1:

Input: "1-2--3--4-5--6--7"
Output: [1,2,5,3,4,6,7]

Example 2:

Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]

Example 3:

Input: "1-401--349---90--88"
Output: [1,401,null,349,88,90]

Note:

  • The number of nodes in the original tree is between 1 and 1000. 
  • Each node will have a value between 1 and 10^9.

Approach #1: [Java]

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int index = 0;
public TreeNode recoverFromPreorder(String S) {
return helper(S, 0);
} public TreeNode helper(String s, int depth) {
int numDash = 0;
while (index + numDash < s.length() && s.charAt(index+numDash) == '-') {
numDash++;
}
if (numDash != depth) return null;
int next = index + numDash;
while (next < s.length() && s.charAt(next) != '-') next++;
int val = Integer.parseInt(s.substring(index + numDash, next));
index = next;
TreeNode root = new TreeNode(val);
root.left = helper(s, depth + 1);
root.right = helper(s, depth + 1);
return root;
}
}

  

Weekly Contest 132的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  8. 75th LeetCode Weekly Contest Champagne Tower

    We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...

  9. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

随机推荐

  1. 【SQL模板】二.创建表视图模板TSQL

    ---Name: 创建表视图模板.sql ---Purpose: 用于创建 数据库中 新的数据表/视图 ---Author: xx ---Time: 2015-12-18 10:26:06 ---Re ...

  2. PAT 1055 集体照 (25)(STL-list+代码)

    1055 集体照 (25)(25 分)提问 拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排 ...

  3. Mybatis之拦截器原理(jdk动态代理优化版本)

    在介绍Mybatis拦截器代码之前,我们先研究下jdk自带的动态代理及优化 其实动态代理也是一种设计模式...优于静态代理,同时动态代理我知道的有两种,一种是面向接口的jdk的代理,第二种是基于第三方 ...

  4. QQ使用技巧

    1.改变真实地理位置 大家知道,现在很多QQ都是显示IP和地理位置的版本,这样一来,自己的位置就暴露了.其实想隐藏自己的位置也简单,只要用代理服务器就是了.不要把它看成很复杂的东西,建议去下载&quo ...

  5. Basic4android v3.80 beta 发布

    增加了条件编译,共享模块,部分支持jar 文件直接访问.还有其他一些更新. I'm happy to release B4A v3.80 BETA. This version includes sev ...

  6. hdu-1069(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:一群猴子,给出n块砖的长x宽y高z,用这些砖拼起的高度最高是多少, 要求底下的砖的长宽都要 ...

  7. 38 Cell-phone Emissions can change Brain Activity 手机辐射有可能改变大脑活动

    Cell-phone Emissions can change Brain Activity 手机辐射有可能改变大脑活动 So many people use the cell phone so fr ...

  8. IntelliJ IDEA 2017版 编译器使用学习笔记(二) (图文详尽版);IDE快捷键使用

    补充介绍IntellJ 介绍主菜单功能及相关用途: File -------------> 对文件进行操作 Edit ------------> 对文本进行操作 View -------- ...

  9. UVa 1639 Candy (数学期望+组合数学+高精度存储)

    题意:有两个盒子各有n个糖,每次随机选一个(概率分别为p,1-p),然后吃掉,直到有一次,你打开盒子发现,没糖了! 输入n,p,求另一个盒子里糖的个数的数学期望. 析:先不说这个题多坑,首先要用lon ...

  10. python 求最大子序列

     动态规划的本质,是对问题状态的定义和状态转移方程的定义.dynamic programming is a method for solving a complex problem by breaki ...