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 x and y with x <= y.  The result of this smash is:

  • If x == y, both stones are totally destroyed;
  • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-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:

  1. Input: [2,7,4,1,8,1]
  2. Output: 1
  3. Explanation:
  4. We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
  5. we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
  6. we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
  7. we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone.

Note:

  1. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 1000

Approach #1: Brute Force. [Java]

  1. class Solution {
  2. public int lastStoneWeight(int[] stones) {
  3.  
  4. Comparator c = new Comparator<Integer>() {
  5. @Override
  6. public int compare(Integer o1, Integer o2) {
  7. if((int)o1<(int)o2)
  8. return 1;
  9. else return -1;
  10. }
  11. };
  12.  
  13. List<Integer> list = new ArrayList<Integer>();
  14. for (int s : stones) list.add(s);
  15. while (list.size() >= 2) {
  16. list.sort(c);
  17. int num1 = list.get(0);
  18. int num2 = list.get(1);
  19. list.remove(0);
  20. list.remove(0);
  21. if (num1 > num2) list.add(num1 - num2);
  22. }
  23. return list.isEmpty() ? 0 : list.get(0);
  24. }
  25. }

  

1047. Remove All Adjacent Duplicates In String

Given a string S of 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:

  1. Input: "abbaca"
  2. Output: "ca"
  3. Explanation:
  4. 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. 1 <= S.length <= 20000
  2. S consists only of English lowercase letters.

Approach #1: Brute Force. [Java]

  1. class Solution {
  2. public String removeDuplicates(String S) {
  3. List<Character> list = new ArrayList<>();
  4. for (int i = 0; i < S.length(); ++i) {
  5. if (i < S.length() - 1 && S.charAt(i) == S.charAt(i+1)) {
  6. i++;
  7. continue;
  8. }
  9. list.add(S.charAt(i));
  10. }
  11. while (haveDuplicates(list)) {
  12.  
  13. }
  14.  
  15. String ret = "";
  16. for (int i = 0; i < list.size(); ++i)
  17. ret += list.get(i);
  18.  
  19. return ret;
  20. }
  21.  
  22. public boolean haveDuplicates(List<Character> list) {
  23. for (int i = 1; i < list.size(); ++i) {
  24. if (list.get(i) == list.get(i-1)) {
  25. list.remove(i);
  26. list.remove(i-1);
  27. return true;
  28. }
  29. }
  30. return false;
  31. }
  32. }

  

1048. Longest String Chain

Given a list of words, each word consists of English lowercase letters.

Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac".

word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2word_2 is a predecessor of word_3, and so on.

Return the longest possible length of a word chain with words chosen from the given list of words.

Example 1:

  1. Input: ["a","b","ba","bca","bda","bdca"]
  2. Output: 4
  3. Explanation: one of the longest word chain is "a","ba","bda","bdca".

Note:

  1. 1 <= words.length <= 1000
  2. 1 <= words[i].length <= 16
  3. words[i] only consists of English lowercase letters.

Approach #1: HashMap + DP. [Java]

  1. class Solution {
  2. public int longestStrChain(String[] words) {
  3. if (words == null || words.length == 0) return 0;
  4. int ans = 0;
  5. Map<String, Integer> map = new HashMap<>();
  6. Arrays.sort(words, new Comparator<String>() {
  7. public int compare(String str1, String str2) {
  8. return str1.length() - str2.length();
  9. }
  10. });
  11.  
  12. for (String word : words) {
  13. if (map.containsKey(word)) continue;
  14. map.put(word, 1);
  15. for (int i = 0; i < word.length(); ++i) {
  16. StringBuilder sb = new StringBuilder(word);
  17. sb.deleteCharAt(i);
  18. String next = sb.toString();
  19. if (map.containsKey(next) && map.get(next) + 1 > map.get(word)) {
  20. map.put(word, map.get(next) + 1);
  21. }
  22. }
  23. if (map.get(word) > ans) ans = map.get(word);
  24. }
  25.  
  26. return ans;
  27. }
  28. }

  

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 x and y with x <= y.  The result of this smash is:

  • If x == y, both stones are totally destroyed;
  • If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-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:

  1. Input: [2,7,4,1,8,1]
  2. Output: 1
  3. Explanation:
  4. We can combine 2 and 4 to get 2 so the array converts to [2,7,1,8,1] then,
  5. we can combine 7 and 8 to get 1 so the array converts to [2,1,1,1] then,
  6. we can combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
  7. we can combine 1 and 1 to get 0 so the array converts to [1] then that's the optimal value.

