Weekly Contest 132
1025. Divisor Game
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number
Non the chalkboard. On each player's turn, that player makes a move consisting of:
- Choosing any
xwith0 < x < NandN % x == 0.- Replacing the number
Non the chalkboard withN - x.Also, if a player cannot make a move, they lose the game.
Return
Trueif 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
rootof a binary tree, find the maximum valueVfor which there exists different nodesAandBwhereV = |A.val - B.val|andAis 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
2and5000.- Each node will have value between
0and100000.
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
Aof integers, return the length of the longest arithmetic subsequence inA.Recall that a subsequence of
Ais a listA[i_1], A[i_2], ..., A[i_k]with0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequenceBis 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 <= 20000 <= 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
rootof a binary tree.At each node in this traversal, we output
Ddashes (whereDis 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
Sof 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
1and1000.- Each node will have a value between
1and10^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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- Spring框架的AOP技术之通知类型
1. 通知类型 * @Before -- 前置通知 * @AfterReturing -- 后置通知 * @Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行) * @After - ...
- css设置超出部分文档隐藏(在table标签中不好使解决方案在下)
css设置: .text-over{overflow: hidden;white-space: nowrap;text-overflow: ellipsis;cursor: pointer} div设 ...
- OCI 编程
一.环境的配置 1.系统环境:要想使用OCI编程需要安装Oracle的客户端,而这个普通的客户端比较大,方便起见,可以安装即时客户端(Instantclient)作为Oracle的访问客户端. 具体 ...
- 在jmeter的beanshell中用java获取系统当前时间
import java.util.*; int y,m,d,h,mi,s; Calendar cal=Calendar.getInstance(); y=cal.get(Calendar.YEAR); ...
- 想到的regular方法果然已经被sklearn实现了就是L1和L2组合rugular
- const stirng* 类型的指针cp,当cout<<*cp<<endl:会提示没有与之匹配的“<<”运算符
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> usin ...
- android 蓝牙通讯编程 备忘
1.启动App后: 判断->蓝牙是否打开(所有功能必须在打牙打开的情况下才能用) 已打开: 启动代码中的蓝牙通讯Service 未打开: 发布 打开蓝牙意图(系统),根据Activity返回进场 ...
- String、StringBuffer与StringBuilder之间区别(转)
最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...
- oracle11g 导出空表
--对已存在的表 执行如下 ,要经过统计分析后 num_rows=0 才准确select 'alter table '||table_name||' allocate extent;' from us ...
- Tomcat 环境变量配置
1.变量和常量 i 和 0 2.环境变量 cmd >set 查看所有环境变量 %PATH% 系统指定可执行文件的搜索路径,可以是 .exe .bat String path=“C:\WINDOW ...



