Weekly Contest 132
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
with0 < x < N
andN % x == 0
.- Replacing the number
N
on the chalkboard withN - 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 <= 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 valueV
for which there exists different nodesA
andB
whereV = |A.val - B.val|
andA
is an ancestor ofB
.(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:
- The number of nodes in the tree is between
2
and5000
.- Each node will have value between
0
and100000
.
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 inA
.Recall that a subsequence of
A
is a listA[i_1], A[i_2], ..., A[i_k]
with0 <= i_1 < i_2 < ... < i_k <= A.length - 1
, and that a sequenceB
is arithmetic ifB[i+1] - B[i]
are all the same value (for0 <= 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:
2 <= A.length <= 2000
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 (whereD
is the depth of this node), then we output the value of this node. (If the depth of a node isD
, the depth of its immediate child isD+1
. The depth of the root node is0
.)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 itsroot
.
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
and1000
.
- Each node will have a value between
1
and10^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的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 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 ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- C#开发中常用加密解密方法解析
一.MD5加密算法 我想这是大家都常听过的算法,可能也用的比较多.那么什么是MD5算法呢?MD5全称是message-digest algorithm 5,简单的说就是单向的加密,即是说无法根据密文推 ...
- 利用委托机制处理.NET中的异常
WinForm代码 private void button1_Click(object sender, EventArgs e) { try { Convert.ToInt32("abcd& ...
- Spring 学习记录3 ConversionService
ConversionService与Environment的关系 通过之前的学习(Spring 学习记录2 Environment),我已经Environment主要是负责解析properties和p ...
- Android无线调试_adbWireless
NC的ADB驱动是个很让人头疼的问题,纵使老玩家有时候也是反复装装不上,有时候就算装上了,换一个ROM就又不行了,真是让人扣心扣肺,欲哭无泪,欲罢不能啊...现在好了,有了adbWireless不但可 ...
- Alien::BatToExeConverter 模块应用
## DOS 下批量任务转换成exe二进制可执行文件 Convert a DOS Batch Script to an Executable Alien::BatToExeConverter::ba ...
- 2018秋季C语言基础课第1次作业
1.翻阅邹欣老师博客关于师生关系博客,并回答下列问题: 1)大学和高中最大的不同是没有人天天看着你,请看大学理想的师生关系是?有何感想? 答:是 Coach / Trainee (健身教练 / 健身 ...
- Linux设置开机启动项
第一种方式:ln -s 建立启动软连接 在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录 Tips:/etc ...
- Redis总结和提取常用的和重要的命令
一:Redis的结构和其数据类型(注redis默认端口号是6379) 1)Redis可以部署多套(多个进程不同端口或直接部署在不同主机),每个Redis都可以有多个db,通过select来选择,默认的 ...
- public class 和class 的区别
Java在编写类的时候可以使用两种方式定义类: public class定义类: class定义类: 如果一个类声明的时候使用了public class进行了声明,则类名称必须与 ...
- timescale
`timescale 1ns/100ps 表示时延单位为1ns, 时延精度为100ps.`timescale 编译器指令在模块说明外部出现, 并且影响后面所有的时延值.