mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 19:01:47 +08:00
add leetcode problem-cn part2
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<p>Given an array filled with letters and numbers, find the longest subarray with an equal number of letters and numbers.</p>
|
||||
|
||||
<p>Return the subarray. If there are more than one answer, return the one which has the smallest index of its left endpoint. If there is no answer, return an empty arrary.</p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>["A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"]
|
||||
|
||||
<strong>Output: </strong>["A","1","B","C","D","2","3","4","E","5","F","G","6","7"]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>["A","A"]
|
||||
|
||||
<strong>Output: </strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>array.length <= 100000</code></li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,14 @@
|
||||
<p>Write a method to count the number of 2s that appear in all the numbers between 0 and n (inclusive).</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>25
|
||||
<strong>Output: </strong>9
|
||||
<strong>Explanation: </strong>(2, 12, 20, 21, 22, 23, 24, 25)(Note that 22 counts for two 2s.)</pre>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>n <= 10^9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,20 @@
|
||||
<p>The data structure <code>TreeNode</code> is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to convert a binary search tree (implemented with <code>TreeNode</code>) into a single linked list. The values should be kept in order and the operation should be performed in place (that is, on the original data structure).</p>
|
||||
|
||||
<p>Return the head node of the linked list after converting.</p>
|
||||
|
||||
<p><b>Note: </b>This problem is slightly different from the original one in the book.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong> [4,2,5,1,3,null,6,0]
|
||||
<strong>Output: </strong> [0,null,1,null,2,null,3,null,4,null,5,null,6]
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes will not exceed 100000.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>A cell <code>(r, c)</code> of an excel sheet is represented as a string <code>"<col><row>"</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code><col></code> denotes the column number <code>c</code> of the cell. It is represented by <strong>alphabetical letters</strong>.
|
||||
|
||||
<ul>
|
||||
<li>For example, the <code>1<sup>st</sup></code> column is denoted by <code>'A'</code>, the <code>2<sup>nd</sup></code> by <code>'B'</code>, the <code>3<sup>rd</sup></code> by <code>'C'</code>, and so on.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code><row></code> is the row number <code>r</code> of the cell. The <code>r<sup>th</sup></code> row is represented by the <strong>integer</strong> <code>r</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given a string <code>s</code> in the format <code>"<col1><row1>:<col2><row2>"</code>, where <code><col1></code> represents the column <code>c1</code>, <code><row1></code> represents the row <code>r1</code>, <code><col2></code> represents the column <code>c2</code>, and <code><row2></code> represents the row <code>r2</code>, such that <code>r1 <= r2</code> and <code>c1 <= c2</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>list of cells</strong></em> <code>(x, y)</code> <em>such that</em> <code>r1 <= x <= r2</code> <em>and</em> <code>c1 <= y <= c2</code>. The cells should be represented as <strong>strings</strong> in the format mentioned above and be sorted in <strong>non-decreasing</strong> order first by columns and then by rows.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" style="width: 250px; height: 160px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "K1:L2"
|
||||
<strong>Output:</strong> ["K1","K2","L1","L2"]
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram shows the cells which should be present in the list.
|
||||
The red arrows denote the order in which the cells should be presented.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" style="width: 500px; height: 50px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "A1:F1"
|
||||
<strong>Output:</strong> ["A1","B1","C1","D1","E1","F1"]
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram shows the cells which should be present in the list.
|
||||
The red arrow denotes the order in which the cells should be presented.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>s.length == 5</code></li>
|
||||
<li><code>'A' <= s[0] <= s[3] <= 'Z'</code></li>
|
||||
<li><code>'1' <= s[1] <= s[4] <= '9'</code></li>
|
||||
<li><code>s</code> consists of uppercase English letters, digits and <code>':'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are currently designing a dynamic array. You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is the number of elements that will be in the array at time <code>i</code>. In addition, you are given an integer <code>k</code>, the <strong>maximum</strong> number of times you can <strong>resize</strong> the array (to<strong> any</strong> size).</p>
|
||||
|
||||
<p>The size of the array at time <code>t</code>, <code>size<sub>t</sub></code>, must be at least <code>nums[t]</code> because there needs to be enough space in the array to hold all the elements. The <strong>space wasted</strong> at time <code>t</code> is defined as <code>size<sub>t</sub> - nums[t]</code>, and the <strong>total</strong> space wasted is the <strong>sum</strong> of the space wasted across every time <code>t</code> where <code>0 <= t < nums.length</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> <strong>total space wasted</strong> if you can resize the array at most</em> <code>k</code> <em>times</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong> The array can have <strong>any size</strong> at the start and does<strong> not </strong>count towards the number of resizing operations.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,20], k = 0
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> size = [20,20].
|
||||
We can set the initial size to be 20.
|
||||
The total wasted space is (20 - 10) + (20 - 20) = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,20,30], k = 1
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> size = [20,20,30].
|
||||
We can set the initial size to be 20 and resize to 30 at time 2.
|
||||
The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,20,15,30,20], k = 2
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> size = [10,20,20,30,30].
|
||||
We can set the initial size to 10, resize to 20 at time 1, and resize to 30 at time 3.
|
||||
The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 - 20) = 15.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= k <= nums.length - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,22 @@
|
||||
<p>Design and build a "least recently used" cache, which evicts the least recently used item. The cache should map from keys to values (allowing you to insert and retrieve a value associ­ated with a particular key) and be initialized with a max size. When it is full, it should evict the least recently used item.</p>
|
||||
|
||||
<p>You should implement following operations: <code>get</code> and <code>put</code>.</p>
|
||||
|
||||
<p>Get a value by key: <code>get(key)</code> - If key is in the cache, return the value, otherwise return -1.<br />
|
||||
Write a key-value pair to the cache: <code>put(key, value)</code> - If the key is not in the cache, then write its value to the cache. Evict the least recently used item before writing if necessary.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
LRUCache cache = new LRUCache( 2 /* capacity */ );
|
||||
|
||||
cache.put(1, 1);
|
||||
cache.put(2, 2);
|
||||
cache.get(1); // returns 1
|
||||
cache.put(3, 3); // evicts key 2
|
||||
cache.get(2); // returns -1 (not found)
|
||||
cache.put(4, 4); // evicts key 1
|
||||
cache.get(1); // returns -1 (not found)
|
||||
cache.get(3); // returns 3
|
||||
cache.get(4); // returns 4
|
||||
</pre>
|
25
算法题(国内版)/problem (English)/T9键盘(English) [t9-lcci].html
Normal file
25
算法题(国内版)/problem (English)/T9键盘(English) [t9-lcci].html
Normal file
@@ -0,0 +1,25 @@
|
||||
<p>On old cell phones, users typed on a numeric keypad and the phone would provide a list of words that matched these numbers. Each digit mapped to a set of 0 - 4 letters. Implement an algo­rithm to return a list of matching words, given a sequence of digits. You are provided a list of valid words. The mapping is shown in the diagram below:</p>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/17_telephone_keypad.png" style="width: 200px;" /></p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "8733", words = ["tree", "used"]
|
||||
<strong>Output:</strong> ["tree", "used"]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "2", words = ["a", "b", "c", "d"]
|
||||
<strong>Output:</strong> ["a", "b", "c"]</pre>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num.length <= 1000</code></li>
|
||||
<li><code>words.length <= 500</code></li>
|
||||
<li><code>words[i].length == num.length</code></li>
|
||||
<li><code>There are no number 0 and 1 in num</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,23 @@
|
||||
<p>Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end to hold the additional characters,and that you are given the "true" length of the string. (Note: If implementing in Java,please use a character array so that you can perform this operation in place.)</p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>"Mr John Smith ", 13
|
||||
<strong>Output: </strong>"Mr%20John%20Smith"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>" ", 5
|
||||
<strong>Output: </strong>"%20%20%20%20%20"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>0 <= S.length <= 500000</code></li>
|
||||
</ol>
|
@@ -0,0 +1,60 @@
|
||||
<p>A <strong>k-mirror number</strong> is a <strong>positive</strong> integer <strong>without leading zeros</strong> that reads the same both forward and backward in base-10 <strong>as well as</strong> in base-k.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>9</code> is a 2-mirror number. The representation of <code>9</code> in base-10 and base-2 are <code>9</code> and <code>1001</code> respectively, which read the same both forward and backward.</li>
|
||||
<li>On the contrary, <code>4</code> is not a 2-mirror number. The representation of <code>4</code> in base-2 is <code>100</code>, which does not read the same both forward and backward.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given the base <code>k</code> and the number <code>n</code>, return <em>the <strong>sum</strong> of the</em> <code>n</code> <em><strong>smallest</strong> k-mirror numbers</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 2, n = 5
|
||||
<strong>Output:</strong> 25
|
||||
<strong>Explanation:
|
||||
</strong>The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
|
||||
base-10 base-2
|
||||
1 1
|
||||
3 11
|
||||
5 101
|
||||
7 111
|
||||
9 1001
|
||||
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 3, n = 7
|
||||
<strong>Output:</strong> 499
|
||||
<strong>Explanation:
|
||||
</strong>The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
|
||||
base-10 base-3
|
||||
1 1
|
||||
2 2
|
||||
4 11
|
||||
8 22
|
||||
121 11111
|
||||
151 12121
|
||||
212 21212
|
||||
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 7, n = 17
|
||||
<strong>Output:</strong> 20379000
|
||||
<strong>Explanation:</strong> The 17 smallest 7-mirror numbers are:
|
||||
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= k <= 9</code></li>
|
||||
<li><code>1 <= n <= 30</code></li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,62 @@
|
||||
<p>You are given two positive integers <code>left</code> and <code>right</code> with <code>left <= right</code>. Calculate the <strong>product</strong> of all integers in the <strong>inclusive</strong> range <code>[left, right]</code>.</p>
|
||||
|
||||
<p>Since the product may be very large, you will <strong>abbreviate</strong> it following these steps:</p>
|
||||
|
||||
<ol>
|
||||
<li>Count all <strong>trailing</strong> zeros in the product and <strong>remove</strong> them. Let us denote this count as <code>C</code>.
|
||||
|
||||
<ul>
|
||||
<li>For example, there are <code>3</code> trailing zeros in <code>1000</code>, and there are <code>0</code> trailing zeros in <code>546</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Denote the remaining number of digits in the product as <code>d</code>. If <code>d > 10</code>, then express the product as <code><pre>...<suf></code> where <code><pre></code> denotes the <strong>first</strong> <code>5</code> digits of the product, and <code><suf></code> denotes the <strong>last</strong> <code>5</code> digits of the product <strong>after</strong> removing all trailing zeros. If <code>d <= 10</code>, we keep it unchanged.
|
||||
<ul>
|
||||
<li>For example, we express <code>1234567654321</code> as <code>12345...54321</code>, but <code>1234567</code> is represented as <code>1234567</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Finally, represent the product as a <strong>string</strong> <code>"<pre>...<suf>eC"</code>.
|
||||
<ul>
|
||||
<li>For example, <code>12345678987600000</code> will be represented as <code>"12345...89876e5"</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>a string denoting the <strong>abbreviated product</strong> of all integers in the <strong>inclusive</strong> range</em> <code>[left, right]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 1, right = 4
|
||||
<strong>Output:</strong> "24e0"
|
||||
<strong>Explanation:</strong> The product is 1 × 2 × 3 × 4 = 24.
|
||||
There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
|
||||
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
|
||||
Thus, the final representation is "24e0".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 2, right = 11
|
||||
<strong>Output:</strong> "399168e2"
|
||||
<strong>Explanation:</strong> The product is 39916800.
|
||||
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
|
||||
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
|
||||
Hence, the abbreviated product is "399168e2".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> left = 371, right = 375
|
||||
<strong>Output:</strong> "7219856259e3"
|
||||
<strong>Explanation:</strong> The product is 7219856259000.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,21 @@
|
||||
<p>There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
first = "pale"
|
||||
second = "ple"
|
||||
<strong>Output:</strong> True
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
first = "pales"
|
||||
second = "pal"
|
||||
<strong>Output:</strong> False
|
||||
</pre>
|
@@ -0,0 +1,26 @@
|
||||
<p>Describe how you could use a single array to implement three stacks.</p>
|
||||
|
||||
<p>You should implement <code>push(stackNum, value)</code>、<code>pop(stackNum)</code>、<code>isEmpty(stackNum)</code>、<code>peek(stackNum)</code> methods. <code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS"> </font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack. </font><code>value</code> is the value that pushed to the stack.</p>
|
||||
|
||||
<p>The constructor requires a <code>stackSize</code> parameter, which represents the size of each stack.</p>
|
||||
|
||||
<p><strong>Example1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>:
|
||||
["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
|
||||
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
|
||||
<strong> Output</strong>:
|
||||
[null, null, null, 1, -1, -1, true]
|
||||
<b>Explanation</b>: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>:
|
||||
["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
|
||||
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
|
||||
<strong> Output</strong>:
|
||||
[null, null, null, null, 2, 1, -1, -1]
|
||||
</pre>
|
@@ -0,0 +1,21 @@
|
||||
<p>A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs. The result may be large, so return it modulo 1000000007.</p>
|
||||
|
||||
<p><strong>Example1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: n = 3
|
||||
<strong> Output</strong>: 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: n = 5
|
||||
<strong> Output</strong>: 13
|
||||
</pre>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= n <= 1000000</code></li>
|
||||
</ol>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,27 @@
|
||||
<p>Given an integer <code>n</code>, return <code>true</code><em> if </em><code>n</code><em> has <strong>exactly three positive divisors</strong>. Otherwise, return </em><code>false</code>.</p>
|
||||
|
||||
<p>An integer <code>m</code> is a <strong>divisor</strong> of <code>n</code> if there exists an integer <code>k</code> such that <code>n = k * m</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explantion:</strong> 2 has only two divisors: 1 and 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explantion:</strong> 4 has three divisors: 1, 2, and 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,22 @@
|
||||
<p>Given a positive integer, print the next smallest and the next largest number that have the same number of 1 bits in their binary representation.</p>
|
||||
|
||||
<p><strong>Example1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: num = 2 (0b10)
|
||||
<strong> Output</strong>: [4, 1] ([0b100, 0b1])
|
||||
</pre>
|
||||
|
||||
<p><strong>Example2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: num = 1
|
||||
<strong> Output</strong>: [2, -1]
|
||||
</pre>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= num <= 2147483647</code></li>
|
||||
<li>If there is no next smallest or next largest number, output -1.</li>
|
||||
</ol>
|
@@ -0,0 +1,47 @@
|
||||
<p>An integer <code>x</code> is <strong>numerically balanced</strong> if for every digit <code>d</code> in the number <code>x</code>, there are <strong>exactly</strong> <code>d</code> occurrences of that digit in <code>x</code>.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <em>the <strong>smallest numerically balanced</strong> number <strong>strictly greater</strong> than </em><code>n</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong>
|
||||
22 is numerically balanced since:
|
||||
- The digit 2 occurs 2 times.
|
||||
It is also the smallest numerically balanced number strictly greater than 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1000
|
||||
<strong>Output:</strong> 1333
|
||||
<strong>Explanation:</strong>
|
||||
1333 is numerically balanced since:
|
||||
- The digit 1 occurs 1 time.
|
||||
- The digit 3 occurs 3 times.
|
||||
It is also the smallest numerically balanced number strictly greater than 1000.
|
||||
Note that 1022 cannot be the answer because 0 appeared more than 0 times.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3000
|
||||
<strong>Output:</strong> 3133
|
||||
<strong>Explanation:</strong>
|
||||
3133 is numerically balanced since:
|
||||
- The digit 1 occurs 1 time.
|
||||
- The digit 3 occurs 3 times.
|
||||
It is also the smallest numerically balanced number strictly greater than 3000.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a binary string <code>binary</code>. A <strong>subsequence</strong> of <code>binary</code> is considered <strong>good</strong> if it is <strong>not empty</strong> and has <strong>no leading zeros</strong> (with the exception of <code>"0"</code>).</p>
|
||||
|
||||
<p>Find the number of <strong>unique good subsequences</strong> of <code>binary</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>binary = "001"</code>, then all the <strong>good</strong> subsequences are <code>["0", "0", "1"]</code>, so the <strong>unique</strong> good subsequences are <code>"0"</code> and <code>"1"</code>. Note that subsequences <code>"00"</code>, <code>"01"</code>, and <code>"001"</code> are not good because they have leading zeros.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>unique good subsequences</strong> of </em><code>binary</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> binary = "001"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The good subsequences of binary are ["0", "0", "1"].
|
||||
The unique good subsequences are "0" and "1".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> binary = "11"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The good subsequences of binary are ["1", "1", "11"].
|
||||
The unique good subsequences are "1" and "11".</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> binary = "101"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The good subsequences of binary are ["1", "0", "1", "10", "11", "101"].
|
||||
The unique good subsequences are "0", "1", "10", "11", and "101".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= binary.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>binary</code> consists of only <code>'0'</code>s and <code>'1'</code>s.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,16 @@
|
||||
<p>Write a function that adds two numbers. You should not use + or any arithmetic operators.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 1, b = 1
|
||||
<strong>Output:</strong> 2</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>a</code> and <code>b</code> may be 0 or negative.</li>
|
||||
<li>The result fits in 32-bit integer.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,74 @@
|
||||
<p>Table: <code>Employees</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
+-------------+---------+
|
||||
employee_id is the primary key for this table.
|
||||
Each row of this table indicates the name of the employee whose ID is employee_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Table: <code>Salaries</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| employee_id | int |
|
||||
| salary | int |
|
||||
+-------------+---------+
|
||||
employee_id is the primary key for this table.
|
||||
Each row of this table indicates the salary of the employee whose ID is employee_id.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write an SQL query to report the IDs of all the employees with <strong>missing information</strong>. The information of an employee is missing if:</p>
|
||||
|
||||
<ul>
|
||||
<li>The employee's <strong>name</strong> is missing, or</li>
|
||||
<li>The employee's <strong>salary</strong> is missing.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the result table ordered by <code>employee_id</code> <strong>in ascending order</strong>.</p>
|
||||
|
||||
<p>The query result format is in the following example.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
Employees table:
|
||||
+-------------+----------+
|
||||
| employee_id | name |
|
||||
+-------------+----------+
|
||||
| 2 | Crew |
|
||||
| 4 | Haven |
|
||||
| 5 | Kristian |
|
||||
+-------------+----------+
|
||||
Salaries table:
|
||||
+-------------+--------+
|
||||
| employee_id | salary |
|
||||
+-------------+--------+
|
||||
| 5 | 76071 |
|
||||
| 1 | 22517 |
|
||||
| 4 | 63539 |
|
||||
+-------------+--------+
|
||||
<strong>Output:</strong>
|
||||
+-------------+
|
||||
| employee_id |
|
||||
+-------------+
|
||||
| 1 |
|
||||
| 2 |
|
||||
+-------------+
|
||||
<strong>Explanation:</strong>
|
||||
Employees 1, 2, 4, and 5 are working at this company.
|
||||
The name of employee 1 is missing.
|
||||
The salary of employee 2 is missing.
|
||||
</pre>
|
@@ -0,0 +1,41 @@
|
||||
<p>Given a string <code>s</code>, find two <strong>disjoint palindromic subsequences</strong> of <code>s</code> such that the <strong>product</strong> of their lengths is <strong>maximized</strong>. The two subsequences are <strong>disjoint</strong> if they do not both pick a character at the same index.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible <strong>product</strong> of the lengths of the two palindromic subsequences</em>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/08/24/two-palindromic-subsequences.png" style="width: 550px; height: 124px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leetcodecom"
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation</strong>: An optimal solution is to choose "ete" for the 1<sup>st</sup> subsequence and "cdc" for the 2<sup>nd</sup> subsequence.
|
||||
The product of their lengths is: 3 * 3 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bb"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation</strong>: An optimal solution is to choose "b" (the first character) for the 1<sup>st</sup> subsequence and "b" (the second character) for the 2<sup>nd</sup> subsequence.
|
||||
The product of their lengths is: 1 * 1 = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "accbcaxxcxx"
|
||||
<strong>Output:</strong> 25
|
||||
<strong>Explanation</strong>: An optimal solution is to choose "accca" for the 1<sup>st</sup> subsequence and "xxcxx" for the 2<sup>nd</sup> subsequence.
|
||||
The product of their lengths is: 5 * 5 = 25.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 12</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters only.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array of <code>events</code> where <code>events[i] = [startTime<sub>i</sub>, endTime<sub>i</sub>, value<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> event starts at <code>startTime<sub>i</sub></code><sub> </sub>and ends at <code>endTime<sub>i</sub></code>, and if you attend this event, you will receive a value of <code>value<sub>i</sub></code>. You can choose <strong>at most</strong> <strong>two</strong> <strong>non-overlapping</strong> events to attend such that the sum of their values is <strong>maximized</strong>.</p>
|
||||
|
||||
<p>Return <em>this <strong>maximum</strong> sum.</em></p>
|
||||
|
||||
<p>Note that the start time and end time is <strong>inclusive</strong>: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time <code>t</code>, the next event must start at or after <code>t + 1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/picture5.png" style="width: 400px; height: 75px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> events = [[1,3,2],[4,5,2],[2,4,3]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation: </strong>Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="Example 1 Diagram" src="https://assets.leetcode.com/uploads/2021/09/21/picture1.png" style="width: 400px; height: 77px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> events = [[1,3,2],[4,5,2],[1,5,5]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation: </strong>Choose event 2 for a sum of 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/09/21/picture3.png" style="width: 400px; height: 66px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> events = [[1,5,3],[1,5,1],[6,6,5]]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation: </strong>Choose events 0 and 2 for a sum of 3 + 5 = 8.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= events.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>events[i].length == 3</code></li>
|
||||
<li><code>1 <= startTime<sub>i</sub> <= endTime<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= value<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
Given two <strong>sorted 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> as well as an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> (<strong>1-based</strong>) smallest product of </em><code>nums1[i] * nums2[j]</code><em> where </em><code>0 <= i < nums1.length</code><em> and </em><code>0 <= j < nums2.length</code>.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,5], nums2 = [3,4], k = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> The 2 smallest products are:
|
||||
- nums1[0] * nums2[0] = 2 * 3 = 6
|
||||
- nums1[0] * nums2[1] = 2 * 4 = 8
|
||||
The 2<sup>nd</sup> smallest product is 8.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [-4,-2,0,3], nums2 = [2,4], k = 6
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The 6 smallest products are:
|
||||
- nums1[0] * nums2[1] = (-4) * 4 = -16
|
||||
- nums1[0] * nums2[0] = (-4) * 2 = -8
|
||||
- nums1[1] * nums2[1] = (-2) * 4 = -8
|
||||
- nums1[1] * nums2[0] = (-2) * 2 = -4
|
||||
- nums1[2] * nums2[0] = 0 * 2 = 0
|
||||
- nums1[2] * nums2[1] = 0 * 4 = 0
|
||||
The 6<sup>th</sup> smallest product is 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [-2,-1,0,1,2], nums2 = [-3,-1,2,4,5], k = 3
|
||||
<strong>Output:</strong> -6
|
||||
<strong>Explanation:</strong> The 3 smallest products are:
|
||||
- nums1[0] * nums2[4] = (-2) * 5 = -10
|
||||
- nums1[0] * nums2[3] = (-2) * 4 = -8
|
||||
- nums1[4] * nums2[0] = 2 * (-3) = -6
|
||||
The 3<sup>rd</sup> smallest product is -6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums1[i], nums2[j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= nums1.length * nums2.length</code></li>
|
||||
<li><code>nums1</code> and <code>nums2</code> are sorted.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,46 @@
|
||||
<p>There are <code>n</code> houses evenly lined up on the street, and each house is beautifully painted. You are given a <strong>0-indexed</strong> integer array <code>colors</code> of length <code>n</code>, where <code>colors[i]</code> represents the color of the <code>i<sup>th</sup></code> house.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> distance between <strong>two</strong> houses with <strong>different</strong> colors</em>.</p>
|
||||
|
||||
<p>The distance between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> houses is <code>abs(i - j)</code>, where <code>abs(x)</code> is the <strong>absolute value</strong> of <code>x</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg1.png" style="width: 610px; height: 84px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = [<u><strong>1</strong></u>,1,1,<strong><u>6</u></strong>,1,1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In the above image, color 1 is blue, and color 6 is red.
|
||||
The furthest two houses with different colors are house 0 and house 3.
|
||||
House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3.
|
||||
Note that houses 3 and 6 can also produce the optimal answer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/31/eg2.png" style="width: 426px; height: 84px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = [<u><strong>1</strong></u>,8,3,8,<u><strong>3</strong></u>]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
|
||||
The furthest two houses with different colors are house 0 and house 4.
|
||||
House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = [<u><strong>0</strong></u>,<strong><u>1</u></strong>]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The furthest two houses with different colors are house 0 and house 1.
|
||||
House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == colors.length</code></li>
|
||||
<li><code>2 <= n <= 100</code></li>
|
||||
<li><code>0 <= colors[i] <= 100</code></li>
|
||||
<li>Test data are generated such that <strong>at least</strong> two houses have different colors.</li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>A majority element is an element that makes up more than half of the items in an array. Given a integers array, find the majority element. If there is no majority element, return -1. Do this in O(N) time and O(1) space.</p>
|
||||
|
||||
<p><strong>Example 1: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>[1,2,5,9,5,9,5,5,5]
|
||||
<strong>Output: </strong>5</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 2: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>[3,2]
|
||||
<strong>Output: </strong>-1</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>Example 3: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>[2,2,1,1,1,2,2]
|
||||
<strong>Output: </strong>2
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,40 @@
|
||||
<p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
|
||||
|
||||
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
|
||||
|
||||
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tickets = [2,3,2], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
|
||||
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
|
||||
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
|
||||
- In the next 4 passes, only the person in position 0 is buying tickets.
|
||||
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == tickets.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= tickets[i] <= 100</code></li>
|
||||
<li><code>0 <= k < n</code></li>
|
||||
</ul>
|
1
算法题(国内版)/problem (English)/二分图(English) [vEAB3K].html
Normal file
1
算法题(国内版)/problem (English)/二分图(English) [vEAB3K].html
Normal file
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,19 @@
|
||||
<p>A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.</p>
|
||||
|
||||
<p><strong>Example:</strong><br />
|
||||
Given the following tree:</p>
|
||||
|
||||
<pre>
|
||||
2
|
||||
/ \
|
||||
1 3
|
||||
</pre>
|
||||
|
||||
<p>Output:</p>
|
||||
|
||||
<pre>
|
||||
[
|
||||
[2,1,3],
|
||||
[2,3,1]
|
||||
]
|
||||
</pre>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
1
算法题(国内版)/problem (English)/二叉树剪枝(English) [pOCWxh].html
Normal file
1
算法题(国内版)/problem (English)/二叉树剪枝(English) [pOCWxh].html
Normal file
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
1
算法题(国内版)/problem (English)/二进制加法(English) [JFETK5].html
Normal file
1
算法题(国内版)/problem (English)/二进制加法(English) [JFETK5].html
Normal file
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,22 @@
|
||||
<p>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".</p>
|
||||
|
||||
<p><strong>Example1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: 0.625
|
||||
<strong> Output</strong>: "0.101"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: 0.1
|
||||
<strong> Output</strong>: "ERROR"
|
||||
<strong> Note</strong>: 0.1 cannot be represented accurately in binary.
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ol>
|
||||
<li>This two charaters "0." should be counted into 32 characters.</li>
|
||||
</ol>
|
@@ -0,0 +1,44 @@
|
||||
<p>Design an algorithm to figure out if someone has won a game of tic-tac-toe. Input is a string array of size N x N, including characters " ", "X" and "O", where " " represents a empty grid.</p>
|
||||
|
||||
<p>The rules of tic-tac-toe are as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Players place characters into an empty grid(" ") in turn.</li>
|
||||
<li>The first player always place character "O", and the second one place "X".</li>
|
||||
<li>Players are only allowed to place characters in empty grid. Replacing a character is not allowed.</li>
|
||||
<li>If there is any row, column or diagonal filled with N same characters, the game ends. The player who place the last charater wins.</li>
|
||||
<li>When there is no empty grid, the game ends.</li>
|
||||
<li>If the game ends, players cannot place any character further.</li>
|
||||
</ul>
|
||||
|
||||
<p>If there is any winner, return the character that the winner used. If there's a draw, return "Draw". If the game doesn't end and there is no winner, return "Pending".</p>
|
||||
|
||||
<p><strong>Example 1: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong> board = ["O X"," XO","X O"]
|
||||
<strong>Output: </strong> "X"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong> board = ["OOX","XXO","OXO"]
|
||||
<strong>Output: </strong> "Draw"
|
||||
<strong>Explanation: </strong> no player wins and no empty grid left
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong> board = ["OOX","XXO","OX "]
|
||||
<strong>Output: </strong> "Pending"
|
||||
<strong>Explanation: </strong> no player wins but there is still a empty grid
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= board.length == board[i].length <= 100</code></li>
|
||||
<li>Input follows the rules.</li>
|
||||
</ul>
|
22
算法题(国内版)/problem (English)/交换和(English) [sum-swap-lcci].html
Normal file
22
算法题(国内版)/problem (English)/交换和(English) [sum-swap-lcci].html
Normal file
@@ -0,0 +1,22 @@
|
||||
<p>Given two arrays of integers, find a pair of values (one value from each array) that you can swap to give the two arrays the same sum.</p>
|
||||
|
||||
<p>Return an array, where the first element is the element in the first array that will be swapped, and the second element is another one in the second array. If there are more than one answers, return any one of them. If there is no answer, return an empty array.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> array1 = [4, 1, 2, 1, 1, 2], array2 = [3, 6, 3, 3]
|
||||
<strong>Output:</strong> [1, 3]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> array1 = <code>[1, 2, 3], array2 = [4, 5, 6]</code>
|
||||
<strong>Output: </strong>[]</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= array1.length, array2.length <= 100000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,15 @@
|
||||
<p>Write a function to swap a number in place (that is, without temporary variables).</p>
|
||||
|
||||
<p><strong>Example: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numbers = [1,2]
|
||||
<strong>Output:</strong> [2,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>numbers.length == 2</code></li>
|
||||
<li><code>-2147483647 <= numbers[i] <= 2147483647</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any. If there's no intersection, return an empty array.</p>
|
||||
The absolute error should not exceed 10^-6. If there are more than one intersections, return the one with smallest X axis value. If there are more than one intersections that have same X axis value, return the one with smallest Y axis value.
|
||||
|
||||
<p><strong>Example 1: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>
|
||||
line1 = {0, 0}, {1, 0}
|
||||
line2 = {1, 1}, {0, -1}
|
||||
<strong>Output: </strong> {0.5, 0}
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>
|
||||
line1 = {0, 0}, {3, 3}
|
||||
line2 = {1, 1}, {2, 2}
|
||||
<strong>Output: </strong> {1, 1}
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3: </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input: </strong>
|
||||
line1 = {0, 0}, {1, 1}
|
||||
line2 = {1, 0}, {2, 1}
|
||||
<strong>Output: </strong> {} (no intersection)
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The absolute value of coordinate value will not exceed 2^7.</li>
|
||||
<li>All coordinates are valid 2D coordinates.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1 @@
|
||||
English description is not available for the problem. Please switch to Chinese.
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is uniquely assigned a value from <code>1</code> to <code>n</code>. You are also given an integer <code>startValue</code> representing the value of the start node <code>s</code>, and a different integer <code>destValue</code> representing the value of the destination node <code>t</code>.</p>
|
||||
|
||||
<p>Find the <strong>shortest path</strong> starting from node <code>s</code> and ending at node <code>t</code>. Generate step-by-step directions of such path as a string consisting of only the <strong>uppercase</strong> letters <code>'L'</code>, <code>'R'</code>, and <code>'U'</code>. Each letter indicates a specific direction:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'L'</code> means to go from a node to its <strong>left child</strong> node.</li>
|
||||
<li><code>'R'</code> means to go from a node to its <strong>right child</strong> node.</li>
|
||||
<li><code>'U'</code> means to go from a node to its <strong>parent</strong> node.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the step-by-step directions of the <strong>shortest path</strong> from node </em><code>s</code><em> to node</em> <code>t</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/15/eg1.png" style="width: 214px; height: 163px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
|
||||
<strong>Output:</strong> "UURL"
|
||||
<strong>Explanation:</strong> The shortest path is: 3 → 1 → 5 → 2 → 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/15/eg2.png" style="width: 74px; height: 102px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,1], startValue = 2, destValue = 1
|
||||
<strong>Output:</strong> "L"
|
||||
<strong>Explanation:</strong> The shortest path is: 2 → 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= n</code></li>
|
||||
<li>All the values in the tree are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= startValue, destValue <= n</code></li>
|
||||
<li><code>startValue != destValue</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>An integer array <code>original</code> is transformed into a <strong>doubled</strong> array <code>changed</code> by appending <strong>twice the value</strong> of every element in <code>original</code>, and then randomly <strong>shuffling</strong> the resulting array.</p>
|
||||
|
||||
<p>Given an array <code>changed</code>, return <code>original</code><em> if </em><code>changed</code><em> is a <strong>doubled</strong> array. If </em><code>changed</code><em> is not a <strong>doubled</strong> array, return an empty array. The elements in</em> <code>original</code> <em>may be returned in <strong>any</strong> order</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> changed = [1,3,4,2,6,8]
|
||||
<strong>Output:</strong> [1,3,4]
|
||||
<strong>Explanation:</strong> One possible original array could be [1,3,4]:
|
||||
- Twice the value of 1 is 1 * 2 = 2.
|
||||
- Twice the value of 3 is 3 * 2 = 6.
|
||||
- Twice the value of 4 is 4 * 2 = 8.
|
||||
Other original arrays could be [4,3,1] or [3,1,4].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> changed = [6,3,0,1]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> changed is not a doubled array.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> changed = [1]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> changed is not a doubled array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= changed.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= changed[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer <code>n</code> representing the length of an unknown array that you are trying to recover. You are also given an array <code>sums</code> containing the values of all <code>2<sup>n</sup></code> <strong>subset sums</strong> of the unknown array (in no particular order).</p>
|
||||
|
||||
<p>Return <em>the array </em><code>ans</code><em> of length </em><code>n</code><em> representing the unknown array. If <strong>multiple</strong> answers exist, return <strong>any</strong> of them</em>.</p>
|
||||
|
||||
<p>An array <code>sub</code> is a <strong>subset</strong> of an array <code>arr</code> if <code>sub</code> can be obtained from <code>arr</code> by deleting some (possibly zero or all) elements of <code>arr</code>. The sum of the elements in <code>sub</code> is one possible <strong>subset sum</strong> of <code>arr</code>. The sum of an empty array is considered to be <code>0</code>.</p>
|
||||
|
||||
<p><strong>Note:</strong> Test cases are generated such that there will <strong>always</strong> be at least one correct answer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, sums = [-3,-2,-1,0,0,1,2,3]
|
||||
<strong>Output:</strong> [1,2,-3]
|
||||
<strong>Explanation: </strong>[1,2,-3] is able to achieve the given subset sums:
|
||||
- []: sum is 0
|
||||
- [1]: sum is 1
|
||||
- [2]: sum is 2
|
||||
- [1,2]: sum is 3
|
||||
- [-3]: sum is -3
|
||||
- [1,-3]: sum is -2
|
||||
- [2,-3]: sum is -1
|
||||
- [1,2,-3]: sum is 0
|
||||
Note that any permutation of [1,2,-3] and also any permutation of [-1,-2,3] will also be accepted.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, sums = [0,0,0,0]
|
||||
<strong>Output:</strong> [0,0]
|
||||
<strong>Explanation:</strong> The only correct answer is [0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, sums = [0,0,5,5,4,-1,4,9,9,-1,4,3,4,8,3,8]
|
||||
<strong>Output:</strong> [0,-1,4,5]
|
||||
<strong>Explanation:</strong> [0,-1,4,5] is able to achieve the given subset sums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 15</code></li>
|
||||
<li><code>sums.length == 2<sup>n</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= sums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <b>0-index</b><strong>ed</strong> string <code>street</code>. Each character in <code>street</code> is either <code>'H'</code> representing a house or <code>'.'</code> representing an empty space.</p>
|
||||
|
||||
<p>You can place buckets on the <strong>empty spaces</strong> to collect rainwater that falls from the adjacent houses. The rainwater from a house at index <code>i</code> is collected if a bucket is placed at index <code>i - 1</code> <strong>and/or</strong> index <code>i + 1</code>. A single bucket, if placed adjacent to two houses, can collect the rainwater from <strong>both</strong> houses.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>number of buckets needed so that for <strong>every</strong> house, there is <strong>at least</strong> one bucket collecting rainwater from it, or </em><code>-1</code><em> if it is impossible.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> street = "H..H"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can put buckets at index 1 and index 2.
|
||||
"H..H" -> "HBBH" ('B' denotes where a bucket is placed).
|
||||
The house at index 0 has a bucket to its right, and the house at index 3 has a bucket to its left.
|
||||
Thus, for every house, there is at least one bucket collecting rainwater from it.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> street = ".H.H."
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can put a bucket at index 2.
|
||||
".H.H." -> ".HBH." ('B' denotes where a bucket is placed).
|
||||
The house at index 1 has a bucket to its right, and the house at index 3 has a bucket to its left.
|
||||
Thus, for every house, there is at least one bucket collecting rainwater from it.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> street = ".HHH."
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong>
|
||||
There is no empty space to place a bucket to collect the rainwater from the house at index 2.
|
||||
Thus, it is impossible to collect the rainwater from all the houses.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= street.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>street[i]</code> is either<code>'H'</code> or <code>'.'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of <strong>distinct</strong> integers <code>nums</code>.</p>
|
||||
|
||||
<p>There is an element in <code>nums</code> that has the <strong>lowest</strong> value and an element that has the <strong>highest</strong> value. We call them the <strong>minimum</strong> and <strong>maximum</strong> respectively. Your goal is to remove <strong>both</strong> these elements from the array.</p>
|
||||
|
||||
<p>A <strong>deletion</strong> is defined as either removing an element from the <strong>front</strong> of the array or removing an element from the <strong>back</strong> of the array.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of deletions it would take to remove <strong>both</strong> the minimum and maximum element from the array.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,<u><strong>10</strong></u>,7,5,4,<u><strong>1</strong></u>,8,6]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
The minimum element in the array is nums[5], which is 1.
|
||||
The maximum element in the array is nums[1], which is 10.
|
||||
We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
|
||||
This results in 2 + 3 = 5 deletions, which is the minimum number possible.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,<u><strong>-4</strong></u>,<u><strong>19</strong></u>,1,8,-2,-3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The minimum element in the array is nums[1], which is -4.
|
||||
The maximum element in the array is nums[2], which is 19.
|
||||
We can remove both the minimum and maximum by removing 3 elements from the front.
|
||||
This results in only 3 deletions, which is the minimum number possible.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [<u><strong>101</strong></u>]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one element in the array, which makes it both the minimum and maximum element.
|
||||
We can remove it with 1 deletion.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li>The integers in <code>nums</code> are <strong>distinct</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,51 @@
|
||||
<p>You have information about <code>n</code> different recipes. You are given a string array <code>recipes</code> and a 2D string array <code>ingredients</code>. The <code>i<sup>th</sup></code> recipe has the name <code>recipes[i]</code>, and you can <strong>create</strong> it if you have <strong>all</strong> the needed ingredients from <code>ingredients[i]</code>. Ingredients to a recipe may need to be created from <strong>other </strong>recipes, i.e., <code>ingredients[i]</code> may contain a string that is in <code>recipes</code>.</p>
|
||||
|
||||
<p>You are also given a string array <code>supplies</code> containing all the ingredients that you initially have, and you have an infinite supply of all of them.</p>
|
||||
|
||||
<p>Return <em>a list of all the recipes that you can create. </em>You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p>Note that two recipes may contain each other in their ingredients.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
|
||||
<strong>Output:</strong> ["bread"]
|
||||
<strong>Explanation:</strong>
|
||||
We can create "bread" since we have the ingredients "yeast" and "flour".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
|
||||
<strong>Output:</strong> ["bread","sandwich"]
|
||||
<strong>Explanation:</strong>
|
||||
We can create "bread" since we have the ingredients "yeast" and "flour".
|
||||
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
|
||||
<strong>Output:</strong> ["bread","sandwich","burger"]
|
||||
<strong>Explanation:</strong>
|
||||
We can create "bread" since we have the ingredients "yeast" and "flour".
|
||||
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
|
||||
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == recipes.length == ingredients.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= ingredients[i].length, supplies.length <= 100</code></li>
|
||||
<li><code>1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10</code></li>
|
||||
<li><code>recipes[i], ingredients[i][j]</code>, and <code>supplies[k]</code> consist only of lowercase English letters.</li>
|
||||
<li>All the values of <code>recipes</code> and <code>supplies</code> combined are unique.</li>
|
||||
<li>Each <code>ingredients[i]</code> does not contain any duplicate values.</li>
|
||||
</ul>
|
@@ -0,0 +1,85 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> that represents a map of the items in a shop. The integers in the grid represent the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> represents a wall that you cannot pass through.</li>
|
||||
<li><code>1</code> represents an empty cell that you can freely move to and from.</li>
|
||||
<li>All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells.</li>
|
||||
</ul>
|
||||
|
||||
<p>It takes <code>1</code> step to travel between adjacent grid cells.</p>
|
||||
|
||||
<p>You are also given integer arrays <code>pricing</code> and <code>start</code> where <code>pricing = [low, high]</code> and <code>start = [row, col]</code> indicates that you start at the position <code>(row, col)</code> and are interested only in items with a price in the range of <code>[low, high]</code> (<strong>inclusive</strong>). You are further given an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are interested in the <strong>positions</strong> of the <code>k</code> <strong>highest-ranked</strong> items whose prices are <strong>within</strong> the given price range. The rank is determined by the <strong>first</strong> of these criteria that is different:</p>
|
||||
|
||||
<ol>
|
||||
<li>Distance, defined as the length of the shortest path from the <code>start</code> (<strong>shorter</strong> distance has a higher rank).</li>
|
||||
<li>Price (<strong>lower</strong> price has a higher rank, but it must be <strong>in the price range</strong>).</li>
|
||||
<li>The row number (<strong>smaller</strong> row number has a higher rank).</li>
|
||||
<li>The column number (<strong>smaller</strong> column number has a higher rank).</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the </em><code>k</code><em> highest-ranked items within the price range <strong>sorted</strong> by their rank (highest to lowest)</em>. If there are fewer than <code>k</code> reachable items within the price range, return <em><strong>all</strong> of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example1drawio.png" style="width: 200px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3
|
||||
<strong>Output:</strong> [[0,1],[1,1],[2,1]]
|
||||
<strong>Explanation:</strong> You start at (0,0).
|
||||
With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2).
|
||||
The ranks of these items are:
|
||||
- (0,1) with distance 1
|
||||
- (1,1) with distance 2
|
||||
- (2,1) with distance 3
|
||||
- (2,2) with distance 4
|
||||
Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example2drawio1.png" style="width: 200px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2
|
||||
<strong>Output:</strong> [[2,1],[1,2]]
|
||||
<strong>Explanation:</strong> You start at (2,3).
|
||||
With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1).
|
||||
The ranks of these items are:
|
||||
- (2,1) with distance 2, price 2
|
||||
- (1,2) with distance 2, price 3
|
||||
- (1,1) with distance 3
|
||||
- (0,1) with distance 4
|
||||
Thus, the 2 highest ranked items in the price range are (2,1) and (1,2).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/example3.png" style="width: 149px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3
|
||||
<strong>Output:</strong> [[2,1],[2,0]]
|
||||
<strong>Explanation:</strong> You start at (0,0).
|
||||
With a price range of [2,3], we can take items from (2,0) and (2,1).
|
||||
The ranks of these items are:
|
||||
- (2,1) with distance 5
|
||||
- (2,0) with distance 6
|
||||
Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).
|
||||
Note that k = 3 but there are only 2 reachable items within the price range.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>pricing.length == 2</code></li>
|
||||
<li><code>2 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>start.length == 2</code></li>
|
||||
<li><code>0 <= row <= m - 1</code></li>
|
||||
<li><code>0 <= col <= n - 1</code></li>
|
||||
<li><code>grid[row][col] > 0</code></li>
|
||||
<li><code>1 <= k <= m * n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>Given an array of strings <code>patterns</code> and a string <code>word</code>, return <em>the <strong>number</strong> of strings in </em><code>patterns</code><em> that exist as a <strong>substring</strong> in </em><code>word</code>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> patterns = ["a","abc","bc","d"], word = "abc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- "a" appears as a substring in "<u>a</u>bc".
|
||||
- "abc" appears as a substring in "<u>abc</u>".
|
||||
- "bc" appears as a substring in "a<u>bc</u>".
|
||||
- "d" does not appear as a substring in "abc".
|
||||
3 of the strings in patterns appear as a substring in word.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> patterns = ["a","b","c"], word = "aaaaabbbbb"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- "a" appears as a substring in "a<u>a</u>aaabbbbb".
|
||||
- "b" appears as a substring in "aaaaabbbb<u>b</u>".
|
||||
- "c" does not appear as a substring in "aaaaabbbbb".
|
||||
2 of the strings in patterns appear as a substring in word.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> patterns = ["a","a","a"], word = "ab"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Each of the patterns appears as a substring in word "<u>a</u>b".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= patterns.length <= 100</code></li>
|
||||
<li><code>1 <= patterns[i].length <= 100</code></li>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>patterns[i]</code> and <code>word</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>You have <code>n</code> tasks and <code>m</code> workers. Each task has a strength requirement stored in a <strong>0-indexed</strong> integer array <code>tasks</code>, with the <code>i<sup>th</sup></code> task requiring <code>tasks[i]</code> strength to complete. The strength of each worker is stored in a <strong>0-indexed</strong> integer array <code>workers</code>, with the <code>j<sup>th</sup></code> worker having <code>workers[j]</code> strength. Each worker can only be assigned to a <strong>single</strong> task and must have a strength <strong>greater than or equal</strong> to the task's strength requirement (i.e., <code>workers[j] >= tasks[i]</code>).</p>
|
||||
|
||||
<p>Additionally, you have <code>pills</code> magical pills that will <strong>increase a worker's strength</strong> by <code>strength</code>. You can decide which workers receive the magical pills, however, you may only give each worker <strong>at most one</strong> magical pill.</p>
|
||||
|
||||
<p>Given the <strong>0-indexed </strong>integer arrays <code>tasks</code> and <code>workers</code> and the integers <code>pills</code> and <code>strength</code>, return <em>the <strong>maximum</strong> number of tasks that can be completed.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [<u><strong>3</strong></u>,<u><strong>2</strong></u>,<u><strong>1</strong></u>], workers = [<u><strong>0</strong></u>,<u><strong>3</strong></u>,<u><strong>3</strong></u>], pills = 1, strength = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
We can assign the magical pill and tasks as follows:
|
||||
- Give the magical pill to worker 0.
|
||||
- Assign worker 0 to task 2 (0 + 1 >= 1)
|
||||
- Assign worker 1 to task 1 (3 >= 2)
|
||||
- Assign worker 2 to task 0 (3 >= 3)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [<u><strong>5</strong></u>,4], workers = [<u><strong>0</strong></u>,0,0], pills = 1, strength = 5
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
We can assign the magical pill and tasks as follows:
|
||||
- Give the magical pill to worker 0.
|
||||
- Assign worker 0 to task 0 (0 + 5 >= 5)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [<u><strong>10</strong></u>,<u><strong>15</strong></u>,30], workers = [<u><strong>0</strong></u>,<u><strong>10</strong></u>,10,10,10], pills = 3, strength = 10
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
We can assign the magical pills and tasks as follows:
|
||||
- Give the magical pill to worker 0 and worker 1.
|
||||
- Assign worker 0 to task 0 (0 + 10 >= 10)
|
||||
- Assign worker 1 to task 1 (10 + 10 >= 15)
|
||||
The last pill is not given because it will not make any worker strong enough for the last task.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == tasks.length</code></li>
|
||||
<li><code>m == workers.length</code></li>
|
||||
<li><code>1 <= n, m <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= pills <= m</code></li>
|
||||
<li><code>0 <= tasks[i], workers[j], strength <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,55 @@
|
||||
<p>There are <code>n</code> projects numbered from <code>0</code> to <code>n - 1</code>. You are given an integer array <code>milestones</code> where each <code>milestones[i]</code> denotes the number of milestones the <code>i<sup>th</sup></code> project has.</p>
|
||||
|
||||
<p>You can work on the projects following these two rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>Every week, you will finish <strong>exactly one</strong> milestone of <strong>one</strong> project. You <strong>must</strong> work every week.</li>
|
||||
<li>You <strong>cannot</strong> work on two milestones from the same project for two <strong>consecutive</strong> weeks.</li>
|
||||
</ul>
|
||||
|
||||
<p>Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will <strong>stop working</strong>. Note that you may not be able to finish every project's milestones due to these constraints.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of weeks you would be able to work on the projects without violating the rules mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> milestones = [1,2,3]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> One possible scenario is:
|
||||
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
|
||||
- During the 2<sup>nd</sup> week, you will work on a milestone of project 2.
|
||||
- During the 3<sup>rd</sup> week, you will work on a milestone of project 1.
|
||||
- During the 4<sup>th</sup> week, you will work on a milestone of project 2.
|
||||
- During the 5<sup>th</sup> week, you will work on a milestone of project 1.
|
||||
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
|
||||
The total number of weeks is 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> milestones = [5,2,1]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> One possible scenario is:
|
||||
- During the 1<sup>st</sup> week, you will work on a milestone of project 0.
|
||||
- During the 2<sup>nd</sup> week, you will work on a milestone of project 1.
|
||||
- During the 3<sup>rd</sup> week, you will work on a milestone of project 0.
|
||||
- During the 4<sup>th</sup> week, you will work on a milestone of project 1.
|
||||
- During the 5<sup>th</sup> week, you will work on a milestone of project 0.
|
||||
- During the 6<sup>th</sup> week, you will work on a milestone of project 2.
|
||||
- During the 7<sup>th</sup> week, you will work on a milestone of project 0.
|
||||
The total number of weeks is 7.
|
||||
Note that you cannot work on the last milestone of project 0 on 8<sup>th</sup> week because it would violate the rules.
|
||||
Thus, one milestone in project 0 will remain unfinished.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == milestones.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= milestones[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>There is a <strong>1-based</strong> binary matrix where <code>0</code> represents land and <code>1</code> represents water. You are given integers <code>row</code> and <code>col</code> representing the number of rows and columns in the matrix, respectively.</p>
|
||||
|
||||
<p>Initially on day <code>0</code>, the <strong>entire</strong> matrix is <strong>land</strong>. However, each day a new cell becomes flooded with <strong>water</strong>. You are given a <strong>1-based</strong> 2D array <code>cells</code>, where <code>cells[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> represents that on the <code>i<sup>th</sup></code> day, the cell on the <code>r<sub>i</sub><sup>th</sup></code> row and <code>c<sub>i</sub><sup>th</sup></code> column (<strong>1-based</strong> coordinates) will be covered with <strong>water</strong> (i.e., changed to <code>1</code>).</p>
|
||||
|
||||
<p>You want to find the <strong>last</strong> day that it is possible to walk from the <strong>top</strong> to the <strong>bottom</strong> by only walking on land cells. You can start from <strong>any</strong> cell in the top row and end at <strong>any</strong> cell in the bottom row. You can only travel in the<strong> four</strong> cardinal directions (left, right, up, and down).</p>
|
||||
|
||||
<p>Return <em>the <strong>last</strong> day where it is possible to walk from the <strong>top</strong> to the <strong>bottom</strong> by only walking on land cells</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/27/1.png" style="width: 624px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.
|
||||
The last day where it is possible to cross from top to bottom is on day 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/27/2.png" style="width: 504px; height: 178px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.
|
||||
The last day where it is possible to cross from top to bottom is on day 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/27/3.png" style="width: 666px; height: 167px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.
|
||||
The last day where it is possible to cross from top to bottom is on day 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= row, col <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>4 <= row * col <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>cells.length == row * col</code></li>
|
||||
<li><code>1 <= r<sub>i</sub> <= row</code></li>
|
||||
<li><code>1 <= c<sub>i</sub> <= col</code></li>
|
||||
<li>All the values of <code>cells</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given two strings <code>s</code> and <code>t</code>. In one step, you can append <strong>any character</strong> to either <code>s</code> or <code>t</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of steps to make </em><code>s</code><em> and </em><code>t</code><em> <strong>anagrams</strong> of each other.</em></p>
|
||||
|
||||
<p>An <strong>anagram</strong> of a string is a string that contains the same characters with a different (or the same) ordering.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "<strong><u>lee</u></strong>tco<u><strong>de</strong></u>", t = "co<u><strong>a</strong></u>t<u><strong>s</strong></u>"
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong>
|
||||
- In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcode<strong><u>as</u></strong>".
|
||||
- In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coats<u><strong>leede</strong></u>".
|
||||
"leetcodeas" and "coatsleede" are now anagrams of each other.
|
||||
We used a total of 2 + 5 = 7 steps.
|
||||
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "night", t = "thing"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The given strings are already anagrams of each other. Thus, we do not need any further steps.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, t.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> and <code>t</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code> of <strong>even</strong> length <code>n</code>. The string consists of <strong>exactly</strong> <code>n / 2</code> opening brackets <code>'['</code> and <code>n / 2</code> closing brackets <code>']'</code>.</p>
|
||||
|
||||
<p>A string is called <strong>balanced</strong> if and only if:</p>
|
||||
|
||||
<ul>
|
||||
<li>It is the empty string, or</li>
|
||||
<li>It can be written as <code>AB</code>, where both <code>A</code> and <code>B</code> are <strong>balanced</strong> strings, or</li>
|
||||
<li>It can be written as <code>[C]</code>, where <code>C</code> is a <strong>balanced</strong> string.</li>
|
||||
</ul>
|
||||
|
||||
<p>You may swap the brackets at <strong>any</strong> two indices <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of swaps to make </em><code>s</code> <em><strong>balanced</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "][]["
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You can make the string balanced by swapping index 0 with index 3.
|
||||
The resulting string is "[[]]".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "]]][[["
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can do the following to make the string balanced:
|
||||
- Swap index 0 with index 4. s = "[]][][".
|
||||
- Swap index 1 with index 5. s = "[[][]]".
|
||||
The resulting string is "[[][]]".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "[]"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The string is already balanced.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>6</sup></code></li>
|
||||
<li><code>n</code> is even.</li>
|
||||
<li><code>s[i]</code> is either <code>'[' </code>or <code>']'</code>.</li>
|
||||
<li>The number of opening brackets <code>'['</code> equals <code>n / 2</code>, and the number of closing brackets <code>']'</code> equals <code>n / 2</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>arr</code> consisting of <code>n</code> positive integers, and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The array <code>arr</code> is called <strong>K-increasing</strong> if <code>arr[i-k] <= arr[i]</code> holds for every index <code>i</code>, where <code>k <= i <= n-1</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>arr = [4, 1, 5, 2, 6, 2]</code> is K-increasing for <code>k = 2</code> because:
|
||||
|
||||
<ul>
|
||||
<li><code>arr[0] <= arr[2] (4 <= 5)</code></li>
|
||||
<li><code>arr[1] <= arr[3] (1 <= 2)</code></li>
|
||||
<li><code>arr[2] <= arr[4] (5 <= 6)</code></li>
|
||||
<li><code>arr[3] <= arr[5] (2 <= 2)</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>However, the same <code>arr</code> is not K-increasing for <code>k = 1</code> (because <code>arr[0] > arr[1]</code>) or <code>k = 3</code> (because <code>arr[0] > arr[3]</code>).</li>
|
||||
</ul>
|
||||
|
||||
<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>arr[i]</code> into <strong>any</strong> positive integer.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array K-increasing for the given </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [5,4,3,2,1], k = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:
|
||||
</strong>For k = 1, the resultant array has to be non-decreasing.
|
||||
Some of the K-increasing arrays that can be formed are [5,<u><strong>6</strong></u>,<u><strong>7</strong></u>,<u><strong>8</strong></u>,<u><strong>9</strong></u>], [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,1], [<u><strong>2</strong></u>,<u><strong>2</strong></u>,3,<u><strong>4</strong></u>,<u><strong>4</strong></u>]. All of them require 4 operations.
|
||||
It is suboptimal to change the array to, for example, [<u><strong>6</strong></u>,<u><strong>7</strong></u>,<u><strong>8</strong></u>,<u><strong>9</strong></u>,<u><strong>10</strong></u>] because it would take 5 operations.
|
||||
It can be shown that we cannot make the array K-increasing in less than 4 operations.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [4,1,5,2,6,2], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
This is the same example as the one in the problem description.
|
||||
Here, for every index i where 2 <= i <= 5, arr[i-2] <=<b> </b>arr[i].
|
||||
Since the given array is already K-increasing, we do not need to perform any operations.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [4,1,5,2,6,2], k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.
|
||||
One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.
|
||||
The array will now be [4,1,5,<u><strong>4</strong></u>,6,<u><strong>5</strong></u>].
|
||||
Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i], k <= arr.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>
|
||||
|
||||
<p>The array <code>nums</code> is called <strong>alternating</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i - 2] == nums[i]</code>, where <code>2 <= i <= n - 1</code>.</li>
|
||||
<li><code>nums[i - 1] != nums[i]</code>, where <code>1 <= i <= n - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>nums[i]</code> into <strong>any</strong> positive integer.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array alternating</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,3,2,4,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
One way to make the array alternating is by converting it to [3,1,3,<u><strong>1</strong></u>,<u><strong>3</strong></u>,<u><strong>1</strong></u>].
|
||||
The number of operations required in this case is 3.
|
||||
It can be proven that it is not possible to make the array alternating in less than 3 operations.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,2,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
One way to make the array alternating is by converting it to [1,2,<u><strong>1</strong></u>,2,<u><strong>1</strong></u>].
|
||||
The number of operations required in this case is 2.
|
||||
Note that the array cannot be converted to [<u><strong>2</strong></u>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given an integer array <code>nums</code>. In one operation, you can replace <strong>any</strong> element in <code>nums</code> with <strong>any</strong> integer.</p>
|
||||
|
||||
<p><code>nums</code> is considered <strong>continuous</strong> if both of the following conditions are fulfilled:</p>
|
||||
|
||||
<ul>
|
||||
<li>All elements in <code>nums</code> are <strong>unique</strong>.</li>
|
||||
<li>The difference between the <strong>maximum</strong> element and the <strong>minimum</strong> element in <code>nums</code> equals <code>nums.length - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>nums = [4, 2, 5, 3]</code> is <strong>continuous</strong>, but <code>nums = [1, 2, 3, 5, 6]</code> is <strong>not continuous</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>nums</code><em> </em><strong><em>continuous</em></strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,5,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums is already continuous.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,5,6]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> One possible solution is to change the last element to 4.
|
||||
The resulting array is [1,2,3,5,4], which is continuous.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,10,100,1000]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> One possible solution is to:
|
||||
- Change the second element to 2.
|
||||
- Change the third element to 3.
|
||||
- Change the fourth element to 4.
|
||||
The resulting array is [1,2,3,4], which is continuous.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>There are <code>n</code> seats and <code>n</code> students in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p>
|
||||
|
||||
<p>You may perform the following move any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase or decrease the position of the <code>i<sup>th</sup></code> student by <code>1</code> (i.e., moving the <code>i<sup>th</sup></code> student from position <code>x</code> to <code>x + 1</code> or <code>x - 1</code>)</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of moves</strong> required to move each student to a seat</em><em> such that no two students are in the same seat.</em></p>
|
||||
|
||||
<p>Note that there may be <strong>multiple</strong> seats or students in the <strong>same </strong>position at the beginning.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> seats = [3,1,5], students = [2,7,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The students are moved as follows:
|
||||
- The first student is moved from from position 2 to position 1 using 1 move.
|
||||
- The second student is moved from from position 7 to position 5 using 2 moves.
|
||||
- The third student is moved from from position 4 to position 3 using 1 move.
|
||||
In total, 1 + 2 + 1 = 4 moves were used.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> seats = [4,1,5,9], students = [1,3,2,6]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The students are moved as follows:
|
||||
- The first student is not moved.
|
||||
- The second student is moved from from position 3 to position 4 using 1 move.
|
||||
- The third student is moved from from position 2 to position 5 using 3 moves.
|
||||
- The fourth student is moved from from position 6 to position 9 using 3 moves.
|
||||
In total, 0 + 1 + 3 + 3 = 7 moves were used.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> seats = [2,2,6,6], students = [1,3,2,6]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6.
|
||||
The students are moved as follows:
|
||||
- The first student is moved from from position 1 to position 2 using 1 move.
|
||||
- The second student is moved from from position 3 to position 6 using 3 moves.
|
||||
- The third student is not moved.
|
||||
- The fourth student is not moved.
|
||||
In total, 1 + 3 + 0 + 0 = 4 moves were used.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == seats.length == students.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= seats[i], students[j] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,65 @@
|
||||
<p>There is a special typewriter with lowercase English letters <code>'a'</code> to <code>'z'</code> arranged in a <strong>circle</strong> with a <strong>pointer</strong>. A character can <strong>only</strong> be typed if the pointer is pointing to that character. The pointer is <strong>initially</strong> pointing to the character <code>'a'</code>.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/31/chart.jpg" style="width: 530px; height: 410px;" />
|
||||
<p>Each second, you may perform one of the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Move the pointer one character <strong>counterclockwise</strong> or <strong>clockwise</strong>.</li>
|
||||
<li>Type the character the pointer is <strong>currently</strong> on.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>word</code>, return the<strong> minimum</strong> number of seconds to type out the characters in <code>word</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abc"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:
|
||||
</strong>The characters are printed as follows:
|
||||
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
|
||||
- Move the pointer clockwise to 'b' in 1 second.
|
||||
- Type the character 'b' in 1 second.
|
||||
- Move the pointer clockwise to 'c' in 1 second.
|
||||
- Type the character 'c' in 1 second.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "bza"
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:
|
||||
</strong>The characters are printed as follows:
|
||||
- Move the pointer clockwise to 'b' in 1 second.
|
||||
- Type the character 'b' in 1 second.
|
||||
- Move the pointer counterclockwise to 'z' in 2 seconds.
|
||||
- Type the character 'z' in 1 second.
|
||||
- Move the pointer clockwise to 'a' in 1 second.
|
||||
- Type the character 'a' in 1 second.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "zjpc"
|
||||
<strong>Output:</strong> 34
|
||||
<strong>Explanation:</strong>
|
||||
The characters are printed as follows:
|
||||
- Move the pointer counterclockwise to 'z' in 1 second.
|
||||
- Type the character 'z' in 1 second.
|
||||
- Move the pointer clockwise to 'j' in 10 seconds.
|
||||
- Type the character 'j' in 1 second.
|
||||
- Move the pointer clockwise to 'p' in 6 seconds.
|
||||
- Type the character 'p' in 1 second.
|
||||
- Move the pointer counterclockwise to 'c' in 13 seconds.
|
||||
- Type the character 'c' in 1 second.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,45 @@
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>smallest</strong> index </em><code>i</code><em> of </em><code>nums</code><em> such that </em><code>i mod 10 == nums[i]</code><em>, or </em><code>-1</code><em> if such index does not exist</em>.</p>
|
||||
|
||||
<p><code>x mod y</code> denotes the <strong>remainder</strong> when <code>x</code> is divided by <code>y</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,2]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
i=0: 0 mod 10 = 0 == nums[0].
|
||||
i=1: 1 mod 10 = 1 == nums[1].
|
||||
i=2: 2 mod 10 = 2 == nums[2].
|
||||
All indices have i mod 10 == nums[i], so we return the smallest index 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
i=0: 0 mod 10 = 0 != nums[0].
|
||||
i=1: 1 mod 10 = 1 != nums[1].
|
||||
i=2: 2 mod 10 = 2 == nums[2].
|
||||
i=3: 3 mod 10 = 3 != nums[3].
|
||||
2 is the only index which has i mod 10 == nums[i].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6,7,8,9,0]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> No index satisfies i mod 10 == nums[i].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of elements that have <strong>both</strong> a strictly smaller and a strictly greater element appear in </em><code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [11,7,2,15]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
|
||||
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
|
||||
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-3,3,3,90]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
|
||||
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums</code>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>You have <code>n</code> flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two <strong>0-indexed</strong> integer arrays <code>plantTime</code> and <code>growTime</code>, of length <code>n</code> each:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>plantTime[i]</code> is the number of <strong>full days</strong> it takes you to <strong>plant</strong> the <code>i<sup>th</sup></code> seed. Every day, you can work on planting exactly one seed. You <strong>do not</strong> have to work on planting the same seed on consecutive days, but the planting of a seed is not complete <strong>until</strong> you have worked <code>plantTime[i]</code> days on planting it in total.</li>
|
||||
<li><code>growTime[i]</code> is the number of <strong>full days</strong> it takes the <code>i<sup>th</sup></code> seed to grow after being completely planted. <strong>After</strong> the last day of its growth, the flower <strong>blooms</strong> and stays bloomed forever.</li>
|
||||
</ul>
|
||||
|
||||
<p>From the beginning of day <code>0</code>, you can plant the seeds in <strong>any</strong> order.</p>
|
||||
|
||||
<p>Return <em>the <strong>earliest</strong> possible day where <strong>all</strong> seeds are blooming</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/1.png" style="width: 453px; height: 149px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> plantTime = [1,4,3], growTime = [2,3,1]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
|
||||
One optimal way is:
|
||||
On day 0, plant the 0<sup>th</sup> seed. The seed grows for 2 full days and blooms on day 3.
|
||||
On days 1, 2, 3, and 4, plant the 1<sup>st</sup> seed. The seed grows for 3 full days and blooms on day 8.
|
||||
On days 5, 6, and 7, plant the 2<sup>nd</sup> seed. The seed grows for 1 full day and blooms on day 9.
|
||||
Thus, on day 9, all the seeds are blooming.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/2.png" style="width: 454px; height: 184px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> plantTime = [1,2,3,2], growTime = [2,1,2,1]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
|
||||
One optimal way is:
|
||||
On day 1, plant the 0<sup>th</sup> seed. The seed grows for 2 full days and blooms on day 4.
|
||||
On days 0 and 3, plant the 1<sup>st</sup> seed. The seed grows for 1 full day and blooms on day 5.
|
||||
On days 2, 4, and 5, plant the 2<sup>nd</sup> seed. The seed grows for 2 full days and blooms on day 8.
|
||||
On days 6 and 7, plant the 3<sup>rd</sup> seed. The seed grows for 1 full day and blooms on day 9.
|
||||
Thus, on day 9, all the seeds are blooming.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> plantTime = [1], growTime = [1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> On day 0, plant the 0<sup>th</sup> seed. The seed grows for 1 full day and blooms on day 2.
|
||||
Thus, on day 2, all the seeds are blooming.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == plantTime.length == growTime.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= plantTime[i], growTime[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,22 @@
|
||||
<p>Write an algorithm to print all ways of arranging n queens on an n x n chess board so that none of them share the same row, column, or diagonal. In this case, "diagonal" means all diagonals, not just the two that bisect the board.</p>
|
||||
|
||||
<p><strong>Notes: </strong>This problem is a generalization of the original one in the book.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong> Input</strong>: 4
|
||||
<strong> Output</strong>: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
|
||||
<strong> Explanation</strong>: 4 queens has following two solutions
|
||||
[
|
||||
[".Q..", // solution 1
|
||||
"...Q",
|
||||
"Q...",
|
||||
"..Q."],
|
||||
|
||||
["..Q.", // solution 2
|
||||
"Q...",
|
||||
"...Q",
|
||||
".Q.."]
|
||||
]
|
||||
</pre>
|
@@ -0,0 +1,47 @@
|
||||
<p>An ant is sitting on an infinite grid of white and black squares. It initially faces right. All squares are white initially.</p>
|
||||
|
||||
<p>At each step, it does the following:</p>
|
||||
|
||||
<p>(1) At a white square, flip the color of the square, turn 90 degrees right (clockwise), and move forward one unit.</p>
|
||||
|
||||
<p>(2) At a black square, flip the color of the square, turn 90 degrees left (counter-clockwise), and move forward one unit.</p>
|
||||
|
||||
<p>Write a program to simulate the first K moves that the ant makes and print the final board as a grid.</p>
|
||||
|
||||
<p>The grid should be represented as an array of strings, where each element represents one row in the grid. The black square is represented as <code>'X'</code>, and the white square is represented as <code>'_'</code>, the square which is occupied by the ant is represented as <code>'L'</code>, <code>'U'</code>, <code>'R'</code>, <code>'D'</code>, which means the left, up, right and down orientations respectively. You only need to return the minimum matrix that is able to contain all squares that are passed through by the ant.</p>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> 0
|
||||
<strong>Output: </strong>["R"]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> 2
|
||||
<strong>Output:
|
||||
</strong>[
|
||||
"_X",
|
||||
"LX"
|
||||
]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> 5
|
||||
<strong>Output:
|
||||
</strong>[
|
||||
"_U",
|
||||
"X_",
|
||||
"XX"
|
||||
]
|
||||
</pre>
|
||||
|
||||
<p><strong>Note: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>K <= 100000</code></li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,40 @@
|
||||
<p>There are <code>n</code> points on a road you are driving your taxi on. The <code>n</code> points on the road are labeled from <code>1</code> to <code>n</code> in the direction you are going, and you want to drive from point <code>1</code> to point <code>n</code> to make money by picking up passengers. You cannot change the direction of the taxi.</p>
|
||||
|
||||
<p>The passengers are represented by a <strong>0-indexed</strong> 2D integer array <code>rides</code>, where <code>rides[i] = [start<sub>i</sub>, end<sub>i</sub>, tip<sub>i</sub>]</code> denotes the <code>i<sup>th</sup></code> passenger requesting a ride from point <code>start<sub>i</sub></code> to point <code>end<sub>i</sub></code> who is willing to give a <code>tip<sub>i</sub></code> dollar tip.</p>
|
||||
|
||||
<p>For<strong> each </strong>passenger <code>i</code> you pick up, you <strong>earn</strong> <code>end<sub>i</sub> - start<sub>i</sub> + tip<sub>i</sub></code> dollars. You may only drive <b>at most one </b>passenger at a time.</p>
|
||||
|
||||
<p>Given <code>n</code> and <code>rides</code>, return <em>the <strong>maximum</strong> number of dollars you can earn by picking up the passengers optimally.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> You may drop off a passenger and pick up a different passenger at the same point.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, rides = [<u>[2,5,4]</u>,[1,5,1]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> We can pick up passenger 0 to earn 5 - 2 + 4 = 7 dollars.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 20, rides = [[1,6,1],<u>[3,10,2]</u>,<u>[10,12,3]</u>,[11,12,2],[12,15,2],<u>[13,18,1]</u>]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> We will pick up the following passengers:
|
||||
- Drive passenger 1 from point 3 to point 10 for a profit of 10 - 3 + 2 = 9 dollars.
|
||||
- Drive passenger 2 from point 10 to point 12 for a profit of 12 - 10 + 3 = 5 dollars.
|
||||
- Drive passenger 5 from point 13 to point 18 for a profit of 18 - 13 + 1 = 6 dollars.
|
||||
We earn 9 + 5 + 6 = 20 dollars in total.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= rides.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>rides[i].length == 3</code></li>
|
||||
<li><code>1 <= start<sub>i</sub> < end<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= tip<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>. The number of ways to <strong>partition</strong> <code>nums</code> is the number of <code>pivot</code> indices that satisfy both conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= pivot < n</code></li>
|
||||
<li><code>nums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. You can choose to change the value of <strong>one</strong> element of <code>nums</code> to <code>k</code>, or to leave the array <strong>unchanged</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible number of ways to <strong>partition</strong> </em><code>nums</code><em> to satisfy both conditions after changing <strong>at most</strong> one element</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-1,2], k = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> One optimal approach is to change nums[0] to k. The array becomes [<strong><u>3</u></strong>,-1,2].
|
||||
There is one way to partition the array:
|
||||
- For pivot = 2, we have the partition [3,-1 | 2]: 3 + -1 == 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0], k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The optimal approach is to leave the array unchanged.
|
||||
There are two ways to partition the array:
|
||||
- For pivot = 1, we have the partition [0 | 0,0]: 0 == 0 + 0.
|
||||
- For pivot = 2, we have the partition [0,0 | 0]: 0 + 0 == 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k = -33
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One optimal approach is to change nums[2] to k. The array becomes [22,4,<u><strong>-33</strong></u>,-20,-15,15,-16,7,19,-10,0,-13,-14].
|
||||
There are four ways to partition the array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= k, nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
1
算法题(国内版)/problem (English)/分割等和子集(English) [NUPfPr].html
Normal file
1
算法题(国内版)/problem (English)/分割等和子集(English) [NUPfPr].html
Normal file
@@ -0,0 +1 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.</p>
|
@@ -0,0 +1,8 @@
|
||||
<p>Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the "right partition"; it does not need to appear between the left and right partitions.</p>
|
||||
|
||||
<p><strong>Example:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = 3->5->8->5->10->2->1, <em>x</em> = 5
|
||||
<strong>Output:</strong> 3->1->2->10->5->5->8
|
||||
</pre>
|
@@ -0,0 +1,2 @@
|
||||
<p>English description is not available for the problem. Please switch to Chinese.<br />
|
||||
</p>
|
@@ -0,0 +1,60 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary array <code>nums</code> of length <code>n</code>. <code>nums</code> can be divided at index <code>i</code> (where <code>0 <= i <= n)</code> into two arrays (possibly empty) <code>nums<sub>left</sub></code> and <code>nums<sub>right</sub></code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums<sub>left</sub></code> has all the elements of <code>nums</code> between index <code>0</code> and <code>i - 1</code> <strong>(inclusive)</strong>, while <code>nums<sub>right</sub></code> has all the elements of nums between index <code>i</code> and <code>n - 1</code> <strong>(inclusive)</strong>.</li>
|
||||
<li>If <code>i == 0</code>, <code>nums<sub>left</sub></code> is <strong>empty</strong>, while <code>nums<sub>right</sub></code> has all the elements of <code>nums</code>.</li>
|
||||
<li>If <code>i == n</code>, <code>nums<sub>left</sub></code> has all the elements of nums, while <code>nums<sub>right</sub></code> is <strong>empty</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>division score</strong> of an index <code>i</code> is the <strong>sum</strong> of the number of <code>0</code>'s in <code>nums<sub>left</sub></code> and the number of <code>1</code>'s in <code>nums<sub>right</sub></code>.</p>
|
||||
|
||||
<p>Return <em><strong>all distinct indices</strong> that have the <strong>highest</strong> possible <strong>division score</strong></em>. You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,1,0]
|
||||
<strong>Output:</strong> [2,4]
|
||||
<strong>Explanation:</strong> Division at index
|
||||
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,<u><strong>1</strong></u>,0]. The score is 0 + 1 = 1.
|
||||
- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,<u><strong>1</strong></u>,0]. The score is 1 + 1 = 2.
|
||||
- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [<u><strong>1</strong></u>,0]. The score is 2 + 1 = 3.
|
||||
- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
|
||||
- 4: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
|
||||
Indices 2 and 4 both have the highest possible division score 3.
|
||||
Note the answer [4,2] would also be accepted.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0]
|
||||
<strong>Output:</strong> [3]
|
||||
<strong>Explanation:</strong> Division at index
|
||||
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,0]. The score is 0 + 0 = 0.
|
||||
- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,0]. The score is 1 + 0 = 1.
|
||||
- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
|
||||
- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
|
||||
Only index 3 has the highest possible division score 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> [0]
|
||||
<strong>Explanation:</strong> Division at index
|
||||
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [<u><strong>1</strong></u>,<u><strong>1</strong></u>]. The score is 0 + 2 = 2.
|
||||
- 1: nums<sub>left</sub> is [1]. nums<sub>right</sub> is [<u><strong>1</strong></u>]. The score is 0 + 1 = 1.
|
||||
- 2: nums<sub>left</sub> is [1,1]. nums<sub>right</sub> is []. The score is 0 + 0 = 0.
|
||||
Only index 0 has the highest possible division score 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>You are given an integer <code>n</code> indicating there are <code>n</code> specialty retail stores. There are <code>m</code> product types of varying amounts, which are given as a <strong>0-indexed</strong> integer array <code>quantities</code>, where <code>quantities[i]</code> represents the number of products of the <code>i<sup>th</sup></code> product type.</p>
|
||||
|
||||
<p>You need to distribute <strong>all products</strong> to the retail stores following these rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>A store can only be given <strong>at most one product type</strong> but can be given <strong>any</strong> amount of it.</li>
|
||||
<li>After distribution, each store will have been given some number of products (possibly <code>0</code>). Let <code>x</code> represent the maximum number of products given to any store. You want <code>x</code> to be as small as possible, i.e., you want to <strong>minimize</strong> the <strong>maximum</strong> number of products that are given to any store.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum possible</em> <code>x</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6, quantities = [11,6]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> One optimal way is:
|
||||
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
|
||||
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
|
||||
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, quantities = [15,10,10]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> One optimal way is:
|
||||
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
|
||||
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
|
||||
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
|
||||
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, quantities = [100000]
|
||||
<strong>Output:</strong> 100000
|
||||
<strong>Explanation:</strong> The only optimal way is:
|
||||
- The 100000 products of type 0 are distributed to the only store.
|
||||
The maximum number of products given to any store is max(100000) = 100000.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == quantities.length</code></li>
|
||||
<li><code>1 <= m <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= quantities[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>'S'</code> and <code>'P'</code> where each <code>'S'</code> represents a seat and each <code>'P'</code> represents a plant.</p>
|
||||
|
||||
<p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 <= i <= n - 1</code>), at most one divider can be installed.</p>
|
||||
|
||||
<p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p>
|
||||
|
||||
<p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> corridor = "SSPPSPS"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 different ways to divide the corridor.
|
||||
The black bars in the above image indicate the two room dividers already installed.
|
||||
Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> corridor = "PPSPSP"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers.
|
||||
Installing any would create some section that does not have exactly two seats.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> corridor = "S"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == corridor.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>corridor[i]</code> is either <code>'S'</code> or <code>'P'</code>.</li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user