Note:

  1. 1 <= stones.length <= 30
  2. 1 <= stones[i] <= 100

Approach #1: DP. [Java]

  1. class Solution {
  2. public int lastStoneWeightII(int[] stones) {
  3. int sum = 0;
  4. int n = stones.length;
  5. for (int stone : stones)
  6. sum += stone;
  7. int total_sum = sum;
  8. sum /= 2;
  9.  
  10. boolean[][] dp = new boolean[sum+1][n+1];
  11. for (int i = 0; i <= n; ++i)
  12. dp[0][i] = true;
  13.  
  14. int max = Integer.MIN_VALUE;
  15. for (int i = 1; i <= sum; ++i) {
  16. for (int j = 1; j <= n; ++j) {
  17. if (dp[i][j-1] == true || (i >= stones[j-1] && dp[i-stones[j-1]][j-1])) {
  18. dp[i][j] = true;
  19. max = Math.max(max, i);
  20. }
  21. }
  22. }
  23.  
  24. return total_sum - max * 2;
  25. }
  26. }

  

Reference:

https://www.geeksforgeeks.org/partition-a-set-into-two-subsets-such-that-the-difference-of-subset-sums-is-minimum/

Weekly Contest 137的更多相关文章

  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. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  6. LeetCode之Weekly Contest 91

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

  7. LeetCode Weekly Contest

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

  8. LeetCode Weekly Contest 47

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

  9. 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 ...

随机推荐

  1. Java基本概念:方法

    一.简介 描述: Java中方法是语句的集合,它们在一起执行一个功能. 方法是解决一类问题的步骤的有序组合,它在类中定义,属于类的成员,包含于类或对象中. 方法在程序中被创建后,在其他使用了该方法的地 ...

  2. bootstrap日期范围选择插件daterangepicker详细使用方法

    插件官方网站地址 bootstrap-daterangepicker是个很方便的插件,但是对我这种菜鸟来说,文档不够详细,摆弄了好久才整好.记录下来供以后参考,也希望能帮到有需要的朋友. 目前版本是2 ...

  3. 后端程序员之路 39、一个Protocol Buffer实例

    实际工作的Protocol Buffer使用经验 # 写proto文件- 协议版本 项目用的是protobuf2,所以要指定 syntax = "proto2";- 包名 pack ...

  4. C++树——遍历二叉树

    在讲遍历之前,我们要先创建一个树: #include <iostream> using namespace std; typedef struct node; typedef node * ...

  5. 性能追击:万字长文30+图揭秘8大主流服务器程序线程模型 | Node.js,Apache,Nginx,Netty,Redis,Tomcat,MySQL,Zuul

    本文为<高性能网络编程游记>的第六篇"性能追击:万字长文30+图揭秘8大主流服务器程序线程模型". 最近拍的照片比较少,不知道配什么图好,于是自己画了一个,凑合着用,让 ...

  6. cocos 向左滚动公告

      properties:{ lblNotice:[cc.Node], speed:1, curtext:null }, start (){ this.getNotice(); }, getNotic ...

  7. 代码审查:从 ArrayList 说线程安全

    本文从代码审查过程中发现的一个 ArrayList 相关的「线程安全」问题出发,来剖析和理解线程安全. 案例分析 前两天在代码 Review 的过程中,看到有小伙伴用了类似以下的写法: List< ...

  8. 锁与同步器的基础--AQS

    什么是AQS AQS全名AbstractQueueSynchronizer,可以翻译为抽象队列同步器 Abstract--说明该类需要被继承,提供实现的框架和一些必要的功能 事实上,AQS也的确提供了 ...

  9. github个人主页 阿里云域名的绑定

    域名解析 我在阿里云上买了一个新域名:gaolu.name,我已经在GitHub Pages上建立了自己的博客:http://gaolu1215.github.io.现在我希望将gaolu.name映 ...

  10. 时间&空间(complexity)

    时间&空间复杂度 时间复杂度: 通俗来说就是随着数据量的增加,程序运行的时间花费量是怎么变化的,时间复杂度常用大o表示.举个例子,猜数字,猜10个,100个.1000个,猜数的数据量是在增加的 ...