From e9695f936cc2b713b30114aecda9ef928c421021 Mon Sep 17 00:00:00 2001 From: openset Date: Sat, 13 Jul 2019 22:20:15 +0800 Subject: [PATCH] Add: new --- README.md | 4 ++ problems/building-h2o/README.md | 56 +++++++++++++++++++ .../largest-rectangle-in-histogram/README.md | 2 +- problems/maximal-rectangle/README.md | 2 +- .../README.md | 4 +- problems/print-foobar-alternately/README.md | 54 ++++++++++++++++++ problems/print-in-order/README.md | 50 +++++++++++++++++ problems/print-zero-even-odd/README.md | 53 ++++++++++++++++++ problems/reported-posts/README.md | 2 +- problems/sort-colors/README.md | 2 +- problems/subsets/README.md | 2 +- readme/301-600.md | 2 +- tag/hash-table/README.md | 2 +- tag/heap/README.md | 2 +- 14 files changed, 227 insertions(+), 10 deletions(-) create mode 100644 problems/building-h2o/README.md create mode 100644 problems/print-foobar-alternately/README.md create mode 100644 problems/print-in-order/README.md create mode 100644 problems/print-zero-even-odd/README.md diff --git a/README.md b/README.md index ecdf1ae92..caf14d60d 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,10 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1117 | [Building H2O](https://leetcode.com/problems/building-h2o) | [Go](https://github.com/openset/leetcode/tree/master/problems/building-h2o) | Hard | +| 1116 | [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 | +| 1115 | [Print FooBar Alternately](https://leetcode.com/problems/print-foobar-alternately) | [Go](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) | Medium | +| 1114 | [Print in Order](https://leetcode.com/problems/print-in-order) | [Go](https://github.com/openset/leetcode/tree/master/problems/print-in-order) | Easy | | 1113 | [Reported Posts](https://leetcode.com/problems/reported-posts) 🔒 | [MySQL](https://github.com/openset/leetcode/tree/master/problems/reported-posts) | Easy | | 1112 | [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 | | 1111 | [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 | diff --git a/problems/building-h2o/README.md b/problems/building-h2o/README.md new file mode 100644 index 000000000..8f6770e45 --- /dev/null +++ b/problems/building-h2o/README.md @@ -0,0 +1,56 @@ + + + + + + + +[< Previous](https://github.com/openset/leetcode/tree/master/problems/print-zero-even-odd "Print Zero Even Odd") +                 +Next > + +## 1117. Building H2O (Hard) + +

There are two kinds of threads, oxygen and hydrogen. Your goal is to group these threads to form water molecules. There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given a releaseHydrogen and releaseOxygen 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. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.

+ +

In other words:

+ + + +

Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

+ +
+

 

+
+ +
+

Example 1:

+ +
+Input: "HOH"
+Output: "HHO"
+Explanation: "HOH" and "OHH" are also valid answers.
+
+ +
+

Example 2:

+ +
+Input: "OOHHHH"
+Output: "HHOHHO"
+Explanation: "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.
+
+
+
+ +

 

+

Constraints:

+ + diff --git a/problems/largest-rectangle-in-histogram/README.md b/problems/largest-rectangle-in-histogram/README.md index c0297fe03..738d4f273 100644 --- a/problems/largest-rectangle-in-histogram/README.md +++ b/problems/largest-rectangle-in-histogram/README.md @@ -33,8 +33,8 @@ ### 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) diff --git a/problems/maximal-rectangle/README.md b/problems/maximal-rectangle/README.md index 41f54ea8d..db7c5a9e3 100644 --- a/problems/maximal-rectangle/README.md +++ b/problems/maximal-rectangle/README.md @@ -27,10 +27,10 @@ ### 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) diff --git a/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/README.md b/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/README.md index caed4fc30..bef8f994f 100644 --- a/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/README.md +++ b/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/README.md @@ -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)

A string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and:

@@ -56,5 +56,5 @@

Constraints:

diff --git a/problems/print-foobar-alternately/README.md b/problems/print-foobar-alternately/README.md new file mode 100644 index 000000000..cc70176af --- /dev/null +++ b/problems/print-foobar-alternately/README.md @@ -0,0 +1,54 @@ + + + + + + + +[< 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) + +

Suppose you are given the following code:

+ +
+class FooBar {
+  public void foo() {
+    for (int i = 0; i < n; i++) {
+      print("foo");
+    }
+  }
+
+  public void bar() {
+    for (int i = 0; i < n; i++) {
+      print("bar");
+    }
+  }
+}
+
+ +

The same instance of FooBar will be passed to two different threads. Thread A will call foo() while thread B will call bar(). Modify the given program to output "foobar" n times.

+ +

 

+ +

Example 1:

+ +
+Input: n = 1
+Output: "foobar"
+Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). "foobar" is being output 1 time.
+
+ +

Example 2:

+ +
+Input: n = 2
+Output: "foobarfoobar"
+Explanation: "foobar" is being output 2 times.
+
+ +### 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) diff --git a/problems/print-in-order/README.md b/problems/print-in-order/README.md new file mode 100644 index 000000000..d5e80ab38 --- /dev/null +++ b/problems/print-in-order/README.md @@ -0,0 +1,50 @@ + + + + + + + +[< 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) + +

Suppose we have a class:

