{"id":5206,"date":"2019-05-18T21:00:15","date_gmt":"2019-05-19T04:00:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5206"},"modified":"2019-05-18T21:06:17","modified_gmt":"2019-05-19T04:06:17","slug":"leetcode-weekly-contest-137","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-weekly-contest-137\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Weekly Contest 137"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1046.&nbsp;Last Stone Weight<\/strong><\/h2>\n\n\n\n<p>Solution: Simulation (priority_queue)<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<br><br><\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int lastStoneWeight(vector<int>& stones) {\n    priority_queue<int> q;\n    for (int s : stones)\n      q.push(s);\n    while (q.size() > 1) {\n      int x = q.top(); q.pop();\n      int y = q.top(); q.pop();\n      if (x == y) continue;\n      q.push(abs(x - y));\n    }\n    return q.empty() ? 0 : q.top();\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1047. Remove All Adjacent Duplicates In String<\/strong><\/h2>\n\n\n\n<p>Solution: Stack \/ Deque<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string removeDuplicates(string S) {\n    deque<char> s;\n    for (char c : S) {\n      if (!s.empty() && s.back() == c) s.pop_back();\n      else s.push_back(c);\n    }    \n    return {begin(s), end(s)};\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1048.&nbsp;Longest String Chain<\/strong><\/h2>\n\n\n\n<p>Solution: DP<\/p>\n\n\n\n<p>dp[i] := max length of chain of (A[0] ~ A[i-1])<\/p>\n\n\n\n<p>dp[i] = max{dp[j] + 1} if A[j] is prederrsor of A[i], 1 &lt;= j &lt;i<\/p>\n\n\n\n<p>Time complexity: O(n^2*l)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int longestStrChain(vector<string>& words) {\n    int n = words.size();\n    sort(begin(words), end(words), [](const string& a, const string& b) { \n      return a.length() < b.length();\n    });\n    vector<int> dp(n, 1);    \n    for (int i = 0; i < n; ++i)\n      for (int j = 0; j < i; ++j) \n        if (valid(words[j], words[i]))\n          dp[i] = dp[j] + 1;\n    return *max_element(begin(dp), end(dp));\n  }\nprivate:\n  bool valid(const string&#038; a, const string&#038; b) {\n    if (a.length() + 1 != b.length()) return false;\n    int count = 0;\n    for (int i = 0, j = 0; i < a.length() &#038;&#038; j < b.length();) {\n      if (a[i] == b[j]) {\n        ++i; ++j;\n      } else { \n        ++count; ++j; \n      }\n    }\n    return count <= 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1049.&nbsp;Last Stone Weight II<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/leetcode.com\/contest\/weekly-contest-137\/\"><\/a>Solution: DP \/ target sum<\/p>\n\n\n\n<p>Time complexity: O(n * S) = O(n * 100)<\/p>\n\n\n\n<p>Space complexity: O(S) = O(100)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int lastStoneWeightII(vector<int>& stones) {\n    const int n = stones.size();\n    const int max_sum = accumulate(begin(stones), end(stones), 0);    \n    unordered_set<int> sums;\n    sums.insert(stones[0]);\n    sums.insert(-stones[0]);\n    for (int i = 1; i < n; ++i) {\n      unordered_set<int> tmp;\n      for (int s : sums) {\n        tmp.insert(s + stones[i]);\n        tmp.insert(s - stones[i]);\n      }\n      swap(tmp, sums);\n    }\n    int ans = INT_MAX;\n    for (int s : sums)           \n      ans = min(ans, abs(s));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1046.&nbsp;Last Stone Weight Solution: Simulation (priority_queue) Time complexity: O(nlogn)Space complexity: O(n) \/\/ Author: Huahua class Solution { public: int lastStoneWeight(vector&#038; stones) { priority_queue q; for&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[18,26,439],"class_list":["post-5206","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-dp","tag-dynamic-programming","tag-weekly-contest","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5206"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5206\/revisions"}],"predecessor-version":[{"id":5209,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5206\/revisions\/5209"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}