Weekly Contest 137
1046. Last Stone Weight
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose the two heaviest rocks and smash them together. Suppose the stones have weights
xandywithx <= y. The result of this smash is:
- If
x == y, both stones are totally destroyed;- If
x != y, the stone of weightxis totally destroyed, and the stone of weightyhas new weighty-x.At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.
Note:
1 <= stones.length <= 301 <= stones[i] <= 1000
Approach #1: Brute Force. [Java]
class Solution {
public int lastStoneWeight(int[] stones) {
Comparator c = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if((int)o1<(int)o2)
return 1;
else return -1;
}
};
List<Integer> list = new ArrayList<Integer>();
for (int s : stones) list.add(s);
while (list.size() >= 2) {
list.sort(c);
int num1 = list.get(0);
int num2 = list.get(1);
list.remove(0);
list.remove(0);
if (num1 > num2) list.add(num1 - num2);
}
return list.isEmpty() ? 0 : list.get(0);
}
}
1047. Remove All Adjacent Duplicates In String
Given a string
Sof lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.We repeatedly make duplicate removals on S until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
Example 1:
Input: "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Note:
1 <= S.length <= 20000Sconsists only of English lowercase letters.
Approach #1: Brute Force. [Java]
class Solution {
public String removeDuplicates(String S) {
List<Character> list = new ArrayList<>();
for (int i = 0; i < S.length(); ++i) {
if (i < S.length() - 1 && S.charAt(i) == S.charAt(i+1)) {
i++;
continue;
}
list.add(S.charAt(i));
}
while (haveDuplicates(list)) {
}
String ret = "";
for (int i = 0; i < list.size(); ++i)
ret += list.get(i);
return ret;
}
public boolean haveDuplicates(List<Character> list) {
for (int i = 1; i < list.size(); ++i) {
if (list.get(i) == list.get(i-1)) {
list.remove(i);
list.remove(i-1);
return true;
}
}
return false;
}
}
1048. Longest String Chain
Given a list of words, each word consists of English lowercase letters.
Let's say
word1is a predecessor ofword2if and only if we can add exactly one letter anywhere inword1to make it equal toword2. For example,"abc"is a predecessor of"abac".A word chain is a sequence of words
[word_1, word_2, ..., word_k]withk >= 1, whereword_1is a predecessor ofword_2,word_2is a predecessor ofword_3, and so on.Return the longest possible length of a word chain with words chosen from the given list of
words.
Example 1:
Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".
Note:
1 <= words.length <= 10001 <= words[i].length <= 16words[i]only consists of English lowercase letters.
Approach #1: HashMap + DP. [Java]
class Solution {
public int longestStrChain(String[] words) {
if (words == null || words.length == 0) return 0;
int ans = 0;
Map<String, Integer> map = new HashMap<>();
Arrays.sort(words, new Comparator<String>() {
public int compare(String str1, String str2) {
return str1.length() - str2.length();
}
});
for (String word : words) {
if (map.containsKey(word)) continue;
map.put(word, 1);
for (int i = 0; i < word.length(); ++i) {
StringBuilder sb = new StringBuilder(word);
sb.deleteCharAt(i);
String next = sb.toString();
if (map.containsKey(next) && map.get(next) + 1 > map.get(word)) {
map.put(word, map.get(next) + 1);
}
}
if (map.get(word) > ans) ans = map.get(word);
}
return ans;
}
}
1049. Last Stone Weight II
We have a collection of rocks, each rock has a positive integer weight.
Each turn, we choose any two rocks and smash them together. Suppose the stones have weights
xandywithx <= y. The result of this smash is:
- If
x == y, both stones are totally destroyed;- If
x != y, the stone of weightxis totally destroyed, and the stone of weightyhas new weighty-x.At the end, there is at most 1 stone left. Return the smallest possible weight of this stone (the weight is 0 if there are no stones left.)
Example 1:
Input: [2,7,4,1,8,1]
Output: 1
Explanation:
We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then,
we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then,
we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.
Note:
1 <= stones.length <= 301 <= stones[i] <= 100
Approach #1: DP. [Java]
class Solution {
public int lastStoneWeightII(int[] stones) {
int sum = 0;
int n = stones.length;
for (int stone : stones)
sum += stone;
int total_sum = sum;
sum /= 2;
boolean[][] dp = new boolean[sum+1][n+1];
for (int i = 0; i <= n; ++i)
dp[0][i] = true;
int max = Integer.MIN_VALUE;
for (int i = 1; i <= sum; ++i) {
for (int j = 1; j <= n; ++j) {
if (dp[i][j-1] == true || (i >= stones[j-1] && dp[i-stones[j-1]][j-1])) {
dp[i][j] = true;
max = Math.max(max, i);
}
}
}
return total_sum - max * 2;
}
}
Reference:
Weekly Contest 137的更多相关文章
- 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 ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- 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 ...
随机推荐
- 双重检验锁模式为什么要使用volatile?
并发编程情况下有三个要点:操作的原子性.可见性.有序性. volatile保证了可见性和有序性,但是并不能保证原子性. 首先看一下DCL(双重检验锁)的实现: public class Singlet ...
- Go的切片
目录 切片 一.切片的创建 1.先创建数组,再引用 二.切片的修改 三.切片的长度和容量 四.使用make创建切片 五.切片的修改和追加 1.修改 2.追加:append 六.切片的函数传值 七.多维 ...
- 项目管理之Git
@[TOC]( Git命令:分支与合并)Git一款很好的项目版本管理工具,更是一款优秀的分布式项目管理工具.今天主要给大家介绍Git 强大的分支和合并功能,分支和合并可以说在实际的工作当中用到的是最多 ...
- 剑指 Offer 14- I. 剪绳子 + 动态规划 + 数论
剑指 Offer 14- I. 剪绳子 题目链接 还是343. 整数拆分的官方题解写的更清楚 本题说的将绳子剪成m段,m是大于1的任意一个正整数,也就是必须剪这个绳子,至于剪成几段,每一段多长,才能使 ...
- Docker搭建HAproxy+tomcat 实现高可用
构建业务镜像1创建tomcat-app1和tomcat-app2两个目录,代表不同的两个基于tomcat的业务.准备tomcat的配置文件[root@localhost ~]#mkdir -p /da ...
- CVE-2016-10033 WordPress <= 4.6 命令执行漏洞
漏洞参考 https://www.jianshu.com/p/85ac4af9f947 漏洞信息 这个锅还是要PHPMailer背(CVE-2016-10033,WordPress 使用 PHPMai ...
- Shtml、html、xhtml、htm以及SSI的了解与认识(转载)
Shtml.html.xhtml.htm以及SSI的了解与认识(转载) 一.htm.html.shtml网页区别(博客园) 文章链接:https://www.cnblogs.com/Renyi-Fan ...
- python基础学习之列表的功能方法
列表:list 格式 li = [1,2,3,4,5,6] 列表内部随意嵌套其他格式:字符串.列表.数字.元组.字典. 列表内部有序,且内容可更改 a = [1,2,3,4] a[0] = 5 ...
- Codeforces Round #574 (Div. 2) D2. Submarine in the Rybinsk Sea (hard edition) 【计算贡献】
一.题目 D2. Submarine in the Rybinsk Sea (hard edition) 二.分析 相比于简单版本,它的复杂地方在于对于不同长度,可能对每个点的贡献可能是有差异的. 但 ...
- 记一次scrapy-redis爬取小说网的分布式搭建过程
scrapy-redis简介 scrapy-redis是scrapy框架基于redis数据库的组件,用于scrapy项目的分布式开发和部署. 有如下特征: 分布式爬取 可以启动多个spider工程,相 ...