+ +
+public class Foo {
+  public void one() { print("one"); }
+  public void two() { print("two"); }
+  public void three() { print("three"); }
+}
+
+ +

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

+ +

 

+ +

Example 1:

+ +
+Input: [1,2,3]
+Output: "onetwothree"
+Explanation: 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(). "onetwothree" is the correct output.
+
+ +

Example 2:

+ +
+Input: [1,3,2]
+Output: "onetwothree"
+Explanation: The input [1,3,2] means thread A calls one(), thread B calls three(), and thread C calls two(). "onetwothree" is the correct output.
+ +

 

+ +

Note:

+ +

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 to ensure our tests' comprehensiveness.

+ +### Similar Questions + 1. [Print FooBar Alternately](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) (Medium) diff --git a/problems/print-zero-even-odd/README.md b/problems/print-zero-even-odd/README.md new file mode 100644 index 000000000..d8c95a3a2 --- /dev/null +++ b/problems/print-zero-even-odd/README.md @@ -0,0 +1,53 @@ + + + + + + + +[< 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) + +

Suppose you are given the following code:

+ +
+class ZeroEvenOdd {
+  public ZeroEvenOdd(int n) { ... }      // constructor
+  public void zero(printNumber) { ... }  // only output 0's
+  public void even(printNumber) { ... }  // only output even numbers
+  public void odd(printNumber) { ... }   // only output odd numbers
+}
+
+ +

The same instance of ZeroEvenOdd will be passed to three different threads:

+ +
    +
  1. Thread A will call zero() which should only output 0's.
  2. +
  3. Thread B will call even() which should only ouput even numbers.
  4. +
  5. Thread C will call odd() which should only output odd numbers.
  6. +
+ +

Each of the thread is given a printNumber method to output an integer. Modify the given program to output the series 010203040506... where the length of the series must be 2n.

+ +

 

+ +

Example 1:

+ +
+Input: n = 2
+Output: "0102"
+Explanation: There are three threads being fired asynchronously. One of them calls zero(), the other calls even(), and the last one calls odd(). "0102" is the correct output.
+
+ +

Example 2:

+ +
+Input: n = 5
+Output: "0102030405"
+
+ +### Similar Questions + 1. [Print FooBar Alternately](https://github.com/openset/leetcode/tree/master/problems/print-foobar-alternately) (Medium) diff --git a/problems/reported-posts/README.md b/problems/reported-posts/README.md index 1eb7a03fa..d1ba8f518 100644 --- a/problems/reported-posts/README.md +++ b/problems/reported-posts/README.md @@ -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) diff --git a/problems/sort-colors/README.md b/problems/sort-colors/README.md index f56df3c27..295f0c19a 100644 --- a/problems/sort-colors/README.md +++ b/problems/sort-colors/README.md @@ -32,9 +32,9 @@ ### 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) diff --git a/problems/subsets/README.md b/problems/subsets/README.md index f66d03539..0758cdce9 100644 --- a/problems/subsets/README.md +++ b/problems/subsets/README.md @@ -32,9 +32,9 @@ ] ### 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) diff --git a/readme/301-600.md b/readme/301-600.md index 7e699fd7f..04f185bde 100644 --- a/readme/301-600.md +++ b/readme/301-600.md @@ -100,7 +100,7 @@ LeetCode Problems' Solutions | 344 | [Reverse String](https://leetcode.com/problems/reverse-string "反转字符串") | [Go](https://github.com/openset/leetcode/tree/master/problems/reverse-string) | Easy | | 345 | [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 | | 346 | [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 | -| 347 | [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 | +| 347 | [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 | | 348 | [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 | | 349 | [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 | | 350 | [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 | diff --git a/tag/hash-table/README.md b/tag/hash-table/README.md index 04a52aa9e..85bd5ada0 100644 --- a/tag/hash-table/README.md +++ b/tag/hash-table/README.md @@ -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 | diff --git a/tag/heap/README.md b/tag/heap/README.md index f76ae3ecf..04b69cc96 100644 --- a/tag/heap/README.md +++ b/tag/heap/README.md @@ -34,7 +34,7 @@ | 373 | [查找和最小的K对数字](https://github.com/openset/leetcode/tree/master/problems/find-k-pairs-with-smallest-sums) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] | Medium | | 358 | [K 距离间隔重排字符串](https://github.com/openset/leetcode/tree/master/problems/rearrange-string-k-distance-apart) 🔒 | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[贪心算法](https://github.com/openset/leetcode/tree/master/tag/greedy/README.md)] [[哈希表](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] | Hard | | 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 | -| 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 | | 313 | [超级丑数](https://github.com/openset/leetcode/tree/master/problems/super-ugly-number) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[数学](https://github.com/openset/leetcode/tree/master/tag/math/README.md)] | Medium | | 295 | [数据流的中位数](https://github.com/openset/leetcode/tree/master/problems/find-median-from-data-stream) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[设计](https://github.com/openset/leetcode/tree/master/tag/design/README.md)] | Hard | | 264 | [丑数 II](https://github.com/openset/leetcode/tree/master/problems/ugly-number-ii) | [[堆](https://github.com/openset/leetcode/tree/master/tag/heap/README.md)] [[数学](https://github.com/openset/leetcode/tree/master/tag/math/README.md)] [[动态规划](https://github.com/openset/leetcode/tree/master/tag/dynamic-programming/README.md)] | Medium |