<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>weekly Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/weekly/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/weekly/</link>
	<description></description>
	<lastBuildDate>Tue, 23 Apr 2019 23:14:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>weekly Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/weekly/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode Weekly Contest 133</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-133/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-133/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 21 Apr 2019 04:39:26 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[contest]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[weekly]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5092</guid>

					<description><![CDATA[<p>LeetCode 1029 Two City Scheduling Solution1: DP dp[i][j] := min cost to put j people into city A for the first i peopledp[0][0] = 0dp[i][0]&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-133/">花花酱 LeetCode Weekly Contest 133</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/3A98vh5zsqw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/weekly-133.png" alt="" class="wp-image-5098" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/weekly-133.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/weekly-133-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/weekly-133-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<h2><strong>LeetCode 1029  Two City Scheduling</strong></h2>



<p>Solution1: DP</p>



<p>dp[i][j] := min cost to put j people into city A for the first i people<br>dp[0][0] = 0<br>dp[i][0] = dp[i -1][0] + cost_b<br>dp[i][j] = min(dp[i &#8211; 1][j] + cost_b, dp[i &#8211; 1][j &#8211; 1] + cost_a)<br>ans := dp[n][n/2]</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(n^2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 8 ms
class Solution {
public:
  int twoCitySchedCost(vector&lt;vector&lt;int&gt;&gt;&amp; costs) {
    int n = costs.size();
    vector&lt;vector&lt;int&gt;&gt; dp(n + 1, vector&lt;int&gt;(n + 1, INT_MAX / 2));
    dp[0][0] = 0;
    for (int i = 1; i &lt;= n; ++i) {      
      dp[i][0] = dp[i - 1][0] + costs[i - 1][1];
      for (int j = 1; j &lt;= i; ++j)
        dp[i][j] = min(dp[i - 1][j - 1] + costs[i - 1][0], 
                       dp[i - 1][j] + costs[i - 1][1]);
    }
    return dp[n][n / 2];
  }
};</pre>
</div></div>



<p>Solution 2: Greedy</p>



<p>Sort by cost_a &#8211; cost_b</p>



<p>Choose the first n/2 people for A, rest for B</p>



<p>Time complexity: O(nlogn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 8 ms
class Solution {
public:
  int twoCitySchedCost(vector&lt;vector&lt;int&gt;&gt;&amp; costs) {
    int n = costs.size();
    sort(begin(costs), end(costs), [](const auto&amp; c1, const auto&amp; c2){
      return c1[0] - c1[1] &lt; c2[0] - c2[1];
    });
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      ans += i &lt; n/2 ? costs[i][0] : costs[i][1];    
    return ans;
  }
};</pre>
</div></div>



<h2><strong> 1030.&nbsp;Matrix Cells in Distance Order</strong></h2>



<p>Solution: Sorting</p>



<p>Time complexity: O(RC*log(RC))<br>Space complexity: O(RC)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; allCellsDistOrder(int R, int C, int r0, int c0) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (int i = 0; i &lt; R; ++i)
      for (int j = 0; j &lt; C; ++j)
        ans.push_back({i, j});
    std::sort(begin(ans), end(ans), [r0, c0](const vector&lt;int&gt;&amp; a, const vector&lt;int&gt;&amp; b){
      return (abs(a[0] - r0) + abs(a[1] - c0)) &lt; (abs(b[0] - r0) + abs(b[1] - c0));
    });
    return ans;
  }
};</pre>
</div></div>



<h2><strong>1031. Maximum Sum of Two Non-Overlapping Subarrays</strong></h2>



<p>Solution: Prefix sum</p>



<p>Time complexity: O(n^2)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxSumTwoNoOverlap(vector&lt;int&gt;&amp; A, int L, int M) {
    const int n = A.size();
    vector&lt;int&gt; s(n + 1, 0);
    for (int i = 0; i &lt; n; ++i)
      s[i + 1] = s[i] + A[i];
    int ans = 0;
    for (int i = 0; i &lt;= n - L; ++i) {
      int ls = s[i + L] - s[i];
      int ms = max(maxSum(s, 0, i - M - 1, M), 
                   maxSum(s, i + L, n - M, M));
      ans = max(ans, ls + ms);      
    }
    return ans;
  }
private:
  int maxSum(const vector&lt;int&gt;&amp; s, int start, int end, int l) {
    int ans = INT_MIN;    
    for (int i = start; i &lt;= end; ++i)
      ans = max(ans, s[i + l] - s[i]);
    return ans;
  }
};</pre>
</div></div>



<h2><strong> 1032. Stream of Characters</strong></h2>



<p>Solution: Trie</p>



<p>Time complexity: <br></p>



<ul><li>build O(sum(len(w))</li><li>query O(max(len(w)) </li></ul>



<p>Space complexity: O(sum(len(w))</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 320 ms, 95 MB
class TrieNode {
public:        
  ~TrieNode() {
    for (auto node : next_)
      delete node;
  }

  void reverse_build(const string&amp; s, int i) {
    if (i == -1) {
      is_word_ = true;
      return;
    }
    const int idx = s[i] - 'a';
    if (!next_[idx]) next_[idx] = new TrieNode();
    next_[idx]-&gt;reverse_build(s, i - 1);
  }
  
  bool reverse_search(const string&amp; s, int i) {
    if (i == -1 || is_word_) return is_word_;
    const int idx = s[i] - 'a';
    if (!next_[idx]) return false;
    return next_[idx]-&gt;reverse_search(s, i - 1);
  }

private:
  bool is_word_ = false;
  TrieNode* next_[26] = {0};
};

class StreamChecker {
public:
  StreamChecker(vector&lt;string&gt;&amp; words) {
    root_ = std::make_unique&lt;TrieNode&gt;();
    for (const string&amp; w : words)
      root_-&gt;reverse_build(w, w.length() - 1);
  }

  bool query(char letter) {
    s_ += letter;
    return root_-&gt;reverse_search(s_, s_.length() - 1);    
  }
  
private:
  string s_;
  unique_ptr&lt;TrieNode&gt; root_;
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 133 ms
class StreamChecker {
  class TrieNode {
    public TrieNode() {
      isWord = false;
      next = new TrieNode[26];
    }
    public boolean isWord;
    public TrieNode next[];    
  }
  
  private TrieNode root;
  private StringBuilder sb;

  public StreamChecker(String[] words) {
    root = new TrieNode();
    sb = new StringBuilder();
    for (String word : words) {
      TrieNode node = root;
      char[] w = word.toCharArray();
      for (int i = w.length - 1; i &gt;= 0; --i) {        
        int idx = w[i] - 'a';
        if (node.next[idx] == null) node.next[idx] = new TrieNode();
        node = node.next[idx];
      }
      node.isWord = true;
    }
  }

  public boolean query(char letter) {
    sb.append(letter);
    TrieNode node = root;
    for (int i = sb.length() - 1; i &gt;= 0; --i) {      
      int idx = sb.charAt(i) - 'a';
      if (node.next[idx] == null) return false;
      if (node.next[idx].isWord) return true;
      node = node.next[idx];
    }
    return false;
  }  
}</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-133/">花花酱 LeetCode Weekly Contest 133</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-133/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Weekly Contest 131 (1021, 1022, 1023, 1024)</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 07 Apr 2019 22:46:19 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[weekly]]></category>
		<category><![CDATA[weekly contest]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5015</guid>

					<description><![CDATA[<p>LeetCode 1021. Remove Outermost Parentheses Solution: Track # of opened parentheses Let n denote the # of opened parentheses after current char, keep &#8216;(&#8216; if&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/">花花酱 LeetCode Weekly Contest 131 (1021, 1022, 1023, 1024)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><strong>LeetCode 1021. Remove Outermost Parentheses</strong></p>



<p>Solution: Track # of opened parentheses</p>



<p>Let n denote the # of  opened parentheses after current char, keep &#8216;(&#8216; if n &gt; 1 and keep &#8216;)&#8217; if n &gt; 0</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string removeOuterParentheses(string S) {    
    string ans;
    int n = 0;    
    for (char c : S) {      
      if (c == '(' &amp;amp;&amp;amp; n++) ans += c;
      if (c == ')' &amp;amp;&amp;amp; --n) ans += c;      
    }
    return ans;
  }
};</pre>
</div></div>



<p><strong>LeetCode 1022. Sum of Root To Leaf Binary Numbers</strong></p>



<p>Solution: Recursion + Math</p>



<p>Keep tracking the number from root to current node.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
    int sumRootToLeaf(TreeNode* root) {      
      return sums(root, 0);
    }
private:
    static constexpr int kMod = 1e9 + 7;
    int sums(TreeNode* root, int c) {
      if (!root) return 0;
      c = ((c &lt;&lt; 1) | root-&gt;val) % kMod;
      if (!root-&gt;left &amp;&amp; !root-&gt;right) {
        return c;
      }
      return sums(root-&gt;left, c) + sums(root-&gt;right, c);
    }
};</pre>
</div></div>



<p><strong>LeetCode 1023. Camelcase Matching</strong></p>



<p>Solution: String&#8230;</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;bool&gt; camelMatch(vector&lt;string&gt;&amp; queries, string p) {
    vector&lt;bool&gt; ans;
    for (const string&amp; q : queries)
      ans.push_back(match(q, p));
    return ans;
  }
private:
  bool match(const string&amp; q, const string&amp; p) {    
    int m = p.length();
    int n = q.length();
    int i = 0;
    int j = 0;
    for (i = 0; i &lt; n; ++i) {      
      if (j == m &amp;&amp; isupper(q[i])) return false;      
      if ((j == m || isupper(p[j])) &amp;&amp; islower(q[i])) continue;        
      if ((isupper(p[j]) || isupper(q[i])) &amp;&amp; p[j] != q[i]) return false;
      if (islower(p[j]) &amp;&amp; p[j] != q[i]) continue;
      ++j;
    }
    return i == n &amp;&amp; j == m;
  }
};</pre>
</div></div>



<p><strong>LeetCode 1024. Video Stitching</strong></p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/tdrPFN9d1y4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Solution 1: DP</p>



<p>Time complexity: O(nT^2)<br>Space complexity: O(T^2)</p>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1024-ep248.png" alt="" class="wp-image-5024" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1024-ep248.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1024-ep248-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1024-ep248-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 16 ms / 9.5 MB
class Solution {
public:
  int videoStitching(vector&lt;vector&lt;int&gt;&gt;&amp; clips, int T) {
    constexpr int kInf = 101;
    // dp[i][j] := min clips to cover range [i, j]
    vector&lt;vector&lt;int&gt;&gt; dp(T + 1, vector&lt;int&gt;(T + 1, kInf));   
    for (const auto&amp; c : clips) {
      int s = c[0];
      int e = c[1];
      for (int l = 1; l &lt;= T; ++l) {
        for (int i = 0; i &lt;= T - l; ++i) {
          int j = i + l;
          if (s &gt; j || e &lt; i) continue;
          if (s &lt;= i &amp;&amp; e &gt;= j) dp[i][j] = 1;
          else if (e &gt;= j) dp[i][j] = min(dp[i][j], dp[i][s] + 1);
          else if (s &lt;= i) dp[i][j] = min(dp[i][j], dp[e][j] + 1);
          else dp[i][j] = min(dp[i][j], dp[i][s] + 1 + dp[e][j]);          
        }
      }
    }
    return dp[0][T] == kInf ? -1 : dp[0][T];
  }
};</pre>

</div><h2 class="tabtitle">C++/V2</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, running time: 20 ms / 9.4 MB
class Solution {
public:
  int videoStitching(vector&lt;vector&lt;int&gt;&gt;&amp; clips, int T) {
    constexpr int kInf = 101;
    // dp[i][j] := min clips to cover range [i, j]
    vector&lt;vector&lt;int&gt;&gt; dp(T + 1, vector&lt;int&gt;(T + 1));
    for (int i = 0; i &lt;= T; ++i)
      for (int j = 0; j &lt;= T; ++j)
        dp[i][j] = i &lt; j ? kInf : 0;
    for (const auto&amp; c : clips) {
      const int s = min(c[0], T);
      const int e = min(c[1], T);
      for (int l = 1; l &lt;= T; ++l)
        for (int i = 0, j = l; j &lt;= T; ++i, ++j)
          dp[i][j] = min(dp[i][j], dp[i][s] + 1 + dp[e][j]);
    }
    return dp[0][T] == kInf ? -1 : dp[0][T];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/">花花酱 LeetCode Weekly Contest 131 (1021, 1022, 1023, 1024)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-131-1021-1022-1023-1024/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Weekly Contest 130 (1017, 1018, 1019, 1020)</title>
		<link>https://zxi.mytechroad.com/blog/uncategorized/leetcode-weekly-contest-130/</link>
					<comments>https://zxi.mytechroad.com/blog/uncategorized/leetcode-weekly-contest-130/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 Mar 2019 04:58:31 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[weekly]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4993</guid>

					<description><![CDATA[<p>1017. Convert to Base -2 Solution: Math / Simulation Time complexity: O(logn)Space complexity: O(logn) [crayon-663c8eabd504e939611503/] [crayon-663c8eabd5050757636838/] 1018.&#160;Binary Prefix Divisible By 5 Solution: Math Time complexity:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/uncategorized/leetcode-weekly-contest-130/">花花酱 LeetCode Weekly Contest 130 (1017, 1018, 1019, 1020)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/uo0jozNyNlg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>1017. Convert to Base -2</strong></h2>



<p><strong>Solution: Math / Simulation</strong></p>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1017-ep247.png" alt="" class="wp-image-5007" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1017-ep247.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1017-ep247-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/04/1017-ep247-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Time complexity: O(logn)<br>Space complexity: O(logn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string baseNeg2(int N) {
    if (N == 0) return &quot;0&quot;; 
    vector&lt;char&gt; ans;
    while (N) {
      ans.push_back((N &amp; 1) + '0');
      N = -(N &gt;&gt; 1);
    }    
    return {rbegin(ans), rend(ans)};
  }
};</pre>

</div><h2 class="tabtitle">base K</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:  
  string baseNeg2(int N) {   
    if (N == 0) return &quot;0&quot;; 
    return baseNegK(N, -2);
  }
private:
  string baseNegK(int N, int K) {
    vector&lt;char&gt; ans;
    while (N) {
      int r = N % K;
      N /= K;
      if (r &lt; 0) {
        r += -K;
        N += 1;
      }
      ans.push_back(r + '0');
    }    
    return {rbegin(ans), rend(ans)};
  }
};</pre>
</div></div>



<h2><strong> 1018.&nbsp;Binary Prefix Divisible By 5</strong><br></h2>



<p><strong>Solution: Math</strong></p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;bool&gt; prefixesDivBy5(vector&lt;int&gt;&amp; A) {
    int num = 0;
    vector&lt;bool&gt; ans(A.size());    
    for (int i = 0; i &lt; A.size(); ++i) {
      num = ((num &lt;&lt; 1) | A[i]) % 5;
      ans[i] = num == 0;
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong> 1019.&nbsp;Next Greater Node In Linked List</strong><br></h2>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/e4BtnOVy0Gs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p><strong>Solution: Reverse + Monotonic Stack</strong></p>



<p>Process in reverse order and keep a monotonically increasing stack, pop all the elements that are smaller than the current one, then the top of the stack (if exists) will be the next greater node.</p>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/03/1030-ep246.png" alt="" class="wp-image-5002" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/03/1030-ep246.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/03/1030-ep246-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/03/1030-ep246-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; nextLargerNodes(ListNode* head) {    
    vector&lt;int&gt; nums;
    while (head) {
      nums.push_back(head-&gt;val);
      head = head-&gt;next;      
    }
    stack&lt;int&gt; s;
    vector&lt;int&gt; ans(nums.size());
    for (int i = nums.size() - 1; i &gt;= 0; --i) {
      while (!s.empty() &amp;&amp; s.top() &lt;= nums[i]) s.pop();
      ans[i] = s.empty() ? 0 : s.top();
      s.push(nums[i]);
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>1020. Number of Enclaves</strong></h2>



<p><strong>Solution: DFS / Connected Components</strong></p>



<p>Time complexity: O(mn)<br>Space complexity: O(mn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numEnclaves(vector&lt;vector&lt;int&gt;&gt;&amp; A) {
    int ans = 0;
    for (int i = 0; i &lt; A.size(); ++i)
      for (int j = 0; j &lt; A[0].size(); ++j) {
        int count = 0;
        if (!dfs(A, j, i, count))
          ans += count;
      }
    return ans;
  }
private:
  static constexpr int dirs[5]{0, -1, 0, 1, 0};
  bool dfs(vector&lt;vector&lt;int&gt;&gt;&amp; A, int x, int y, int&amp; count) {
    if (x &lt; 0 || x == A[0].size() || y &lt; 0 || y == A.size()) return true;    
    if (A[y][x] == 0) return false;    
    ++count;
    A[y][x] = 0;
    bool valid = false;    
    for (int i = 0; i &lt; 4; ++i)
      valid |= dfs(A, x + dirs[i], y + dirs[i + 1], count);
    return valid;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/uncategorized/leetcode-weekly-contest-130/">花花酱 LeetCode Weekly Contest 130 (1017, 1018, 1019, 1020)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/uncategorized/leetcode-weekly-contest-130/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Weekly Contest 129 (1020, 1021, 1022, 1023)</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-129-1020-1021-1022-1023/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-129-1020-1021-1022-1023/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 24 Mar 2019 15:08:48 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[weekly]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4985</guid>

					<description><![CDATA[<p>1020.&#160;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 [crayon-663c8eabd52e8178811606/] Time complexity:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-129-1020-1021-1022-1023/">花花酱 LeetCode Weekly Contest 129 (1020, 1021, 1022, 1023)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2><strong>1020.&nbsp;Partition Array Into Three Parts With Equal Sum</strong></h2>



<p>Return true if there is a 2/3*sum prefix sum after a 1/3 prefix sum</p>



<pre class="crayon-plain-tag">[---- 1/3 ----][-----1/3-----][-----1/3-----]
[------------2/3-------------][-----1/3-----]</pre>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def canThreePartsEqualSum(self, A: List[int]) -&amp;gt; bool:
    s = sum(A)
    if s % 3 != 0: return False    
    c = 0
    found = False
    for a in A:
      c += a
      if c == s // 3 * 2 and found: return True
      if c == s // 3: found = True
    return False</pre>
</div></div>



<h2><strong>1021.&nbsp;Best Sightseeing Pair</strong></h2>



<p>Greedy, only keep the best A[i] + i so far and pair with A[j] &#8211; j, i &lt; j</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int maxScoreSightseeingPair(vector&lt;int&gt;&amp; A) {
    int ans = 0;
    int left = 0;
    for (int i = 0; i &lt; A.size(); ++i) {
      ans = max(ans, left + A[i] - i);
      left = max(left, A[i] + i);
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>1022.&nbsp;Smallest Integer Divisible by K</strong></h2>



<p>Math</p>



<p>Time complexity: O(K)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution {
public:
  int smallestRepunitDivByK(int K) {
    if (K % 2 == 0 || K % 5 == 0) return -1;
    int r = 0;
    for (int l = 1; l &lt;= K; ++l) {
      r = (r * 10 + 1) % K;
      if (r == 0) return l;
    }
    return -1;
  }
};</pre>
</div></div>



<h2><strong>1023.&nbsp;Binary String With Substrings Representing 1 To N</strong></h2>



<p>Brute Force, try all possible substrings and convert them into decimals.</p>



<p>Time complexity: O(|S|*log(N)^2)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">Python</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def queryString(self, S: str, N: int) -&amp;gt; bool:
    s = set()
    c = 0    
    for l in range(1, 32):
      for i in range(0, len(S) - l + 1):        
        n = int(S[i:i+l], 2)
        if n == 0 or n &gt; N or n in s: continue        
        s.add(n)
        c += 1
    return c == N</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-129-1020-1021-1022-1023/">花花酱 LeetCode Weekly Contest 129 (1020, 1021, 1022, 1023)</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-129-1020-1021-1022-1023/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Weekly 123</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-123/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-123/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 11 Feb 2019 04:28:00 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[weekly]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4840</guid>

					<description><![CDATA[<p>Solutions https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/ https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/ https://zxi.mytechroad.com/blog/greedy/leetcode-991-broken-calculator/ https://zxi.mytechroad.com/blog/two-pointers/leetcode-992-subarrays-with-k-different-integers/</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-123/">花花酱 LeetCode Weekly 123</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/FZPtxuxArLU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<h2><strong>Solutions</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/">https://zxi.mytechroad.com/blog/simulation/leetcode-989-add-to-array-form-of-integer/</a></li><li><a href="https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/">https://zxi.mytechroad.com/blog/graph/leetcode-990-satisfiability-of-equality-equations/</a></li><li><a href="https://zxi.mytechroad.com/blog/greedy/leetcode-991-broken-calculator/">https://zxi.mytechroad.com/blog/greedy/leetcode-991-broken-calculator/</a></li><li><a href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-992-subarrays-with-k-different-integers/">https://zxi.mytechroad.com/blog/two-pointers/leetcode-992-subarrays-with-k-different-integers/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-123/">花花酱 LeetCode Weekly 123</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-123/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode Weekly Contest 121</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-121/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-121/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 27 Jan 2019 18:39:19 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[weekly]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4706</guid>

					<description><![CDATA[<p>https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/ https://zxi.mytechroad.com/blog/bit/leetcode-982-triples-with-bitwise-and-equal-to-zero/ https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/ https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-121/">花花酱 LeetCode Weekly Contest 121</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/wByfMK9vL6M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<figure class="wp-block-image"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/01/Weekly-121.png" alt="" class="wp-image-4707" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2019/01/Weekly-121.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/01/Weekly-121-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2019/01/Weekly-121-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<ul><li><a href="https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/">https://zxi.mytechroad.com/blog/greedy/leetcode-984-string-without-aaa-or-bbb/</a></li><li><a href="https://zxi.mytechroad.com/blog/bit/leetcode-982-triples-with-bitwise-and-equal-to-zero/">https://zxi.mytechroad.com/blog/bit/leetcode-982-triples-with-bitwise-and-equal-to-zero/</a></li><li><a href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/">https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-983-minimum-cost-for-tickets/</a></li><li><a href="https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/">https://zxi.mytechroad.com/blog/hashtable/leetcode-981-time-based-key-value-store/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-121/">花花酱 LeetCode Weekly Contest 121</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-weekly-contest-121/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
