<?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>contest Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/contest/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/contest/</link>
	<description></description>
	<lastBuildDate>Mon, 22 Apr 2019 07:41:46 +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>contest Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/contest/</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>
	</channel>
</rss>
