{"id":4985,"date":"2019-03-24T08:08:48","date_gmt":"2019-03-24T15:08:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4985"},"modified":"2019-03-24T08:16:55","modified_gmt":"2019-03-24T15:16:55","slug":"leetcode-weekly-contest-129-1020-1021-1022-1023","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-weekly-contest-129-1020-1021-1022-1023\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode Weekly Contest 129 (1020, 1021, 1022, 1023)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1020.&nbsp;Partition Array Into Three Parts With Equal Sum<\/strong><\/h2>\n\n\n\n<p>Return true if there is a 2\/3*sum prefix sum after a 1\/3 prefix sum<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[---- 1\/3 ----][-----1\/3-----][-----1\/3-----]\n[------------2\/3-------------][-----1\/3-----]<\/pre>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">class Solution:\n  def canThreePartsEqualSum(self, A: List[int]) -&gt; bool:\n    s = sum(A)\n    if s % 3 != 0: return False    \n    c = 0\n    found = False\n    for a in A:\n      c += a\n      if c == s \/\/ 3 * 2 and found: return True\n      if c == s \/\/ 3: found = True\n    return False\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1021.&nbsp;Best Sightseeing Pair<\/strong><\/h2>\n\n\n\n<p>Greedy, only keep the best A[i] + i so far and pair with A[j] &#8211; j, i &lt; j<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  int maxScoreSightseeingPair(vector<int>& A) {\n    int ans = 0;\n    int left = 0;\n    for (int i = 0; i < A.size(); ++i) {\n      ans = max(ans, left + A[i] - i);\n      left = max(left, A[i] + i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1022.&nbsp;Smallest Integer Divisible by K<\/strong><\/h2>\n\n\n\n<p>Math<\/p>\n\n\n\n<p>Time complexity: O(K)<br>Space complexity: O(1)<\/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  int smallestRepunitDivByK(int K) {\n    if (K % 2 == 0 || K % 5 == 0) return -1;\n    int r = 0;\n    for (int l = 1; l <= K; ++l) {\n      r = (r * 10 + 1) % K;\n      if (r == 0) return l;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1023.&nbsp;Binary String With Substrings Representing 1 To N<\/strong><\/h2>\n\n\n\n<p>Brute Force, try all possible substrings and convert them into decimals.<\/p>\n\n\n\n<p>Time complexity: O(|S|*log(N)^2)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">class Solution:\n  def queryString(self, S: str, N: int) -&gt; bool:\n    s = set()\n    c = 0    \n    for l in range(1, 32):\n      for i in range(0, len(S) - l + 1):        \n        n = int(S[i:i+l], 2)\n        if n == 0 or n > N or n in s: continue        \n        s.add(n)\n        c += 1\n    return c == N\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1020.&nbsp;Partition Array Into Three Parts With Equal Sum Return true if there is a 2\/3*sum prefix sum after a 1\/3 prefix sum [&#8212;- 1\/3 &#8212;-][&#8212;&#8211;1\/3&#8212;&#8211;][&#8212;&#8211;1\/3&#8212;&#8211;]&#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":[456],"class_list":["post-4985","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-weekly","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4985","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=4985"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4985\/revisions"}],"predecessor-version":[{"id":4991,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4985\/revisions\/4991"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}