Skip to content

Commit b97b065

Browse files
committed
Added problem description
1 parent 1cbdabe commit b97b065

File tree

57 files changed

+641
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+641
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h3 align="left"> 1957. Delete Characters to Make Fancy String</h3>
2+
<div><p>A <strong>fancy string</strong> is a string where no <strong>three</strong> <strong>consecutive</strong> characters are equal.</p>
3+
4+
<p>Given a string <code>s</code>, delete the <strong>minimum</strong> possible number of characters from <code>s</code> to make it <strong>fancy</strong>.</p>
5+
6+
<p>Return <em>the final string after the deletion</em>. It can be shown that the answer will always be <strong>unique</strong>.</p>
7+
8+
<p>&nbsp;</p>
9+
<p><strong>Example 1:</strong></p>
10+
11+
<pre><strong>Input:</strong> s = "le<u>e</u>etcode"
12+
<strong>Output:</strong> "leetcode"
13+
<strong>Explanation:</strong>
14+
Remove an 'e' from the first group of 'e's to create "leetcode".
15+
No three consecutive characters are equal, so return "leetcode".
16+
</pre>
17+
18+
<p><strong>Example 2:</strong></p>
19+
20+
<pre><strong>Input:</strong> s = "<u>a</u>aab<u>aa</u>aa"
21+
<strong>Output:</strong> "aabaa"
22+
<strong>Explanation:</strong>
23+
Remove an 'a' from the first group of 'a's to create "aabaaaa".
24+
Remove two 'a's from the second group of 'a's to create "aabaa".
25+
No three consecutive characters are equal, so return "aabaa".
26+
</pre>
27+
28+
<p><strong>Example 3:</strong></p>
29+
30+
<pre><strong>Input:</strong> s = "aab"
31+
<strong>Output:</strong> "aab"
32+
<strong>Explanation:</strong> No three consecutive characters are equal, so return "aab".
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
40+
<li><code>s</code> consists only of lowercase English letters.</li>
41+
</ul>
42+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<h3 align="left"> 1758. Minimum Changes To Make Alternating Binary String</h3>
2+
<div><p>You are given a string <code>s</code> consisting only of the characters <code>'0'</code> and <code>'1'</code>. In one operation, you can change any <code>'0'</code> to <code>'1'</code> or vice versa.</p>
3+
4+
<p>The string is called alternating if no two adjacent characters are equal. For example, the string <code>"010"</code> is alternating, while the string <code>"0100"</code> is not.</p>
5+
6+
<p>Return <em>the <strong>minimum</strong> number of operations needed to make</em> <code>s</code> <em>alternating</em>.</p>
7+
8+
<p>&nbsp;</p>
9+
<p><strong>Example 1:</strong></p>
10+
11+
<pre><strong>Input:</strong> s = "0100"
12+
<strong>Output:</strong> 1
13+
<strong>Explanation:</strong> If you change the last character to '1', s will be "0101", which is alternating.
14+
</pre>
15+
16+
<p><strong>Example 2:</strong></p>
17+
18+
<pre><strong>Input:</strong> s = "10"
19+
<strong>Output:</strong> 0
20+
<strong>Explanation:</strong> s is already alternating.
21+
</pre>
22+
23+
<p><strong>Example 3:</strong></p>
24+
25+
<pre><strong>Input:</strong> s = "1111"
26+
<strong>Output:</strong> 2
27+
<strong>Explanation:</strong> You need two operations to reach "0101" or "1010".
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
35+
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
36+
</ul>
37+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<h3 align="left"> 1368. Minimum Cost to Make at Least One Valid Path in a Grid</h3>
2+
<div><p>Given an <code>m x n</code> grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of <code>grid[i][j]</code> can be:</p>
3+
4+
<ul>
5+
<li><code>1</code> which means go to the cell to the right. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j + 1]</code>)</li>
6+
<li><code>2</code> which means go to the cell to the left. (i.e go from <code>grid[i][j]</code> to <code>grid[i][j - 1]</code>)</li>
7+
<li><code>3</code> which means go to the lower cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i + 1][j]</code>)</li>
8+
<li><code>4</code> which means go to the upper cell. (i.e go from <code>grid[i][j]</code> to <code>grid[i - 1][j]</code>)</li>
9+
</ul>
10+
11+
<p>Notice that there could be some signs on the cells of the grid that point outside the grid.</p>
12+
13+
<p>You will initially start at the upper left cell <code>(0, 0)</code>. A valid path in the grid is a path that starts from the upper left cell <code>(0, 0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code> following the signs on the grid. The valid path does not have to be the shortest.</p>
14+
15+
<p>You can modify the sign on a cell with <code>cost = 1</code>. You can modify the sign on a cell <strong>one time only</strong>.</p>
16+
17+
<p>Return <em>the minimum cost to make the grid have at least one valid path</em>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Example 1:</strong></p>
21+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid1.png" style="width: 400px; height: 390px;">
22+
<pre><strong>Input:</strong> grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
23+
<strong>Output:</strong> 3
24+
<strong>Explanation:</strong> You will start at point (0, 0).
25+
The path to (3, 3) is as follows. (0, 0) --&gt; (0, 1) --&gt; (0, 2) --&gt; (0, 3) change the arrow to down with cost = 1 --&gt; (1, 3) --&gt; (1, 2) --&gt; (1, 1) --&gt; (1, 0) change the arrow to down with cost = 1 --&gt; (2, 0) --&gt; (2, 1) --&gt; (2, 2) --&gt; (2, 3) change the arrow to down with cost = 1 --&gt; (3, 3)
26+
The total cost = 3.
27+
</pre>
28+
29+
<p><strong>Example 2:</strong></p>
30+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid2.png" style="width: 350px; height: 341px;">
31+
<pre><strong>Input:</strong> grid = [[1,1,3],[3,2,2],[1,1,4]]
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> You can follow the path from (0, 0) to (2, 2).
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/13/grid3.png" style="width: 200px; height: 192px;">
38+
<pre><strong>Input:</strong> grid = [[1,2],[4,3]]
39+
<strong>Output:</strong> 1
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>m == grid.length</code></li>
47+
<li><code>n == grid[i].length</code></li>
48+
<li><code>1 &lt;= m, n &lt;= 100</code></li>
49+
<li><code>1 &lt;= grid[i][j] &lt;= 4</code></li>
50+
</ul>
51+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h3 align="left"> 1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers</h3>
2+
<div><p>Given two arrays of integers <code>nums1</code> and <code>nums2</code>, return the number of triplets formed (type 1 and type 2) under the following rules:</p>
3+
4+
<ul>
5+
<li>Type 1: Triplet (i, j, k) if <code>nums1[i]<sup>2</sup> == nums2[j] * nums2[k]</code> where <code>0 &lt;= i &lt; nums1.length</code> and <code>0 &lt;= j &lt; k &lt; nums2.length</code>.</li>
6+
<li>Type 2: Triplet (i, j, k) if <code>nums2[i]<sup>2</sup> == nums1[j] * nums1[k]</code> where <code>0 &lt;= i &lt; nums2.length</code> and <code>0 &lt;= j &lt; k &lt; nums1.length</code>.</li>
7+
</ul>
8+
9+
<p>&nbsp;</p>
10+
<p><strong>Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> nums1 = [7,4], nums2 = [5,2,8,9]
13+
<strong>Output:</strong> 1
14+
<strong>Explanation:</strong> Type 1: (1, 1, 2), nums1[1]<sup>2</sup> = nums2[1] * nums2[2]. (4<sup>2</sup> = 2 * 8).
15+
</pre>
16+
17+
<p><strong>Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> nums1 = [1,1], nums2 = [1,1,1]
20+
<strong>Output:</strong> 9
21+
<strong>Explanation:</strong> All Triplets are valid, because 1<sup>2</sup> = 1 * 1.
22+
Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]<sup>2</sup> = nums2[j] * nums2[k].
23+
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]<sup>2</sup> = nums1[j] * nums1[k].
24+
</pre>
25+
26+
<p><strong>Example 3:</strong></p>
27+
28+
<pre><strong>Input:</strong> nums1 = [7,7,8,3], nums2 = [1,2,9,7]
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> There are 2 valid triplets.
31+
Type 1: (3,0,2). nums1[3]<sup>2</sup> = nums2[0] * nums2[2].
32+
Type 2: (3,0,1). nums2[3]<sup>2</sup> = nums1[0] * nums1[1].
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>
40+
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li>
41+
</ul>
42+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h3 align="left"> 1117. Building H2O</h3>
2+
<div><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.</p>
3+
4+
<p>There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given <code>releaseHydrogen</code> and <code>releaseOxygen</code> methods respectively, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must 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.</p>
5+
6+
<p>In other words:</p>
7+
8+
<ul>
9+
<li>If an oxygen thread arrives at the barrier when no hydrogen threads are present, it must wait for two hydrogen threads.</li>
10+
<li>If a hydrogen thread arrives at the barrier when no other threads are present, it must wait for an oxygen thread and another hydrogen thread.</li>
11+
</ul>
12+
13+
<p>We do not have to worry about matching the threads up explicitly; the threads do not necessarily know which other threads they are paired up with. The key is that threads pass the barriers in complete sets; thus, if we examine the sequence of threads that bind and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.</p>
14+
15+
<p>Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong>Example 1:</strong></p>
19+
20+
<pre><strong>Input:</strong> water = "HOH"
21+
<strong>Output:</strong> "HHO"
22+
<strong>Explanation:</strong> "HOH" and "OHH" are also valid answers.
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<pre><strong>Input:</strong> water = "OOHHHH"
28+
<strong>Output:</strong> "HHOHHO"
29+
<strong>Explanation:</strong> "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" and "OHHOHH" are also valid answers.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>3 * n == water.length</code></li>
37+
<li><code>1 &lt;= n &lt;= 20</code></li>
38+
<li><code>water[i]</code> is either <code>'H'</code> or <code>'O'</code>.</li>
39+
<li>There will be exactly <code>2 * n</code> <code>'H'</code> in <code>water</code>.</li>
40+
<li>There will be exactly <code>n</code> <code>'O'</code> in <code>water</code>.</li>
41+
</ul>
42+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<h3 align="left"> 1195. Fizz Buzz Multithreaded</h3>
2+
<div><p>You have the four functions:</p>
3+
4+
<ul>
5+
<li><code>printFizz</code> that prints the word <code>"fizz"</code> to the console,</li>
6+
<li><code>printBuzz</code> that prints the word <code>"buzz"</code> to the console,</li>
7+
<li><code>printFizzBuzz</code> that prints the word <code>"fizzbuzz"</code> to the console, and</li>
8+
<li><code>printNumber</code> that prints a given integer to the console.</li>
9+
</ul>
10+
11+
<p>You are given an instance of the class <code>FizzBuzz</code> that has four functions: <code>fizz</code>, <code>buzz</code>, <code>fizzbuzz</code> and <code>number</code>. The same instance of <code>FizzBuzz</code> will be passed to four different threads:</p>
12+
13+
<ul>
14+
<li><strong>Thread A:</strong> calls <code>fizz()</code> that should output the word <code>"fizz"</code>.</li>
15+
<li><strong>Thread B:</strong> calls <code>buzz()</code> that should output the word <code>"buzz"</code>.</li>
16+
<li><strong>Thread C:</strong> calls <code>fizzbuzz()</code> that should output the word <code>"fizzbuzz"</code>.</li>
17+
<li><strong>Thread D:</strong> calls <code>number()</code> that should only output the integers.</li>
18+
</ul>
19+
20+
<p>Modify the given class to output the series <code>[1, 2, "fizz", 4, "buzz", ...]</code> where the <code>i<sup>th</sup></code> token (<strong>1-indexed</strong>) of the series is:</p>
21+
22+
<ul>
23+
<li><code>"fizzbuzz"</code> if <code>i</code> is divisible by <code>3</code> and <code>5</code>,</li>
24+
<li><code>"fizz"</code> if <code>i</code> is divisible by <code>3</code> and not <code>5</code>,</li>
25+
<li><code>"buzz"</code> if <code>i</code> is divisible by <code>5</code> and not <code>3</code>, or</li>
26+
<li><code>i</code> if <code>i</code> is not divisible by <code>3</code> or <code>5</code>.</li>
27+
</ul>
28+
29+
<p>Implement the <code>FizzBuzz</code> class:</p>
30+
31+
<ul>
32+
<li><code>FizzBuzz(int n)</code> Initializes the object with the number <code>n</code> that represents the length of the sequence that should be printed.</li>
33+
<li><code>void fizz(printFizz)</code> Calls <code>printFizz</code> to output <code>"fizz"</code>.</li>
34+
<li><code>void buzz(printBuzz)</code> Calls <code>printBuzz</code> to output <code>"buzz"</code>.</li>
35+
<li><code>void fizzbuzz(printFizzBuzz)</code> Calls <code>printFizzBuzz</code> to output <code>"fizzbuzz"</code>.</li>
36+
<li><code>void number(printNumber)</code> Calls <code>printnumber</code> to output the numbers.</li>
37+
</ul>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Example 1:</strong></p>
41+
<pre><strong>Input:</strong> n = 15
42+
<strong>Output:</strong> [1,2,"fizz",4,"buzz","fizz",7,8,"fizz","buzz",11,"fizz",13,14,"fizzbuzz"]
43+
</pre><p><strong>Example 2:</strong></p>
44+
<pre><strong>Input:</strong> n = 5
45+
<strong>Output:</strong> [1,2,"fizz",4,"buzz"]
46+
</pre>
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= n &lt;= 50</code></li>
52+
</ul>
53+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<h3 align="left"> 1115. Print FooBar Alternately</h3>
2+
<div><p>Suppose you are given the following code:</p>
3+
4+
<pre>class FooBar {
5+
public void foo() {
6+
for (int i = 0; i &lt; n; i++) {
7+
print("foo");
8+
}
9+
}
10+
11+
public void bar() {
12+
for (int i = 0; i &lt; n; i++) {
13+
print("bar");
14+
}
15+
}
16+
}
17+
</pre>
18+
19+
<p>The same instance of <code>FooBar</code> will be passed to two different threads:</p>
20+
21+
<ul>
22+
<li>thread <code>A</code> will call <code>foo()</code>, while</li>
23+
<li>thread <code>B</code> will call <code>bar()</code>.</li>
24+
</ul>
25+
26+
<p>Modify the given program to output <code>"foobar"</code> <code>n</code> times.</p>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Example 1:</strong></p>
30+
31+
<pre><strong>Input:</strong> n = 1
32+
<strong>Output:</strong> "foobar"
33+
<strong>Explanation:</strong> There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
34+
"foobar" is being output 1 time.
35+
</pre>
36+
37+
<p><strong>Example 2:</strong></p>
38+
39+
<pre><strong>Input:</strong> n = 2
40+
<strong>Output:</strong> "foobarfoobar"
41+
<strong>Explanation:</strong> "foobar" is being output 2 times.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= n &lt;= 1000</code></li>
49+
</ul>
50+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<h3 align="left"> 1116. Print Zero Even Odd</h3>
2+
<div><p>You have a function <code>printNumber</code> that can be called with an integer parameter and prints it to the console.</p>
3+
4+
<ul>
5+
<li>For example, calling <code>printNumber(7)</code> prints <code>7</code> to the console.</li>
6+
</ul>
7+
8+
<p>You are given an instance of the class <code>ZeroEvenOdd</code> that has three functions: <code>zero</code>, <code>even</code>, and <code>odd</code>. The same instance of <code>ZeroEvenOdd</code> will be passed to three different threads:</p>
9+
10+
<ul>
11+
<li><strong>Thread A:</strong> calls <code>zero()</code> that should only output <code>0</code>'s.</li>
12+
<li><strong>Thread B:</strong> calls <code>even()</code> that should only output even numbers.</li>
13+
<li><strong>Thread C:</strong> calls <code>odd()</code> that should only output odd numbers.</li>
14+
</ul>
15+
16+
<p>Modify the given class to output the series <code>"010203040506..."</code> where the length of the series must be <code>2n</code>.</p>
17+
18+
<p>Implement the <code>ZeroEvenOdd</code> class:</p>
19+
20+
<ul>
21+
<li><code>ZeroEvenOdd(int n)</code> Initializes the object with the number <code>n</code> that represents the numbers that should be printed.</li>
22+
<li><code>void zero(printNumber)</code> Calls <code>printNumber</code> to output one zero.</li>
23+
<li><code>void even(printNumber)</code> Calls <code>printNumber</code> to output one even number.</li>
24+
<li><code>void odd(printNumber)</code> Calls <code>printNumber</code> to output one odd number.</li>
25+
</ul>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Example 1:</strong></p>
29+
30+
<pre><strong>Input:</strong> n = 2
31+
<strong>Output:</strong> "0102"
32+
<strong>Explanation:</strong> There are three threads being fired asynchronously.
33+
One of them calls zero(), the other calls even(), and the last one calls odd().
34+
"0102" is the correct output.
35+
</pre>
36+
37+
<p><strong>Example 2:</strong></p>
38+
39+
<pre><strong>Input:</strong> n = 5
40+
<strong>Output:</strong> "0102030405"
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= n &lt;= 1000</code></li>
48+
</ul>
49+
</div>

0 commit comments

Comments
 (0)