Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ LeetCode Problems' Solutions

| # | Title | Solution | Difficulty |
| :-: | - | - | :-: |
| <span id="1117">1117</span> | [Building H2O](https://leetcode.com/problems/building-h2o) | [Go](https://github.com/openset/leetcode/tree/master/problems/building-h2o) | Hard |
| <span id="1116">1116</span> | [Print Zero Even Odd](https://leetcode.com/problems/print-zero-even-odd) | [Go](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd) | Medium |
| <span id="1115">1115</span> | [Print FooBar Alternately](https://leetcode.com/problems/print-foobar-alternately) | [Go](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) | Medium |
| <span id="1114">1114</span> | [Print in Order](https://leetcode.com/problems/print-in-order) | [Go](https://github.com/openset/leetcode/tree/master/problems/print-in-order) | Easy |
| <span id="1113">1113</span> | [Reported Posts](https://leetcode.com/problems/reported-posts) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/reported-posts) | Easy |
| <span id="1112">1112</span> | [Highest Grade For Each Student](https://leetcode.com/problems/highest-grade-for-each-student) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/highest-grade-for-each-student) | Medium |
| <span id="1111">1111</span> | [Maximum Nesting Depth of Two Valid Parentheses Strings](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings "有效括号的嵌套深度") | [Go](https://github.com/openset/leetcode/tree/master/problems/maximum-nesting-depth-of-two-valid-parentheses-strings) | Medium |
Expand Down
56 changes: 56 additions & 0 deletions problems/building-h2o/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author Openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd "Print Zero Even Odd")

Next >

## 1117. Building H2O (Hard)

<p>There are two kinds of threads, <code>oxygen</code> and <code>hydrogen</code>. Your goal is to group these threads to form water molecules.&nbsp;There is a barrier where each thread has to&nbsp;wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given a <code>releaseHydrogen</code>&nbsp;and <code>releaseOxygen</code>&nbsp;method respectfully, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must be able to immediately bond with each other to form a water molecule.&nbsp;You must guarantee that all the threads from one molecule bond <em>before</em> any other threads from the next molecule do.</p>

<p>In other words:</p>

<ul>
<li>If an oxygen thread arrives at the barrier when no hydrogen threads are present, it has to wait for two hydrogen threads.</li>
<li>If a hydrogen thread arrives at the barrier when no other threads are present, it has to wait for an oxygen thread and another hydrogen thread.</li>
</ul>

<p>Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.</p>

<div>
<p>&nbsp;</p>
</div>

<div>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input: </strong><span id="example-input-1-1">&quot;HOH&quot;</span>
<strong>Output: </strong><span id="example-output-1">&quot;HHO&quot;
<strong>Explanation:</strong> &quot;HOH&quot; and &quot;OHH&quot; are also valid answers.</span>
</pre>

<div>
<p><strong>Example 2:</strong></p>

<pre>
<strong>Input: </strong><span id="example-input-1-1">&quot;OOHHHH&quot;</span>
<strong>Output: </strong><span id="example-output-1">&quot;HHOHHO&quot;
<strong>Explanation:</strong> &quot;HOHHHO&quot;, &quot;OHHHHO&quot;, &quot;HHOHOH&quot;, &quot;HOHHOH&quot;, &quot;OHHHOH&quot;, &quot;HHOOHH&quot;, &quot;HOHOHH&quot; and &quot;OHHOHH&quot; are also valid answers.</span>
</pre>
</div>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>Total length of input string will be 3<em>n</em>, where 1 &le;&nbsp;<em>n</em>&nbsp;&le; 50.</li>
<li>Total number of <code>H</code> will be 2<em>n</em>&nbsp;in the input string.</li>
<li>Total number of <code>O</code> will&nbsp;be <em>n</em>&nbsp;in the input&nbsp;string.</li>
</ul>
2 changes: 1 addition & 1 deletion problems/largest-rectangle-in-histogram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</pre>

### Related Topics
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
[[Stack](https://github.com/openset/leetcode/tree/master/tag/stack/README.md)]
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]

### Similar Questions
1. [Maximal Rectangle](https://github.com/openset/leetcode/tree/master/problems/maximal-rectangle) (Hard)
2 changes: 1 addition & 1 deletion problems/maximal-rectangle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
</pre>

### Related Topics
[[Stack](https://github.com/openset/leetcode/tree/master/tag/stack/README.md)]
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
[[Hash Table](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)]
[[Dynamic Programming](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)]
[[Stack](https://github.com/openset/leetcode/tree/master/tag/stack/README.md)]

### Similar Questions
1. [Largest Rectangle in Histogram](https://github.com/openset/leetcode/tree/master/problems/largest-rectangle-in-histogram) (Hard)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

[Next >](https://github.com/openset/leetcode/tree/master/problems/highest-grade-for-each-student "Highest Grade For Each Student")

## 1111. Maximum Nesting Depth of Two Valid Parentheses Strings (Hard)
## 1111. Maximum Nesting Depth of Two Valid Parentheses Strings (Medium)

<p>A string is a <em>valid parentheses string</em>&nbsp;(denoted VPS) if and only if it consists of <code>&quot;(&quot;</code> and <code>&quot;)&quot;</code> characters only, and:</p>

Expand Down Expand Up @@ -56,5 +56,5 @@
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= text.size &lt;= 10000</code></li>
<li><code>1 &lt;= seq.size &lt;= 10000</code></li>
</ul>
54 changes: 54 additions & 0 deletions problems/print-foobar-alternately/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author Openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/print-in-order "Print in Order")

[Next >](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd "Print Zero Even Odd")

## 1115. Print FooBar Alternately (Medium)

<p>Suppose you are given the following code:</p>

<pre>
class FooBar {
public void foo() {
&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {
&nbsp; &nbsp; &nbsp; print(&quot;foo&quot;);
&nbsp; }
}

public void bar() {
&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {
&nbsp; &nbsp; &nbsp; print(&quot;bar&quot;);
&nbsp; &nbsp; }
}
}
</pre>

<p>The same instance of <code>FooBar</code> will be passed to two different threads. Thread A will call&nbsp;<code>foo()</code> while thread B will call&nbsp;<code>bar()</code>.&nbsp;Modify the given program to output &quot;foobar&quot; <em>n</em> times.</p>

<p>&nbsp;</p>

<p><strong>Example 1:</strong></p>

<pre>
<b>Input:</b> n = 1
<b>Output:</b> &quot;foobar&quot;
<strong>Explanation:</strong> There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). &quot;foobar&quot; is being output 1 time.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<b>Input:</b> n = 2
<b>Output:</b> &quot;foobarfoobar&quot;
<strong>Explanation:</strong> &quot;foobar&quot; is being output 2 times.
</pre>

### Similar Questions
1. [Print in Order](https://github.com/openset/leetcode/tree/master/problems/print-in-order) (Easy)
1. [Print Zero Even Odd](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd) (Medium)
50 changes: 50 additions & 0 deletions problems/print-in-order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author Openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/reported-posts "Reported Posts")

[Next >](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately "Print FooBar Alternately")

## 1114. Print in Order (Easy)

<p>Suppose we have a class:</p>

<pre>
public class Foo {
&nbsp; public void one() { print(&quot;one&quot;); }
&nbsp; public void two() { print(&quot;two&quot;); }
&nbsp; public void three() { print(&quot;three&quot;); }
}
</pre>

<p>The same instance of <code>Foo</code> will be passed to three different threads. Thread A will call <code>one()</code>, thread B will call <code>two()</code>, and thread C will call <code>three()</code>. Design a mechanism and modify the program&nbsp;to ensure that&nbsp;<code>two()</code>&nbsp;is executed after&nbsp;<code>one()</code>, and&nbsp;<code>three()</code> is executed after&nbsp;<code>two()</code>.</p>

<p>&nbsp;</p>

<p><strong>Example 1:</strong></p>

<pre>
<b>Input:</b> [1,2,3]
<b>Output:</b> &quot;onetwothree&quot;
<strong>Explanation:</strong> There are three threads being fired asynchronously. The input [1,2,3] means thread A calls one(), thread B calls two(), and thread C calls three(). &quot;onetwothree&quot; is the correct output.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<b>Input:</b> [1,3,2]
<b>Output:</b> &quot;onetwothree&quot;
<strong>Explanation:</strong> The input [1,3,2] means thread A calls one(), thread B calls three(), and thread C calls two(). &quot;onetwothree&quot; is the correct output.</pre>

<p>&nbsp;</p>

<p><strong>Note:</strong></p>

<p>We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seems to imply the ordering. The input format you see is mainly&nbsp;to ensure our tests&#39; comprehensiveness.</p>

### Similar Questions
1. [Print FooBar Alternately](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) (Medium)
53 changes: 53 additions & 0 deletions problems/print-zero-even-odd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
<!--+----------------------------------------------------------------------+-->
<!--|@author Openset <openset.wang@gmail.com> |-->
<!--|@link https://github.com/openset |-->
<!--|@home https://github.com/openset/leetcode |-->
<!--+----------------------------------------------------------------------+-->

[< Previous](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately "Print FooBar Alternately")

[Next >](https://github.com/openset/leetcode/tree/master/problems/building-h2o "Building H2O")

## 1116. Print Zero Even Odd (Medium)

<p>Suppose you are given the following code:</p>

<pre>
class ZeroEvenOdd {
&nbsp; public ZeroEvenOdd(int n) { ... }&nbsp; // constructor
public void zero(printNumber) { ... } // only output 0&#39;s
public void even(printNumber) { ... } // only output even numbers
public void odd(printNumber) { ... } // only output odd numbers
}
</pre>

<p>The same instance of <code>ZeroEvenOdd</code> will be passed to three different threads:</p>

<ol>
<li>Thread A will call&nbsp;<code>zero()</code>&nbsp;which should only output 0&#39;s.</li>
<li>Thread B will call&nbsp;<code>even()</code>&nbsp;which should only ouput even numbers.</li>
<li>Thread C will call <code>odd()</code>&nbsp;which should only output odd numbers.</li>
</ol>

<p>Each of the thread is given a&nbsp;<code>printNumber</code> method to output&nbsp;an integer. Modify the given program to output the series&nbsp;<code>010203040506</code>... where the length of the series must be 2<em>n</em>.</p>

<p>&nbsp;</p>

<p><strong>Example 1:</strong></p>

<pre>
<b>Input:</b> n = 2
<b>Output:</b> &quot;0102&quot;
<strong>Explanation:</strong> There are three threads being fired asynchronously. One of them calls zero(), the other calls even(), and the last one calls odd(). &quot;0102&quot; is the correct output.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<b>Input:</b> n = 5
<b>Output:</b> &quot;0102030405&quot;
</pre>

### Similar Questions
1. [Print FooBar Alternately](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) (Medium)
2 changes: 1 addition & 1 deletion problems/reported-posts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[< Previous](https://github.com/openset/leetcode/tree/master/problems/highest-grade-for-each-student "Highest Grade For Each Student")

Next >
[Next >](https://github.com/openset/leetcode/tree/master/problems/print-in-order "Print in Order")

## 1113. Reported Posts (Easy)

Expand Down
2 changes: 1 addition & 1 deletion problems/sort-colors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
</ul>

### Related Topics
[[Sort](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)]
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
[[Two Pointers](https://github.com/openset/leetcode/tree/master/tag/two-pointers/README.md)]
[[Sort](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)]

### Similar Questions
1. [Sort List](https://github.com/openset/leetcode/tree/master/problems/sort-list) (Medium)
Expand Down
2 changes: 1 addition & 1 deletion problems/subsets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
]</pre>

### Related Topics
[[Bit Manipulation](https://github.com/openset/leetcode/tree/master/tag/bit-manipulation/README.md)]
[[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
[[Backtracking](https://github.com/openset/leetcode/tree/master/tag/backtracking/README.md)]
[[Bit Manipulation](https://github.com/openset/leetcode/tree/master/tag/bit-manipulation/README.md)]

### Similar Questions
1. [Subsets II](https://github.com/openset/leetcode/tree/master/problems/subsets-ii) (Medium)
Expand Down
2 changes: 1 addition & 1 deletion readme/301-600.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ LeetCode Problems' Solutions
| <span id="344">344</span> | [Reverse String](https://leetcode.com/problems/reverse-string "反转字符串") | [Go](https://github.com/openset/leetcode/tree/master/problems/reverse-string) | Easy |
| <span id="345">345</span> | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string "反转字符串中的元音字母") | [Go](https://github.com/openset/leetcode/tree/master/problems/reverse-vowels-of-a-string) | Easy |
| <span id="346">346</span> | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream "数据流中的移动平均值") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/moving-average-from-data-stream) | Easy |
| <span id="347">347</span> | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements "前K个高频元素") | [Go](https://github.com/openset/leetcode/tree/master/problems/top-k-frequent-elements) | Medium |
| <span id="347">347</span> | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements "前 K 个高频元素") | [Go](https://github.com/openset/leetcode/tree/master/problems/top-k-frequent-elements) | Medium |
| <span id="348">348</span> | [Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe "判定井字棋胜负") 🔒 | [Go](https://github.com/openset/leetcode/tree/master/problems/design-tic-tac-toe) | Medium |
| <span id="349">349</span> | [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays "两个数组的交集") | [Go](https://github.com/openset/leetcode/tree/master/problems/intersection-of-two-arrays) | Easy |
| <span id="350">350</span> | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii "两个数组的交集 II") | [Go](https://github.com/openset/leetcode/tree/master/problems/intersection-of-two-arrays-ii) | Easy |
Expand Down
2 changes: 1 addition & 1 deletion tag/hash-table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
| 355 | [设计推特](https://github.com/openset/leetcode/tree/master/problems/design-twitter) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[设计](https://github.com/openset/leetcode/tree/master/tag/design/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] | Medium |
| 350 | [两个数组的交集 II](https://github.com/openset/leetcode/tree/master/problems/intersection-of-two-arrays-ii) | [[排序](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] [[双指针](https://github.com/openset/leetcode/tree/master/tag/two-pointers/README.md)] [[二分查找](https://github.com/openset/leetcode/tree/master/tag/binary-search/README.md)] | Easy |
| 349 | [两个数组的交集](https://github.com/openset/leetcode/tree/master/problems/intersection-of-two-arrays) | [[排序](https://github.com/openset/leetcode/tree/master/tag/sort/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] [[双指针](https://github.com/openset/leetcode/tree/master/tag/two-pointers/README.md)] [[二分查找](https://github.com/openset/leetcode/tree/master/tag/binary-search/README.md)] | Easy |
| 347 | [前K个高频元素](https://github.com/openset/leetcode/tree/master/problems/top-k-frequent-elements) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] | Medium |
| 347 | [前 K 个高频元素](https://github.com/openset/leetcode/tree/master/problems/top-k-frequent-elements) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] | Medium |
| 340 | [至多包含 K 个不同字符的最长子串](https://github.com/openset/leetcode/tree/master/problems/longest-substring-with-at-most-k-distinct-characters) 🔒 | [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] [[字符串](https://github.com/openset/leetcode/tree/master/tag/string/README.md)] [[Sliding Window](https://github.com/openset/leetcode/tree/master/tag/sliding-window/README.md)] | Hard |
| 336 | [回文对](https://github.com/openset/leetcode/tree/master/problems/palindrome-pairs) | [[字典树](https://github.com/openset/leetcode/tree/master/tag/trie/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] [[字符串](https://github.com/openset/leetcode/tree/master/tag/string/README.md)] | Hard |
| 325 | [和等于 k 的最长子数组长度](https://github.com/openset/leetcode/tree/master/problems/maximum-size-subarray-sum-equals-k) 🔒 | [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] | Medium |
Expand Down
Loading