mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 03:11:42 +08:00
add leetcode problem-cn part5
This commit is contained in:
30
算法题(国内版)/problem (English)/01 矩阵(English) [01-matrix].html
Normal file
30
算法题(国内版)/problem (English)/01 矩阵(English) [01-matrix].html
Normal file
@@ -0,0 +1,30 @@
|
||||
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p>
|
||||
|
||||
<p>The distance between two adjacent cells is <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]]
|
||||
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]]
|
||||
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>There is at least one <code>0</code> in <code>mat</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given an array of <code>n</code> integers <code>nums</code>, a <strong>132 pattern</strong> is a subsequence of three integers <code>nums[i]</code>, <code>nums[j]</code> and <code>nums[k]</code> such that <code>i < j < k</code> and <code>nums[i] < nums[k] < nums[j]</code>.</p>
|
||||
|
||||
<p>Return <em><code>true</code> if there is a <strong>132 pattern</strong> in <code>nums</code>, otherwise, return <code>false</code>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> There is no 132 pattern in the sequence.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,4,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> There is a 132 pattern in the sequence: [1, 4, 2].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,3,2,0]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
42
算法题(国内版)/problem (English)/IPO(English) [ipo].html
Normal file
42
算法题(国内版)/problem (English)/IPO(English) [ipo].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>Suppose LeetCode will start its <strong>IPO</strong> soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the <strong>IPO</strong>. Since it has limited resources, it can only finish at most <code>k</code> distinct projects before the <strong>IPO</strong>. Help LeetCode design the best way to maximize its total capital after finishing at most <code>k</code> distinct projects.</p>
|
||||
|
||||
<p>You are given <code>n</code> projects where the <code>i<sup>th</sup></code> project has a pure profit <code>profits[i]</code> and a minimum capital of <code>capital[i]</code> is needed to start it.</p>
|
||||
|
||||
<p>Initially, you have <code>w</code> capital. When you finish a project, you will obtain its pure profit and the profit will be added to your total capital.</p>
|
||||
|
||||
<p>Pick a list of <strong>at most</strong> <code>k</code> distinct projects from given projects to <strong>maximize your final capital</strong>, and return <em>the final maximized capital</em>.</p>
|
||||
|
||||
<p>The answer is guaranteed to fit in a 32-bit signed integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Since your initial capital is 0, you can only start the project indexed 0.
|
||||
After finishing it you will obtain profit 1 and your capital becomes 1.
|
||||
With capital 1, you can either start the project indexed 1 or the project indexed 2.
|
||||
Since you can choose at most 2 projects, you need to finish the project indexed 2 to get the maximum capital.
|
||||
Therefore, output the final maximized capital, which is 0 + 1 + 3 = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
|
||||
<strong>Output:</strong> 6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= w <= 10<sup>9</sup></code></li>
|
||||
<li><code>n == profits.length</code></li>
|
||||
<li><code>n == capital.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= profits[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= capital[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
60
算法题(国内版)/problem (English)/LFU 缓存(English) [lfu-cache].html
Normal file
60
算法题(国内版)/problem (English)/LFU 缓存(English) [lfu-cache].html
Normal file
@@ -0,0 +1,60 @@
|
||||
<p>Design and implement a data structure for a <a href="https://en.wikipedia.org/wiki/Least_frequently_used" target="_blank">Least Frequently Used (LFU)</a> cache.</p>
|
||||
|
||||
<p>Implement the <code>LFUCache</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LFUCache(int capacity)</code> Initializes the object with the <code>capacity</code> of the data structure.</li>
|
||||
<li><code>int get(int key)</code> Gets the value of the <code>key</code> if the <code>key</code> exists in the cache. Otherwise, returns <code>-1</code>.</li>
|
||||
<li><code>void put(int key, int value)</code> Update the value of the <code>key</code> if present, or inserts the <code>key</code> if not already present. When the cache reaches its <code>capacity</code>, it should invalidate and remove the <strong>least frequently used</strong> key before inserting a new item. For this problem, when there is a <strong>tie</strong> (i.e., two or more keys with the same frequency), the <strong>least recently used</strong> <code>key</code> would be invalidated.</li>
|
||||
</ul>
|
||||
|
||||
<p>To determine the least frequently used key, a <strong>use counter</strong> is maintained for each key in the cache. The key with the smallest <strong>use counter</strong> is the least frequently used key.</p>
|
||||
|
||||
<p>When a key is first inserted into the cache, its <strong>use counter</strong> is set to <code>1</code> (due to the <code>put</code> operation). The <strong>use counter</strong> for a key in the cache is incremented either a <code>get</code> or <code>put</code> operation is called on it.</p>
|
||||
|
||||
<p>The functions <code data-stringify-type="code">get</code> and <code data-stringify-type="code">put</code> must each run in <code>O(1)</code> average time complexity.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
|
||||
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
// cnt(x) = the use counter for key x
|
||||
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
|
||||
LFUCache lfu = new LFUCache(2);
|
||||
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
|
||||
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
|
||||
lfu.get(1); // return 1
|
||||
// cache=[1,2], cnt(2)=1, cnt(1)=2
|
||||
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
|
||||
// cache=[3,1], cnt(3)=1, cnt(1)=2
|
||||
lfu.get(2); // return -1 (not found)
|
||||
lfu.get(3); // return 3
|
||||
// cache=[3,1], cnt(3)=2, cnt(1)=2
|
||||
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
|
||||
// cache=[4,3], cnt(4)=1, cnt(3)=2
|
||||
lfu.get(1); // return -1 (not found)
|
||||
lfu.get(3); // return 3
|
||||
// cache=[3,4], cnt(4)=1, cnt(3)=3
|
||||
lfu.get(4); // return 4
|
||||
// cache=[4,3], cnt(4)=2, cnt(3)=3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= capacity <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= key <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= value <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>2 * 10<sup>5</sup></code> calls will be made to <code>get</code> and <code>put</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<span style="display: none;"> </span>
|
@@ -0,0 +1,34 @@
|
||||
<blockquote>Note: This is a companion problem to the <a href="https://leetcode.com/discuss/interview-question/system-design/" target="_blank">System Design</a> problem: <a href="https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/" target="_blank">Design TinyURL</a>.</blockquote>
|
||||
|
||||
<p>TinyURL is a URL shortening service where you enter a URL such as <code>https://leetcode.com/problems/design-tinyurl</code> and it returns a short URL such as <code>http://tinyurl.com/4e9iAk</code>. Design a class to encode a URL and decode a tiny URL.</p>
|
||||
|
||||
<p>There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.</p>
|
||||
|
||||
<p>Implement the <code>Solution</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Solution()</code> Initializes the object of the system.</li>
|
||||
<li><code>String encode(String longUrl)</code> Returns a tiny URL for the given <code>longUrl</code>.</li>
|
||||
<li><code>String decode(String shortUrl)</code> Returns the original long URL for the given <code>shortUrl</code>. It is guaranteed that the given <code>shortUrl</code> was encoded by the same object.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> url = "https://leetcode.com/problems/design-tinyurl"
|
||||
<strong>Output:</strong> "https://leetcode.com/problems/design-tinyurl"
|
||||
|
||||
<strong>Explanation:</strong>
|
||||
Solution obj = new Solution();
|
||||
string tiny = obj.encode(url); // returns the encoded tiny url.
|
||||
string ans = obj.decode(tiny); // returns the original url after deconding it.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= url.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>url</code> is guranteed to be a valid URL.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p>
|
||||
|
||||
<p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>'s and </em><code>n</code><em> </em><code>1</code><em>'s in the subset</em>.</p>
|
||||
|
||||
<p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["10","0001","111001","1","0"], m = 5, n = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4.
|
||||
Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}.
|
||||
{"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> strs = ["10","0","1"], m = 1, n = 1
|
||||
<strong>Output:</strong> 2
|
||||
<b>Explanation:</b> The largest subset is {"0", "1"}, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= strs.length <= 600</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> consists only of digits <code>'0'</code> and <code>'1'</code>.</li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
</ul>
|
16
算法题(国内版)/problem (English)/七进制数(English) [base-7].html
Normal file
16
算法题(国内版)/problem (English)/七进制数(English) [base-7].html
Normal file
@@ -0,0 +1,16 @@
|
||||
<p>Given an integer <code>num</code>, return <em>a string of its <strong>base 7</strong> representation</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> num = 100
|
||||
<strong>Output:</strong> "202"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> num = -7
|
||||
<strong>Output:</strong> "-10"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-10<sup>7</sup> <= num <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>The <strong>next greater element</strong> of some element <code>x</code> in an array is the <strong>first greater</strong> element that is <strong>to the right</strong> of <code>x</code> in the same array.</p>
|
||||
|
||||
<p>You are given two <strong>distinct 0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, where <code>nums1</code> is a subset of <code>nums2</code>.</p>
|
||||
|
||||
<p>For each <code>0 <= i < nums1.length</code>, find the index <code>j</code> such that <code>nums1[i] == nums2[j]</code> and determine the <strong>next greater element</strong> of <code>nums2[j]</code> in <code>nums2</code>. If there is no next greater element, then the answer for this query is <code>-1</code>.</p>
|
||||
|
||||
<p>Return <em>an array </em><code>ans</code><em> of length </em><code>nums1.length</code><em> such that </em><code>ans[i]</code><em> is the <strong>next greater element</strong> as described above.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,1,2], nums2 = [1,3,4,2]
|
||||
<strong>Output:</strong> [-1,3,-1]
|
||||
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
|
||||
- 4 is underlined in nums2 = [1,3,<u>4</u>,2]. There is no next greater element, so the answer is -1.
|
||||
- 1 is underlined in nums2 = [<u>1</u>,3,4,2]. The next greater element is 3.
|
||||
- 2 is underlined in nums2 = [1,3,4,<u>2</u>]. There is no next greater element, so the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,4], nums2 = [1,2,3,4]
|
||||
<strong>Output:</strong> [3,-1]
|
||||
<strong>Explanation:</strong> The next greater element for each value of nums1 is as follows:
|
||||
- 2 is underlined in nums2 = [1,<u>2</u>,3,4]. The next greater element is 3.
|
||||
- 4 is underlined in nums2 = [1,2,3,<u>4</u>]. There is no next greater element, so the answer is -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li>All integers in <code>nums1</code> and <code>nums2</code> are <strong>unique</strong>.</li>
|
||||
<li>All the integers of <code>nums1</code> also appear in <code>nums2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow up:</strong> Could you find an <code>O(nums1.length + nums2.length)</code> solution?
|
@@ -0,0 +1,29 @@
|
||||
<p>Given a circular integer array <code>nums</code> (i.e., the next element of <code>nums[nums.length - 1]</code> is <code>nums[0]</code>), return <em>the <strong>next greater number</strong> for every element in</em> <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>next greater number</strong> of a number <code>x</code> is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn't exist, return <code>-1</code> for this number.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> [2,-1,2]
|
||||
Explanation: The first 1's next greater number is 2;
|
||||
The number 2 can't find next greater number.
|
||||
The second 1's next greater number needs to search circularly, which is also 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,3]
|
||||
<strong>Output:</strong> [2,3,4,-1,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.</p>
|
||||
|
||||
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg" style="width: 523px; height: 342px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [7,2,4,3], l2 = [5,6,4]
|
||||
<strong>Output:</strong> [7,8,0,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4]
|
||||
<strong>Output:</strong> [8,0,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [0], l2 = [0]
|
||||
<strong>Output:</strong> [0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 9</code></li>
|
||||
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Could you solve it without reversing the input lists?</p>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given the <code>root</code> of a binary search tree (BST) with duplicates, return <em>all the <a href="https://en.wikipedia.org/wiki/Mode_(statistics)" target="_blank">mode(s)</a> (i.e., the most frequently occurred element) in it</em>.</p>
|
||||
|
||||
<p>If the tree has more than one mode, return them in <strong>any order</strong>.</p>
|
||||
|
||||
<p>Assume a BST is defined as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>The left subtree of a node contains only nodes with keys <strong>less than or equal to</strong> the node's key.</li>
|
||||
<li>The right subtree of a node contains only nodes with keys <strong>greater than or equal to</strong> the node's key.</li>
|
||||
<li>Both the left and right subtrees must also be binary search trees.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/mode-tree.jpg" style="width: 142px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,2]
|
||||
<strong>Output:</strong> [2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0]
|
||||
<strong>Output:</strong> [0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow up:</strong> Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
|
@@ -0,0 +1,27 @@
|
||||
<p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,2,6,1,3]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
|
@@ -0,0 +1,29 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>the length of the <strong>diameter</strong> of the tree</em>.</p>
|
||||
|
||||
<p>The <strong>diameter</strong> of a binary tree is the <strong>length</strong> of the longest path between any two nodes in a tree. This path may or may not pass through the <code>root</code>.</p>
|
||||
|
||||
<p>The <strong>length</strong> of a path between two nodes is represented by the number of edges between them.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg" style="width: 292px; height: 302px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is the length of the path [4,2,1,3] or [5,2,1,3].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>You are given the <code>head</code> of a linked list, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the head of the linked list after <strong>swapping</strong> the values of the </em><code>k<sup>th</sup></code> <em>node from the beginning and the </em><code>k<sup>th</sup></code> <em>node from the end (the list is <strong>1-indexed</strong>).</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/linked1.jpg" style="width: 400px; height: 112px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,4,5], k = 2
|
||||
<strong>Output:</strong> [1,4,3,2,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [7,9,6,6,7,8,3,0,9,5], k = 5
|
||||
<strong>Output:</strong> [7,9,6,6,8,7,3,0,9,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the list is <code>n</code>.</li>
|
||||
<li><code>1 <= k <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= Node.val <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,18 @@
|
||||
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "owoztneoer"
|
||||
<strong>Output:</strong> "012"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "fviefuro"
|
||||
<strong>Output:</strong> "45"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is one of the characters <code>["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]</code>.</li>
|
||||
<li><code>s</code> is <strong>guaranteed</strong> to be valid.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 <= i <= n</code>), <strong>either</strong> of the following is true:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>perm[i]</code> is divisible by <code>i</code>.</li>
|
||||
<li><code>i</code> is divisible by <code>perm[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <em>the <strong>number</strong> of the <strong>beautiful arrangements</strong> that you can construct</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 2
|
||||
<b>Explanation:</b>
|
||||
The first beautiful arrangement is [1,2]:
|
||||
- perm[1] = 1 is divisible by i = 1
|
||||
- perm[2] = 2 is divisible by i = 2
|
||||
The second beautiful arrangement is [2,1]:
|
||||
- perm[1] = 2 is divisible by i = 1
|
||||
- i = 2 is divisible by perm[2] = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 15</code></li>
|
||||
</ul>
|
39
算法题(国内版)/problem (English)/供暖器(English) [heaters].html
Normal file
39
算法题(国内版)/problem (English)/供暖器(English) [heaters].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>Winter is coming! During the contest, your first job is to design a standard heater with a fixed warm radius to warm all the houses.</p>
|
||||
|
||||
<p>Every house can be warmed, as long as the house is within the heater's warm radius range. </p>
|
||||
|
||||
<p>Given the positions of <code>houses</code> and <code>heaters</code> on a horizontal line, return <em>the minimum radius standard of heaters so that those heaters could cover all houses.</em></p>
|
||||
|
||||
<p><strong>Notice</strong> that all the <code>heaters</code> follow your radius standard, and the warm radius will the same.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> houses = [1,2,3], heaters = [2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> houses = [1,2,3,4], heaters = [1,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> houses = [1,5], heaters = [2]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= houses.length, heaters.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= houses[i], heaters[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.</p>
|
||||
|
||||
<p>Implement the <code>AllOne</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>AllOne()</code> Initializes the object of the data structure.</li>
|
||||
<li><code>inc(String key)</code> Increments the count of the string <code>key</code> by <code>1</code>. If <code>key</code> does not exist in the data structure, insert it with count <code>1</code>.</li>
|
||||
<li><code>dec(String key)</code> Decrements the count of the string <code>key</code> by <code>1</code>. If the count of <code>key</code> is <code>0</code> after the decrement, remove it from the data structure. It is guaranteed that <code>key</code> exists in the data structure before the decrement.</li>
|
||||
<li><code>getMaxKey()</code> Returns one of the keys with the maximal count. If no element exists, return an empty string <code>""</code>.</li>
|
||||
<li><code>getMinKey()</code> Returns one of the keys with the minimum count. If no element exists, return an empty string <code>""</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"]
|
||||
[[], ["hello"], ["hello"], [], [], ["leet"], [], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, null, "hello", "hello", null, "hello", "leet"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
AllOne allOne = new AllOne();
|
||||
allOne.inc("hello");
|
||||
allOne.inc("hello");
|
||||
allOne.getMaxKey(); // return "hello"
|
||||
allOne.getMinKey(); // return "hello"
|
||||
allOne.inc("leet");
|
||||
allOne.getMaxKey(); // return "hello"
|
||||
allOne.getMinKey(); // return "leet"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= key.length <= 10</code></li>
|
||||
<li><code>key</code> consists of lowercase English letters.</li>
|
||||
<li>It is guaranteed that for each call to <code>dec</code>, <code>key</code> is existing in the data structure.</li>
|
||||
<li>At most <code>5 * 10<sup>4</sup></code> calls will be made to <code>inc</code>, <code>dec</code>, <code>getMaxKey</code>, and <code>getMinKey</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
|
||||
|
||||
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,2,-3]
|
||||
<strong>Output:</strong> [2,-3,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,2,-5]
|
||||
<strong>Output:</strong> [2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given a <strong>non-empty</strong> array <code>nums</code> containing <strong>only positive integers</strong>, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,11,5]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The array can be partitioned as [1, 5, 5] and [11].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,5]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The array cannot be partitioned into equal sum subsets.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.</p>
|
||||
|
||||
<p>Each child <code>i</code> has a greed factor <code>g[i]</code>, which is the minimum size of a cookie that the child will be content with; and each cookie <code>j</code> has a size <code>s[j]</code>. If <code>s[j] >= g[i]</code>, we can assign the cookie <code>j</code> to the child <code>i</code>, and the child <code>i</code> will be content. Your goal is to maximize the number of your content children and output the maximum number.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> g = [1,2,3], s = [1,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
|
||||
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
|
||||
You need to output 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> g = [1,2], s = [1,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
|
||||
You have 3 cookies and their sizes are big enough to gratify all of the children,
|
||||
You need to output 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= g.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.</p>
|
||||
|
||||
<p>Basically, the deletion can be divided into two stages:</p>
|
||||
|
||||
<ol>
|
||||
<li>Search for a node to remove.</li>
|
||||
<li>If the node is found, delete the node.</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px; height: 214px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 3
|
||||
<strong>Output:</strong> [5,4,6,2,null,null,7]
|
||||
<strong>Explanation:</strong> Given key to delete is 3. So we find the node with value 3 and delete it.
|
||||
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
|
||||
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px; height: 255px;" />
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,3,6,2,4,null,7], key = 0
|
||||
<strong>Output:</strong> [5,3,6,2,4,null,7]
|
||||
<strong>Explanation:</strong> The tree does not contain a node with value = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [], key = 0
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>Each node has a <strong>unique</strong> value.</li>
|
||||
<li><code>root</code> is a valid binary search tree.</li>
|
||||
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Could you solve it with time complexity <code>O(height of tree)</code>?</p>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given an array of characters <code>chars</code>, compress it using the following algorithm:</p>
|
||||
|
||||
<p>Begin with an empty string <code>s</code>. For each group of <strong>consecutive repeating characters</strong> in <code>chars</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If the group's length is <code>1</code>, append the character to <code>s</code>.</li>
|
||||
<li>Otherwise, append the character followed by the group's length.</li>
|
||||
</ul>
|
||||
|
||||
<p>The compressed string <code>s</code> <strong>should not be returned separately</strong>, but instead, be stored <strong>in the input character array <code>chars</code></strong>. Note that group lengths that are <code>10</code> or longer will be split into multiple characters in <code>chars</code>.</p>
|
||||
|
||||
<p>After you are done <strong>modifying the input array,</strong> return <em>the new length of the array</em>.</p>
|
||||
|
||||
<p>You must write an algorithm that uses only constant extra space.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> chars = ["a","a","b","b","c","c","c"]
|
||||
<strong>Output:</strong> Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
|
||||
<strong>Explanation:</strong> The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> chars = ["a"]
|
||||
<strong>Output:</strong> Return 1, and the first character of the input array should be: ["a"]
|
||||
<strong>Explanation:</strong> The only group is "a", which remains uncompressed since it's a single character.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
|
||||
<strong>Output:</strong> Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
|
||||
<strong>Explanation:</strong> The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= chars.length <= 2000</code></li>
|
||||
<li><code>chars[i]</code> is a lowercase English letter, uppercase English letter, digit, or symbol.</li>
|
||||
</ul>
|
@@ -0,0 +1,20 @@
|
||||
<p>Given a string <code>s</code> and an integer <code>k</code>, reverse the first <code>k</code> characters for every <code>2k</code> characters counting from the start of the string.</p>
|
||||
|
||||
<p>If there are fewer than <code>k</code> characters left, reverse all of them. If there are less than <code>2k</code> but greater than or equal to <code>k</code> characters, then reverse the first <code>k</code> characters and leave the other as original.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "abcdefg", k = 2
|
||||
<strong>Output:</strong> "bacdfeg"
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s = "abcd", k = 2
|
||||
<strong>Output:</strong> "bacd"
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of only lowercase English letters.</li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
32
算法题(国内版)/problem (English)/可怜的小猪(English) [poor-pigs].html
Normal file
32
算法题(国内版)/problem (English)/可怜的小猪(English) [poor-pigs].html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>There are <code>buckets</code> buckets of liquid, where <strong>exactly one</strong> of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have <code>minutesToTest</code> minutes to determine which bucket is poisonous.</p>
|
||||
|
||||
<p>You can feed the pigs according to these steps:</p>
|
||||
|
||||
<ol>
|
||||
<li>Choose some live pigs to feed.</li>
|
||||
<li>For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.</li>
|
||||
<li>Wait for <code>minutesToDie</code> minutes. You may <strong>not</strong> feed any other pigs during this time.</li>
|
||||
<li>After <code>minutesToDie</code> minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.</li>
|
||||
<li>Repeat this process until you run out of time.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given <code>buckets</code>, <code>minutesToDie</code>, and <code>minutesToTest</code>, return <em>the <strong>minimum</strong> number of pigs needed to figure out which bucket is poisonous within the allotted time</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> buckets = 1000, minutesToDie = 15, minutesToTest = 60
|
||||
<strong>Output:</strong> 5
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 15
|
||||
<strong>Output:</strong> 2
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> buckets = 4, minutesToDie = 15, minutesToTest = 30
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= buckets <= 1000</code></li>
|
||||
<li><code>1 <= minutesToDie <= minutesToTest <= 100</code></li>
|
||||
</ul>
|
37
算法题(国内版)/problem (English)/四数相加 II(English) [4sum-ii].html
Normal file
37
算法题(国内版)/problem (English)/四数相加 II(English) [4sum-ii].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>Given four integer arrays <code>nums1</code>, <code>nums2</code>, <code>nums3</code>, and <code>nums4</code> all of length <code>n</code>, return the number of tuples <code>(i, j, k, l)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j, k, l < n</code></li>
|
||||
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The two tuples are:
|
||||
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
|
||||
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>n == nums2.length</code></li>
|
||||
<li><code>n == nums3.length</code></li>
|
||||
<li><code>n == nums4.length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given <code>n</code> <code>points</code> in the plane that are all <strong>distinct</strong>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. A <strong>boomerang</strong> is a tuple of points <code>(i, j, k)</code> such that the distance between <code>i</code> and <code>j</code> equals the distance between <code>i</code> and <code>k</code> <strong>(the order of the tuple matters)</strong>.</p>
|
||||
|
||||
<p>Return <em>the number of boomerangs</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[0,0],[1,0],[2,0]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1]]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == points.length</code></li>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>All the points are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return <em>an array of the largest value in each row</em> of the tree <strong>(0-indexed)</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="width: 300px; height: 172px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,5,3,null,9]
|
||||
<strong>Output:</strong> [1,3,9]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3]
|
||||
<strong>Output:</strong> [1,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>A <a href="https://en.wikipedia.org/wiki/Complex_number" target="_blank">complex number</a> can be represented as a string on the form <code>"<strong>real</strong>+<strong>imaginary</strong>i"</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>real</code> is the real part and is an integer in the range <code>[-100, 100]</code>.</li>
|
||||
<li><code>imaginary</code> is the imaginary part and is an integer in the range <code>[-100, 100]</code>.</li>
|
||||
<li><code>i<sup>2</sup> == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given two complex numbers <code>num1</code> and <code>num2</code> as strings, return <em>a string of the complex number that represents their multiplications</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "1+1i", num2 = "1+1i"
|
||||
<strong>Output:</strong> "0+2i"
|
||||
<strong>Explanation:</strong> (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "1+-1i", num2 = "1+-1i"
|
||||
<strong>Output:</strong> "0+-2i"
|
||||
<strong>Explanation:</strong> (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code> and <code>num2</code> are valid complex numbers.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>There is an <code>m x n</code> rectangular island that borders both the <strong>Pacific Ocean</strong> and <strong>Atlantic Ocean</strong>. The <strong>Pacific Ocean</strong> touches the island's left and top edges, and the <strong>Atlantic Ocean</strong> touches the island's right and bottom edges.</p>
|
||||
|
||||
<p>The island is partitioned into a grid of square cells. You are given an <code>m x n</code> integer matrix <code>heights</code> where <code>heights[r][c]</code> represents the <strong>height above sea level</strong> of the cell at coordinate <code>(r, c)</code>.</p>
|
||||
|
||||
<p>The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is <strong>less than or equal to</strong> the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.</p>
|
||||
|
||||
<p>Return <em>a <strong>2D list</strong> of grid coordinates </em><code>result</code><em> where </em><code>result[i] = [r<sub>i</sub>, c<sub>i</sub>]</code><em> denotes that rain water can flow from cell </em><code>(r<sub>i</sub>, c<sub>i</sub>)</code><em> to <strong>both</strong> the Pacific and Atlantic oceans</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg" style="width: 573px; height: 573px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
|
||||
<strong>Output:</strong> [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> heights = [[2,1],[1,2]]
|
||||
<strong>Output:</strong> [[0,0],[0,1],[1,0],[1,1]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == heights.length</code></li>
|
||||
<li><code>n == heights[r].length</code></li>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>0 <= heights[r][c] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given two integers <code>n</code> and <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>lexicographically smallest integer in the range</em> <code>[1, n]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 13, k = 2
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, k = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given a string <code>s</code>, return <em>the number of segments in the string</em>.</p>
|
||||
|
||||
<p>A <strong>segment</strong> is defined to be a contiguous sequence of <strong>non-space characters</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "Hello, my name is John"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The five segments are ["Hello,", "my", "name", "is", "John"]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "Hello"
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 300</code></li>
|
||||
<li><code>s</code> consists of lowercase and uppercase English letters, digits, or one of the following characters <code>"!@#$%^&*()_+-=',.:"</code>.</li>
|
||||
<li>The only space character in <code>s</code> is <code>' '</code>.</li>
|
||||
</ul>
|
34
算法题(国内版)/problem (English)/字符串相加(English) [add-strings].html
Normal file
34
算法题(国内版)/problem (English)/字符串相加(English) [add-strings].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>Given two non-negative integers, <code>num1</code> and <code>num2</code> represented as string, return <em>the sum of</em> <code>num1</code> <em>and</em> <code>num2</code> <em>as a string</em>.</p>
|
||||
|
||||
<p>You must solve the problem without using any built-in library for handling large integers (such as <code>BigInteger</code>). You must also not convert the inputs to integers directly.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "11", num2 = "123"
|
||||
<strong>Output:</strong> "134"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "456", num2 = "77"
|
||||
<strong>Output:</strong> "533"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = "0", num2 = "0"
|
||||
<strong>Output:</strong> "0"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1.length, num2.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>num1</code> and <code>num2</code> consist of only digits.</li>
|
||||
<li><code>num1</code> and <code>num2</code> don't have any leading zeros except for the zero itself.</li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>A <a href="https://en.wikipedia.org/wiki/Perfect_number" target="_blank"><strong>perfect number</strong></a> is a <strong>positive integer</strong> that is equal to the sum of its <strong>positive divisors</strong>, excluding the number itself. A <strong>divisor</strong> of an integer <code>x</code> is an integer that can divide <code>x</code> evenly.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <code>true</code><em> if </em><code>n</code><em> is a perfect number, otherwise return </em><code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 28
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 28 = 1 + 2 + 4 + 7 + 14
|
||||
1, 2, 4, 7, and 14 are all divisors of 28.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 7
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a license key represented as a string <code>s</code> that consists of only alphanumeric characters and dashes. The string is separated into <code>n + 1</code> groups by <code>n</code> dashes. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>We want to reformat the string <code>s</code> such that each group contains exactly <code>k</code> characters, except for the first group, which could be shorter than <code>k</code> but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.</p>
|
||||
|
||||
<p>Return <em>the reformatted license key</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "5F3Z-2e-9-w", k = 4
|
||||
<strong>Output:</strong> "5F3Z-2E9W"
|
||||
<strong>Explanation:</strong> The string s has been split into two parts, each part has 4 characters.
|
||||
Note that the two extra dashes are not needed and can be removed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "2-5g-3-J", k = 2
|
||||
<strong>Output:</strong> "2-5G-3J"
|
||||
<strong>Explanation:</strong> The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of English letters, digits, and dashes <code>'-'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>Given an <code>m x n</code> matrix <code>mat</code>, return <em>an array of all the elements of the array in a diagonal order</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>Output:</strong> [1,2,4,7,5,3,6,8,9]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,2],[3,4]]
|
||||
<strong>Output:</strong> [1,2,3,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= mat[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given an array of <code>intervals</code>, where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> and each <code>start<sub>i</sub></code> is <strong>unique</strong>.</p>
|
||||
|
||||
<p>The <strong>right interval</strong> for an interval <code>i</code> is an interval <code>j</code> such that <code>start<sub>j</sub> >= end<sub>i</sub></code> and <code>start<sub>j</sub></code> is <strong>minimized</strong>. Note that <code>i</code> may equal <code>j</code>.</p>
|
||||
|
||||
<p>Return <em>an array of <strong>right interval</strong> indices for each interval <code>i</code></em>. If no <strong>right interval</strong> exists for interval <code>i</code>, then put <code>-1</code> at index <code>i</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,2]]
|
||||
<strong>Output:</strong> [-1]
|
||||
<strong>Explanation:</strong> There is only one interval in the collection, so it outputs -1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[3,4],[2,3],[1,2]]
|
||||
<strong>Output:</strong> [-1,0,1]
|
||||
<strong>Explanation:</strong> There is no right interval for [3,4].
|
||||
The right interval for [2,3] is [3,4] since start<sub>0</sub> = 3 is the smallest start that is >= end<sub>1</sub> = 3.
|
||||
The right interval for [1,2] is [2,3] since start<sub>1</sub> = 2 is the smallest start that is >= end<sub>2</sub> = 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,4],[2,3],[3,4]]
|
||||
<strong>Output:</strong> [-1,2,-1]
|
||||
<strong>Explanation:</strong> There is no right interval for [1,4] and [3,4].
|
||||
The right interval for [2,3] is [3,4] since start<sub>2</sub> = 3 is the smallest start that is >= end<sub>1</sub> = 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>-10<sup>6</sup> <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>The start point of each interval is <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given <code>row x col</code> <code>grid</code> representing a map where <code>grid[i][j] = 1</code> represents land and <code>grid[i][j] = 0</code> represents water.</p>
|
||||
|
||||
<p>Grid cells are connected <strong>horizontally/vertically</strong> (not diagonally). The <code>grid</code> is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).</p>
|
||||
|
||||
<p>The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong> The perimeter is the 16 yellow stripes in the image above.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1]]
|
||||
<strong>Output:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,0]]
|
||||
<strong>Output:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>row == grid.length</code></li>
|
||||
<li><code>col == grid[i].length</code></li>
|
||||
<li><code>1 <= row, col <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
|
||||
<li>There is exactly one island in <code>grid</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,22 @@
|
||||
<p>Serialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.</p>
|
||||
|
||||
<p>Design an algorithm to serialize and deserialize a <b>binary search tree</b>. There is no restriction on how your serialization/deserialization algorithm should work. You need to ensure that a binary search tree can be serialized to a string, and this string can be deserialized to the original tree structure.</p>
|
||||
|
||||
<p><b>The encoded string should be as compact as possible.</b></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> root = [2,1,3]
|
||||
<strong>Output:</strong> [2,1,3]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> root = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>The input tree is <strong>guaranteed</strong> to be a binary search tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>A password is considered strong if the below conditions are all met:</p>
|
||||
|
||||
<ul>
|
||||
<li>It has at least <code>6</code> characters and at most <code>20</code> characters.</li>
|
||||
<li>It contains at least <strong>one lowercase</strong> letter, at least <strong>one uppercase</strong> letter, and at least <strong>one digit</strong>.</li>
|
||||
<li>It does not contain three repeating characters in a row (i.e., <code>"...aaa..."</code> is weak, but <code>"...aa...a..."</code> is strong, assuming other conditions are met).</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>password</code>, return <em>the minimum number of steps required to make <code>password</code> strong. if <code>password</code> is already strong, return <code>0</code>.</em></p>
|
||||
|
||||
<p>In one step, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Insert one character to <code>password</code>,</li>
|
||||
<li>Delete one character from <code>password</code>, or</li>
|
||||
<li>Replace one character of <code>password</code> with another character.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> password = "a"
|
||||
<strong>Output:</strong> 5
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> password = "aA1"
|
||||
<strong>Output:</strong> 3
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> password = "1337C0d3"
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= password.length <= 50</code></li>
|
||||
<li><code>password</code> consists of letters, digits, dot <code>'.'</code> or exclamation mark <code>'!'</code>.</li>
|
||||
</ul>
|
43
算法题(国内版)/problem (English)/我能赢吗(English) [can-i-win].html
Normal file
43
算法题(国内版)/problem (English)/我能赢吗(English) [can-i-win].html
Normal file
@@ -0,0 +1,43 @@
|
||||
<p>In the "100 game" two players take turns adding, to a running total, any integer from <code>1</code> to <code>10</code>. The player who first causes the running total to <strong>reach or exceed</strong> 100 wins.</p>
|
||||
|
||||
<p>What if we change the game so that players <strong>cannot</strong> re-use integers?</p>
|
||||
|
||||
<p>For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100.</p>
|
||||
|
||||
<p>Given two integers <code>maxChoosableInteger</code> and <code>desiredTotal</code>, return <code>true</code> if the first player to move can force a win, otherwise, return <code>false</code>. Assume both players play <strong>optimally</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 11
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
No matter which integer the first player choose, the first player will lose.
|
||||
The first player can choose an integer from 1 up to 10.
|
||||
If the first player choose 1, the second player can only choose integers from 2 up to 10.
|
||||
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
|
||||
Same with other integers chosen by the first player, the second player will always win.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 0
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> maxChoosableInteger = 10, desiredTotal = 1
|
||||
<strong>Output:</strong> true
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= maxChoosableInteger <= 20</code></li>
|
||||
<li><code>0 <= desiredTotal <= 300</code></li>
|
||||
</ul>
|
51
算法题(国内版)/problem (English)/扫雷游戏(English) [minesweeper].html
Normal file
51
算法题(国内版)/problem (English)/扫雷游戏(English) [minesweeper].html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>Let's play the minesweeper game (<a href="https://en.wikipedia.org/wiki/Minesweeper_(video_game)" target="_blank">Wikipedia</a>, <a href="http://minesweeperonline.com" target="_blank">online game</a>)!</p>
|
||||
|
||||
<p>You are given an <code>m x n</code> char matrix <code>board</code> representing the game board where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'M'</code> represents an unrevealed mine,</li>
|
||||
<li><code>'E'</code> represents an unrevealed empty square,</li>
|
||||
<li><code>'B'</code> represents a revealed blank square that has no adjacent mines (i.e., above, below, left, right, and all 4 diagonals),</li>
|
||||
<li>digit (<code>'1'</code> to <code>'8'</code>) represents how many mines are adjacent to this revealed square, and</li>
|
||||
<li><code>'X'</code> represents a revealed mine.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer array <code>click</code> where <code>click = [click<sub>r</sub>, click<sub>c</sub>]</code> represents the next click position among all the unrevealed squares (<code>'M'</code> or <code>'E'</code>).</p>
|
||||
|
||||
<p>Return <em>the board after revealing this position according to the following rules</em>:</p>
|
||||
|
||||
<ol>
|
||||
<li>If a mine <code>'M'</code> is revealed, then the game is over. You should change it to <code>'X'</code>.</li>
|
||||
<li>If an empty square <code>'E'</code> with no adjacent mines is revealed, then change it to a revealed blank <code>'B'</code> and all of its adjacent unrevealed squares should be revealed recursively.</li>
|
||||
<li>If an empty square <code>'E'</code> with at least one adjacent mine is revealed, then change it to a digit (<code>'1'</code> to <code>'8'</code>) representing the number of adjacent mines.</li>
|
||||
<li>Return the board when no more squares will be revealed.</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_1.png" style="width: 500px; max-width: 400px; height: 269px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
|
||||
<strong>Output:</strong> [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_2.png" style="width: 500px; max-width: 400px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
|
||||
<strong>Output:</strong> [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n == board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>board[i][j]</code> is either <code>'M'</code>, <code>'E'</code>, <code>'B'</code>, or a digit from <code>'1'</code> to <code>'8'</code>.</li>
|
||||
<li><code>click.length == 2</code></li>
|
||||
<li><code>0 <= click<sub>r</sub> < m</code></li>
|
||||
<li><code>0 <= click<sub>c</sub> < n</code></li>
|
||||
<li><code>board[click<sub>r</sub>][click<sub>c</sub>]</code> is either <code>'M'</code> or <code>'E'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given two strings <code>s</code> and <code>p</code>, return <em>an array of all the start indices of </em><code>p</code><em>'s anagrams in </em><code>s</code>. You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbaebabacd", p = "abc"
|
||||
<strong>Output:</strong> [0,6]
|
||||
<strong>Explanation:</strong>
|
||||
The substring with start index = 0 is "cba", which is an anagram of "abc".
|
||||
The substring with start index = 6 is "bac", which is an anagram of "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abab", p = "ab"
|
||||
<strong>Output:</strong> [0,1,2]
|
||||
<strong>Explanation:</strong>
|
||||
The substring with start index = 0 is "ab", which is an anagram of "ab".
|
||||
The substring with start index = 1 is "ba", which is an anagram of "ab".
|
||||
The substring with start index = 2 is "ab", which is an anagram of "ab".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> and <code>p</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,21 @@
|
||||
<p>Given an array <code>nums</code> of <code>n</code> integers where <code>nums[i]</code> is in the range <code>[1, n]</code>, return <em>an array of all the integers in the range</em> <code>[1, n]</code> <em>that do not appear in</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [4,3,2,7,8,2,3,1]
|
||||
<strong>Output:</strong> [5,6]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> [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>1 <= nums[i] <= n</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Could you do it without extra space and in <code>O(n)</code> runtime? You may assume the returned list does not count as extra space.</p>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [2,1,3]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
|
||||
<strong>Output:</strong> 7
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>Given the <code>root</code> of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.</p>
|
||||
|
||||
<p>As a reminder, a <em>binary search tree</em> is a tree that satisfies these constraints:</p>
|
||||
|
||||
<ul>
|
||||
<li>The left subtree of a node contains only nodes with keys <strong>less than</strong> the node's key.</li>
|
||||
<li>The right subtree of a node contains only nodes with keys <strong>greater than</strong> the node's key.</li>
|
||||
<li>Both the left and right subtrees must also be binary search trees.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/02/tree.png" style="width: 500px; height: 341px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
|
||||
<strong>Output:</strong> [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [0,null,1]
|
||||
<strong>Output:</strong> [1,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>All the values in the tree are <strong>unique</strong>.</li>
|
||||
<li><code>root</code> is guaranteed to be a valid binary search tree.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 1038: <a href="https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/" target="_blank">https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/</a></p>
|
@@ -0,0 +1,27 @@
|
||||
<p>You have <code>n</code> coins and you want to build a staircase with these coins. The staircase consists of <code>k</code> rows where the <code>i<sup>th</sup></code> row has exactly <code>i</code> coins. The last row of the staircase <strong>may be</strong> incomplete.</p>
|
||||
|
||||
<p>Given the integer <code>n</code>, return <em>the number of <strong>complete rows</strong> of the staircase you will build</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg" style="width: 253px; height: 253px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Because the 3<sup>rd</sup> row is incomplete, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg" style="width: 333px; height: 333px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 8
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Because the 4<sup>th</sup> row is incomplete, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly <code>duration</code> seconds. More formally, an attack at second <code>t</code> will mean Ashe is poisoned during the <strong>inclusive</strong> time interval <code>[t, t + duration - 1]</code>. If Teemo attacks again <strong>before</strong> the poison effect ends, the timer for it is <strong>reset</strong>, and the poison effect will end <code>duration</code> seconds after the new attack.</p>
|
||||
|
||||
<p>You are given a <strong>non-decreasing</strong> integer array <code>timeSeries</code>, where <code>timeSeries[i]</code> denotes that Teemo attacks Ashe at second <code>timeSeries[i]</code>, and an integer <code>duration</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>total</strong> number of seconds that Ashe is poisoned</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> timeSeries = [1,4], duration = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
|
||||
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
|
||||
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
|
||||
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> timeSeries = [1,2], duration = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Teemo's attacks on Ashe go as follows:
|
||||
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
|
||||
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
|
||||
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
|
||||
<li><code>timeSeries</code> is sorted in <strong>non-decreasing</strong> order.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>The <strong>complement</strong> of an integer is the integer you get when you flip all the <code>0</code>'s to <code>1</code>'s and all the <code>1</code>'s to <code>0</code>'s in its binary representation.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, The integer <code>5</code> is <code>"101"</code> in binary and its <strong>complement</strong> is <code>"010"</code> which is the integer <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given an integer <code>num</code>, return <em>its complement</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num < 2<sup>31</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 1009: <a href="https://leetcode.com/problems/complement-of-base-10-integer/" target="_blank">https://leetcode.com/problems/complement-of-base-10-integer/</a></p>
|
@@ -0,0 +1,25 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 <= i <= j < n</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,10,5,25,2,8]
|
||||
<strong>Output:</strong> 28
|
||||
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
|
||||
<strong>Output:</strong> 127
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
|
||||
|
||||
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j < nums.length</code></li>
|
||||
<li><code>i != j</code></li>
|
||||
<li><code>nums[i] - nums[j] == k</code></li>
|
||||
</ul>
|
||||
|
||||
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
|
||||
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given an integer array <code>nums</code> of length <code>n</code> where all the integers of <code>nums</code> are in the range <code>[1, n]</code> and each integer appears <strong>once</strong> or <strong>twice</strong>, return <em>an array of all the integers that appears <strong>twice</strong></em>.</p>
|
||||
|
||||
<p>You must write an algorithm that runs in <code>O(n) </code>time and uses only constant extra space.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [4,3,2,7,8,2,3,1]
|
||||
<strong>Output:</strong> [2,3]
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,1,2]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre><p><strong>Example 3:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1]
|
||||
<strong>Output:</strong> []
|
||||
</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>1 <= nums[i] <= n</code></li>
|
||||
<li>Each element in <code>nums</code> appears <strong>once</strong> or <strong>twice</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>Given an array of intervals <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, return <em>the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> [1,3] can be removed and the rest of the intervals are non-overlapping.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,2],[1,2],[1,2]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You need to remove two [1,2] to make the rest of the intervals non-overlapping.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> intervals = [[1,2],[2,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You don't need to remove any of the intervals since they're already non-overlapping.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>-5 * 10<sup>4</sup> <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p>
|
||||
|
||||
<p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ABAB", k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Replace the two 'A's with two 'B's or vice versa.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AABABBA", k = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Replace the one 'A' in the middle with 'B' and form "AABBBBA".
|
||||
The substring "BBBB" has the longest repeating letters, which is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only uppercase English letters.</li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>Given an integer n, return <em>the <strong>largest palindromic integer</strong> that can be represented as the product of two <code>n</code>-digits integers</em>. Since the answer can be very large, return it <strong>modulo</strong> <code>1337</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 987
|
||||
Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
<p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,0,1,1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,0,1,1,0,1]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 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,46 @@
|
||||
<p>A gene string can be represented by an 8-character long string, with choices from <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
|
||||
|
||||
<p>Suppose we need to investigate a mutation from a gene string <code>start</code> to a gene string <code>end</code> where one mutation is defined as one single character changed in the gene string.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"AACCGGTT" --> "AACCGGTA"</code> is one mutation.</li>
|
||||
</ul>
|
||||
|
||||
<p>There is also a gene bank <code>bank</code> that records all the valid gene mutations. A gene must be in <code>bank</code> to make it a valid gene string.</p>
|
||||
|
||||
<p>Given the two gene strings <code>start</code> and <code>end</code> and the gene bank <code>bank</code>, return <em>the minimum number of mutations needed to mutate from </em><code>start</code><em> to </em><code>end</code>. If there is no such a mutation, return <code>-1</code>.</p>
|
||||
|
||||
<p>Note that the starting point is assumed to be valid, so it might not be included in the bank.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>start.length == 8</code></li>
|
||||
<li><code>end.length == 8</code></li>
|
||||
<li><code>0 <= bank.length <= 10</code></li>
|
||||
<li><code>bank[i].length == 8</code></li>
|
||||
<li><code>start</code>, <code>end</code>, and <code>bank[i]</code> consist of only the characters <code>['A', 'C', 'G', 'T']</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>Given an integer <code>n</code> represented as a string, return <em>the smallest <strong>good base</strong> of</em> <code>n</code>.</p>
|
||||
|
||||
<p>We call <code>k >= 2</code> a <strong>good base</strong> of <code>n</code>, if all digits of <code>n</code> base <code>k</code> are <code>1</code>'s.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "13"
|
||||
<strong>Output:</strong> "3"
|
||||
<strong>Explanation:</strong> 13 base 3 is 111.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "4681"
|
||||
<strong>Output:</strong> "8"
|
||||
<strong>Explanation:</strong> 4681 base 8 is 11111.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = "1000000000000000000"
|
||||
<strong>Output:</strong> "999999999999999999"
|
||||
<strong>Explanation:</strong> 1000000000000000000 base 999999999999999999 is 11.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n</code> is an integer in the range <code>[3, 10<sup>18</sup>]</code>.</li>
|
||||
<li><code>n</code> does not contain any leading zeros.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
|
||||
|
||||
<p>In one move, you can increment <code>n - 1</code> elements of the array by <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Only three moves are needed (remember each move increments two elements):
|
||||
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li>The answer is guaranteed to fit in a <strong>32-bit</strong> integer.</li>
|
||||
</ul>
|
@@ -0,0 +1,16 @@
|
||||
Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
|
||||
<strong>Output:</strong> 1
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>
|
||||
|
||||
<p>In one move, you can increment or decrement an element of the array by <code>1</code>.</p>
|
||||
|
||||
<p>Test cases are designed so that the answer will fit in a <strong>32-bit</strong> integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Only two moves are needed (remember each move increments or decrements one element):
|
||||
[<u>1</u>,2,3] => [2,2,<u>3</u>] => [2,2,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,10,2,9]
|
||||
<strong>Output:</strong> 16
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given a string <code>s</code>, find <em>the longest palindromic <strong>subsequence</strong>'s length in</em> <code>s</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> s = "bbbab"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bbbb".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbbd"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One possible longest palindromic subsequence is "bb".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given an array of strings <code>strs</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between them</em>. If the longest uncommon subsequence does not exist, return <code>-1</code>.</p>
|
||||
|
||||
<p>An <strong>uncommon subsequence</strong> between an array of strings is a string that is a <strong>subsequence of one string but not the others</strong>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of a string <code>s</code> is a string that can be obtained after deleting any number of characters from <code>s</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"abc"</code> is a subsequence of <code>"aebdc"</code> because you can delete the underlined characters in <code>"a<u>e</u>b<u>d</u>c"</code> to get <code>"abc"</code>. Other subsequences of <code>"aebdc"</code> include <code>"aebdc"</code>, <code>"aeb"</code>, and <code>""</code> (empty string).</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> strs = ["aba","cdc","eae"]
|
||||
<strong>Output:</strong> 3
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> strs = ["aaa","aaa","aa"]
|
||||
<strong>Output:</strong> -1
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= strs.length <= 50</code></li>
|
||||
<li><code>1 <= strs[i].length <= 10</code></li>
|
||||
<li><code>strs[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>Given two strings <code>a</code> and <code>b</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between </em><code>a</code> <em>and</em> <code>b</code>. If the longest uncommon subsequence does not exist, return <code>-1</code>.</p>
|
||||
|
||||
<p>An <strong>uncommon subsequence</strong> between two strings is a string that is a <strong>subsequence of one but not the other</strong>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> of a string <code>s</code> is a string that can be obtained after deleting any number of characters from <code>s</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>"abc"</code> is a subsequence of <code>"aebdc"</code> because you can delete the underlined characters in <code>"a<u>e</u>b<u>d</u>c"</code> to get <code>"abc"</code>. Other subsequences of <code>"aebdc"</code> include <code>"aebdc"</code>, <code>"aeb"</code>, and <code>""</code> (empty string).</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "aba", b = "cdc"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> One longest uncommon subsequence is "aba" because "aba" is a subsequence of "aba" but not "cdc".
|
||||
Note that "cdc" is also a longest uncommon subsequence.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "aaa", b = "bbb"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The longest uncommon subsequences are "aaa" and "bbb".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = "aaa", b = "aaa"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Every subsequence of string a is also a subsequence of string b. Similarly, every subsequence of string b is also a subsequence of string a.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a.length, b.length <= 100</code></li>
|
||||
<li><code>a</code> and <code>b</code> consist of lower-case English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,21 @@
|
||||
<p>You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.</p>
|
||||
|
||||
<p>Return <em>the single element that appears only once</em>.</p>
|
||||
|
||||
<p>Your solution must run in <code>O(log n)</code> time and <code>O(1)</code> space.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,1,2,3,3,4,4,8,8]
|
||||
<strong>Output:</strong> 2
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [3,3,7,7,10,11,11]
|
||||
<strong>Output:</strong> 10
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:</p>
|
||||
|
||||
<ol>
|
||||
<li>The area of the rectangular web page you designed must equal to the given target area.</li>
|
||||
<li>The width <code>W</code> should not be larger than the length <code>L</code>, which means <code>L >= W</code>.</li>
|
||||
<li>The difference between length <code>L</code> and width <code>W</code> should be as small as possible.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>an array <code>[L, W]</code> where <code>L</code> and <code>W</code> are the length and width of the web page you designed in sequence.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> area = 4
|
||||
<strong>Output:</strong> [2,2]
|
||||
<strong>Explanation:</strong> The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
|
||||
But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> area = 37
|
||||
<strong>Output:</strong> [37,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> area = 122122
|
||||
<strong>Output:</strong> [427,286]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= area <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a string <code>s</code>, sort it in <strong>decreasing order</strong> based on the <strong>frequency</strong> of the characters. The <strong>frequency</strong> of a character is the number of times it appears in the string.</p>
|
||||
|
||||
<p>Return <em>the sorted string</em>. If there are multiple answers, return <em>any of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "tree"
|
||||
<strong>Output:</strong> "eert"
|
||||
<strong>Explanation:</strong> 'e' appears twice while 'r' and 't' both appear once.
|
||||
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cccaaa"
|
||||
<strong>Output:</strong> "aaaccc"
|
||||
<strong>Explanation:</strong> Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
|
||||
Note that "cacaca" is incorrect, as the same characters must be together.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "Aabb"
|
||||
<strong>Output:</strong> "bbAa"
|
||||
<strong>Explanation:</strong> "bbaA" is also a valid answer, but "Aabb" is incorrect.
|
||||
Note that 'A' and 'a' are treated as two different characters.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of uppercase and lowercase English letters and digits.</li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
<p>We define the usage of capitals in a word to be right when one of the following cases holds:</p>
|
||||
|
||||
<ul>
|
||||
<li>All letters in this word are capitals, like <code>"USA"</code>.</li>
|
||||
<li>All letters in this word are not capitals, like <code>"leetcode"</code>.</li>
|
||||
<li>Only the first letter in this word is capital, like <code>"Google"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>word</code>, return <code>true</code> if the usage of capitals in it is right.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> word = "USA"
|
||||
<strong>Output:</strong> true
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> word = "FlaG"
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists of lowercase and uppercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p>
|
||||
|
||||
<p>Given two integers <code>x</code> and <code>y</code>, return <em>the <strong>Hamming distance</strong> between them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 1, y = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1 (0 0 0 1)
|
||||
4 (0 1 0 0)
|
||||
↑ ↑
|
||||
The above arrows point to positions where the corresponding bits are different.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 3, y = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= x, y <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p>
|
||||
|
||||
<p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,14,2]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
|
||||
showing the four bits relevant in this case).
|
||||
The answer will be:
|
||||
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,14,4]
|
||||
<strong>Output:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p>
|
||||
|
||||
<ul>
|
||||
<li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li>
|
||||
<li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. There is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>
|
||||
|
||||
<p>Return <em>the median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
|
||||
<strong>Output:</strong> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
|
||||
<strong>Explanation:</strong>
|
||||
Window position Median
|
||||
--------------- -----
|
||||
[<strong>1 3 -1</strong>] -3 5 3 6 7 1
|
||||
1 [<strong>3 -1 -3</strong>] 5 3 6 7 -1
|
||||
1 3 [<strong>-1 -3 5</strong>] 3 6 7 -1
|
||||
1 3 -1 [<strong>-3 5 3</strong>] 6 7 3
|
||||
1 3 -1 -3 [<strong>5 3 6</strong>] 7 5
|
||||
1 3 -1 -3 5 [<strong>3 6 7</strong>] 6
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3
|
||||
<strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>You are given an integer array <code>matchsticks</code> where <code>matchsticks[i]</code> is the length of the <code>i<sup>th</sup></code> matchstick. You want to use <strong>all the matchsticks</strong> to make one square. You <strong>should not break</strong> any stick, but you can link them up, and each matchstick must be used <strong>exactly one time</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> if you can make this square and <code>false</code> otherwise.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" style="width: 253px; height: 253px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matchsticks = [1,1,2,2,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> You can form a square with length 2, one side of the square came two sticks with length 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matchsticks = [3,3,3,3,4]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> You cannot find a way to form a square with all the matchsticks.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matchsticks.length <= 15</code></li>
|
||||
<li><code>1 <= matchsticks[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>You are playing a game involving a <strong>circular</strong> array of non-zero integers <code>nums</code>. Each <code>nums[i]</code> denotes the number of indices forward/backward you must move if you are located at index <code>i</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i]</code> is positive, move <code>nums[i]</code> steps <strong>forward</strong>, and</li>
|
||||
<li>If <code>nums[i]</code> is negative, move <code>nums[i]</code> steps <strong>backward</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Since the array is <strong>circular</strong>, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element.</p>
|
||||
|
||||
<p>A <strong>cycle</strong> in the array consists of a sequence of indices <code>seq</code> of length <code>k</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li>Following the movement rules above results in the repeating index sequence <code>seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...</code></li>
|
||||
<li>Every <code>nums[seq[j]]</code> is either <strong>all positive</strong> or <strong>all negative</strong>.</li>
|
||||
<li><code>k > 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <code>true</code><em> if there is a <strong>cycle</strong> in </em><code>nums</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-1,1,2,2]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong>
|
||||
There is a cycle from index 0 -> 2 -> 3 -> 0 -> ...
|
||||
The cycle's length is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
The sequence from index 1 -> 1 -> 1 -> ... is not a cycle because the sequence's length is 1.
|
||||
By definition the sequence's length must be strictly greater than 1 to be a cycle.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,1,-1,-2,-2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong>
|
||||
The sequence from index 1 -> 2 -> 1 -> ... is not a cycle because nums[1] is positive, but nums[2] is negative.
|
||||
Every nums[seq[j]] must be either all positive or all negative.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Could you solve it in <code>O(n)</code> time complexity and <code>O(1)</code> extra space complexity?</p>
|
@@ -0,0 +1,40 @@
|
||||
<p>We define the string <code>s</code> to be the infinite wraparound string of <code>"abcdefghijklmnopqrstuvwxyz"</code>, so <code>s</code> will look like this:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>p</code>, return <em>the number of <strong>unique non-empty substrings</strong> of </em><code>p</code><em> are present in </em><code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> p = "a"
|
||||
<strong>Output:</strong> 1
|
||||
Explanation: Only the substring "a" of p is in s.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> p = "cac"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two substrings ("a", "c") of p in s.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> p = "zab"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> There are six substrings ("z", "a", "b", "za", "ab", and "zab") of p in s.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= p.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>p</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array <code>points</code> where <code>points[i] = [x<sub>start</sub>, x<sub>end</sub>]</code> denotes a balloon whose <strong>horizontal diameter</strong> stretches between <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code>. You do not know the exact y-coordinates of the balloons.</p>
|
||||
|
||||
<p>Arrows can be shot up <strong>directly vertically</strong> (in the positive y-direction) from different points along the x-axis. A balloon with <code>x<sub>start</sub></code> and <code>x<sub>end</sub></code> is <strong>burst</strong> by an arrow shot at <code>x</code> if <code>x<sub>start</sub> <= x <= x<sub>end</sub></code>. There is <strong>no limit</strong> to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.</p>
|
||||
|
||||
<p>Given the array <code>points</code>, return <em>the <strong>minimum</strong> number of arrows that must be shot to burst all balloons</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[10,16],[2,8],[1,6],[7,12]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
|
||||
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
|
||||
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,2],[3,4],[5,6],[7,8]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One arrow needs to be shot for each balloon for a total of 4 arrows.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,2],[2,3],[3,4],[4,5]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The balloons can be burst by 2 arrows:
|
||||
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
|
||||
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-2<sup>31</sup> <= x<sub>start</sub> < x<sub>end</sub> <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>Given an <code>m x n</code> matrix <code>board</code> where each cell is a battleship <code>'X'</code> or empty <code>'.'</code>, return <em>the number of the <strong>battleships</strong> on</em> <code>board</code>.</p>
|
||||
|
||||
<p><strong>Battleships</strong> can only be placed horizontally or vertically on <code>board</code>. In other words, they can only be made of the shape <code>1 x k</code> (<code>1</code> row, <code>k</code> columns) or <code>k x 1</code> (<code>k</code> rows, <code>1</code> column), where <code>k</code> can be of any size. At least one horizontal or vertical cell separates between two battleships (i.e., there are no adjacent battleships).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/battelship-grid.jpg" style="width: 333px; height: 333px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> board = [["."]]
|
||||
<strong>Output:</strong> 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n == board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>board[i][j]</code> is either <code>'.'</code> or <code>'X'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong> Could you do it in one-pass, using only <code>O(1)</code> extra memory and without modifying the values <code>board</code>?</p>
|
40
算法题(国内版)/problem (English)/目标和(English) [target-sum].html
Normal file
40
算法题(国内版)/problem (English)/目标和(English) [target-sum].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>
|
||||
|
||||
<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>'+'</code> and <code>'-'</code> before each integer in nums and then concatenate all the integers.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>'+'</code> before <code>2</code> and a <code>'-'</code> before <code>1</code> and concatenate them to build the expression <code>"+2-1"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> There are 5 ways to assign symbols to make the sum of nums be target 3.
|
||||
-1 + 1 + 1 + 1 + 1 = 3
|
||||
+1 - 1 + 1 + 1 + 1 = 3
|
||||
+1 + 1 - 1 + 1 + 1 = 3
|
||||
+1 + 1 + 1 - 1 + 1 = 3
|
||||
+1 + 1 + 1 + 1 - 1 = 3
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1], target = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= sum(nums[i]) <= 1000</code></li>
|
||||
<li><code>-1000 <= target <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given an integer array <code>score</code> of size <code>n</code>, where <code>score[i]</code> is the score of the <code>i<sup>th</sup></code> athlete in a competition. All the scores are guaranteed to be <strong>unique</strong>.</p>
|
||||
|
||||
<p>The athletes are <strong>placed</strong> based on their scores, where the <code>1<sup>st</sup></code> place athlete has the highest score, the <code>2<sup>nd</sup></code> place athlete has the <code>2<sup>nd</sup></code> highest score, and so on. The placement of each athlete determines their rank:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>1<sup>st</sup></code> place athlete's rank is <code>"Gold Medal"</code>.</li>
|
||||
<li>The <code>2<sup>nd</sup></code> place athlete's rank is <code>"Silver Medal"</code>.</li>
|
||||
<li>The <code>3<sup>rd</sup></code> place athlete's rank is <code>"Bronze Medal"</code>.</li>
|
||||
<li>For the <code>4<sup>th</sup></code> place to the <code>n<sup>th</sup></code> place athlete, their rank is their placement number (i.e., the <code>x<sup>th</sup></code> place athlete's rank is <code>"x"</code>).</li>
|
||||
</ul>
|
||||
|
||||
<p>Return an array <code>answer</code> of size <code>n</code> where <code>answer[i]</code> is the <strong>rank</strong> of the <code>i<sup>th</sup></code> athlete.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> score = [5,4,3,2,1]
|
||||
<strong>Output:</strong> ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
|
||||
<strong>Explanation:</strong> The placements are [1<sup>st</sup>, 2<sup>nd</sup>, 3<sup>rd</sup>, 4<sup>th</sup>, 5<sup>th</sup>].</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> score = [10,3,8,9,4]
|
||||
<strong>Output:</strong> ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
|
||||
<strong>Explanation:</strong> The placements are [1<sup>st</sup>, 5<sup>th</sup>, 3<sup>rd</sup>, 2<sup>nd</sup>, 4<sup>th</sup>].
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == score.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= score[i] <= 10<sup>6</sup></code></li>
|
||||
<li>All the values in <code>score</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>There are <code>n</code> cities. Some of them are connected, while some are not. If city <code>a</code> is connected directly with city <code>b</code>, and city <code>b</code> is connected directly with city <code>c</code>, then city <code>a</code> is connected indirectly with city <code>c</code>.</p>
|
||||
|
||||
<p>A <strong>province</strong> is a group of directly or indirectly connected cities and no other cities outside of the group.</p>
|
||||
|
||||
<p>You are given an <code>n x n</code> matrix <code>isConnected</code> where <code>isConnected[i][j] = 1</code> if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and <code>isConnected[i][j] = 0</code> otherwise.</p>
|
||||
|
||||
<p>Return <em>the total number of <strong>provinces</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> isConnected = [[1,1,0],[1,1,0],[0,0,1]]
|
||||
<strong>Output:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> isConnected = [[1,0,0],[0,1,0],[0,0,1]]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>n == isConnected.length</code></li>
|
||||
<li><code>n == isConnected[i].length</code></li>
|
||||
<li><code>isConnected[i][j]</code> is <code>1</code> or <code>0</code>.</li>
|
||||
<li><code>isConnected[i][i] == 1</code></li>
|
||||
<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
|
||||
</ul>
|
61
算法题(国内版)/problem (English)/祖玛游戏(English) [zuma-game].html
Normal file
61
算法题(国内版)/problem (English)/祖玛游戏(English) [zuma-game].html
Normal file
@@ -0,0 +1,61 @@
|
||||
<p>You are playing a variation of the game Zuma.</p>
|
||||
|
||||
<p>In this variation of Zuma, there is a <strong>single row</strong> of colored balls on a board, where each ball can be colored red <code>'R'</code>, yellow <code>'Y'</code>, blue <code>'B'</code>, green <code>'G'</code>, or white <code>'W'</code>. You also have several colored balls in your hand.</p>
|
||||
|
||||
<p>Your goal is to <strong>clear all</strong> of the balls from the board. On each turn:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick <strong>any</strong> ball from your hand and insert it in between two balls in the row or on either end of the row.</li>
|
||||
<li>If there is a group of <strong>three or more consecutive balls</strong> of the <strong>same color</strong>, remove the group of balls from the board.
|
||||
<ul>
|
||||
<li>If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If there are no more balls on the board, then you win the game.</li>
|
||||
<li>Repeat this process until you either win or do not have any more balls in your hand.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given a string <code>board</code>, representing the row of balls on the board, and a string <code>hand</code>, representing the balls in your hand, return <em>the <strong>minimum</strong> number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return </em><code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> board = "WRRBBW", hand = "RB"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible to clear all the balls. The best you can do is:
|
||||
- Insert 'R' so the board becomes WRR<u>R</u>BBW. W<u>RRR</u>BBW -> WBBW.
|
||||
- Insert 'B' so the board becomes WBB<u>B</u>W. W<u>BBB</u>W -> WW.
|
||||
There are still balls remaining on the board, and you are out of balls to insert.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> board = "WWRRBBWW", hand = "WRBRW"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> To make the board empty:
|
||||
- Insert 'R' so the board becomes WWRR<u>R</u>BBWW. WW<u>RRR</u>BBWW -> WWBBWW.
|
||||
- Insert 'B' so the board becomes WWBB<u>B</u>WW. WW<u>BBB</u>WW -> <u>WWWW</u> -> empty.
|
||||
2 balls from your hand were needed to clear the board.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> board = "G", hand = "GGGGG"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> To make the board empty:
|
||||
- Insert 'G' so the board becomes G<u>G</u>.
|
||||
- Insert 'G' so the board becomes GG<u>G</u>. <u>GGG</u> -> empty.
|
||||
2 balls from your hand were needed to clear the board.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= board.length <= 16</code></li>
|
||||
<li><code>1 <= hand.length <= 5</code></li>
|
||||
<li><code>board</code> and <code>hand</code> consist of the characters <code>'R'</code>, <code>'Y'</code>, <code>'B'</code>, <code>'G'</code>, and <code>'W'</code>.</li>
|
||||
<li>The initial row of balls on the board will <strong>not</strong> have any groups of three or more consecutive balls of the same color.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>A magical string <code>s</code> consists of only <code>'1'</code> and <code>'2'</code> and obeys the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The string s is magical because concatenating the number of contiguous occurrences of characters <code>'1'</code> and <code>'2'</code> generates the string <code>s</code> itself.</li>
|
||||
</ul>
|
||||
|
||||
<p>The first few elements of <code>s</code> is <code>s = "1221121221221121122……"</code>. If we group the consecutive <code>1</code>'s and <code>2</code>'s in <code>s</code>, it will be <code>"1 22 11 2 1 22 1 22 11 2 11 22 ......"</code> and the occurrences of <code>1</code>'s or <code>2</code>'s in each group are <code>"1 2 2 1 1 2 1 2 2 1 2 2 ......"</code>. You can see that the occurrence sequence is <code>s</code> itself.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return the number of <code>1</code>'s in the first <code>n</code> number in the magical string <code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 6
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The first 6 elements of magical string s is "122112" and it contains three 1's, so return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
41
算法题(国内版)/problem (English)/移除盒子(English) [remove-boxes].html
Normal file
41
算法题(国内版)/problem (English)/移除盒子(English) [remove-boxes].html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>You are given several <code>boxes</code> with different colors represented by different positive numbers.</p>
|
||||
|
||||
<p>You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (i.e., composed of <code>k</code> boxes, <code>k >= 1</code>), remove them and get <code>k * k</code> points.</p>
|
||||
|
||||
<p>Return <em>the maximum points you can get</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [1,3,2,2,2,3,4,3,1]
|
||||
<strong>Output:</strong> 23
|
||||
<strong>Explanation:</strong>
|
||||
[1, 3, 2, 2, 2, 3, 4, 3, 1]
|
||||
----> [1, 3, 3, 4, 3, 1] (3*3=9 points)
|
||||
----> [1, 3, 3, 3, 1] (1*1=1 points)
|
||||
----> [1, 1] (3*3=9 points)
|
||||
----> [] (2*2=4 points)
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [1,1,1]
|
||||
<strong>Output:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> boxes = [1]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxes.length <= 100</code></li>
|
||||
<li><code>1 <= boxes[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>Bob is standing at cell <code>(0, 0)</code>, and he wants to reach <code>destination</code>: <code>(row, column)</code>. He can only travel <strong>right</strong> and <strong>down</strong>. You are going to help Bob by providing <strong>instructions</strong> for him to reach <code>destination</code>.</p>
|
||||
|
||||
<p>The <strong>instructions</strong> are represented as a string, where each character is either:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'H'</code>, meaning move horizontally (go <strong>right</strong>), or</li>
|
||||
<li><code>'V'</code>, meaning move vertically (go <strong>down</strong>).</li>
|
||||
</ul>
|
||||
|
||||
<p>Multiple <strong>instructions</strong> will lead Bob to <code>destination</code>. For example, if <code>destination</code> is <code>(2, 3)</code>, both <code>"HHHVV"</code> and <code>"HVHVH"</code> are valid <strong>instructions</strong>.</p>
|
||||
|
||||
<p>However, Bob is very picky. Bob has a lucky number <code>k</code>, and he wants the <code>k<sup>th</sup></code> <strong>lexicographically smallest instructions</strong> that will lead him to <code>destination</code>. <code>k</code> is <strong>1-indexed</strong>.</p>
|
||||
|
||||
<p>Given an integer array <code>destination</code> and an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code><em> <strong>lexicographically smallest instructions</strong> that will take Bob to </em><code>destination</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex1.png" style="width: 300px; height: 229px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> destination = [2,3], k = 1
|
||||
<strong>Output:</strong> "HHHVV"
|
||||
<strong>Explanation:</strong> All the instructions that reach (2, 3) in lexicographic order are as follows:
|
||||
["HHHVV", "HHVHV", "HHVVH", "HVHHV", "HVHVH", "HVVHH", "VHHHV", "VHHVH", "VHVHH", "VVHHH"].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex2.png" style="width: 300px; height: 229px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> destination = [2,3], k = 2
|
||||
<strong>Output:</strong> "HHVHV"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/10/12/ex3.png" style="width: 300px; height: 229px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> destination = [2,3], k = 3
|
||||
<strong>Output:</strong> "HHVVH"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>destination.length == 2</code></li>
|
||||
<li><code>1 <= row, column <= 15</code></li>
|
||||
<li><code>1 <= k <= nCr(row + column, row)</code>, where <code>nCr(a, b)</code> denotes <code>a</code> choose <code>b</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The first distinct maximum is 3.
|
||||
The second distinct maximum is 2.
|
||||
The third distinct maximum is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The first distinct maximum is 2.
|
||||
The second distinct maximum is 1.
|
||||
The third distinct maximum does not exist, so the maximum (2) is returned instead.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,3,1]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The first distinct maximum is 3.
|
||||
The second distinct maximum is 2 (both 2's are counted together since they have the same value).
|
||||
The third distinct maximum is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow up:</strong> Can you find an <code>O(n)</code> solution?
|
@@ -0,0 +1,48 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of all the <strong>arithmetic subsequences</strong> of</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A sequence of numbers is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[1, 3, 5, 7, 9]</code>, <code>[7, 7, 7, 7]</code>, and <code>[3, -1, -5, -9]</code> are arithmetic sequences.</li>
|
||||
<li>For example, <code>[1, 1, 2, 5, 7]</code> is not an arithmetic sequence.</li>
|
||||
</ul>
|
||||
|
||||
<p>A <strong>subsequence</strong> of an array is a sequence that can be formed by removing some elements (possibly none) of the array.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[2,5,10]</code> is a subsequence of <code>[1,2,1,<strong><u>2</u></strong>,4,1,<u><strong>5</strong></u>,<u><strong>10</strong></u>]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The test cases are generated so that the answer fits in <strong>32-bit</strong> integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,6,8,10]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> All arithmetic subsequence slices are:
|
||||
[2,4,6]
|
||||
[4,6,8]
|
||||
[6,8,10]
|
||||
[2,4,6,8]
|
||||
[4,6,8,10]
|
||||
[2,4,6,8,10]
|
||||
[2,6,10]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,7,7,7,7]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong> Any subsequence of this array is arithmetic.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>We define <code>str = [s, n]</code> as the string <code>str</code> which consists of the string <code>s</code> concatenated <code>n</code> times.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>str == ["abc", 3] =="abcabcabc"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>We define that string <code>s1</code> can be obtained from string <code>s2</code> if we can remove some characters from <code>s2</code> such that it becomes <code>s1</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>s1 = "abc"</code> can be obtained from <code>s2 = "ab<strong><u>dbe</u></strong>c"</code> based on our definition by removing the bolded underlined characters.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are given two strings <code>s1</code> and <code>s2</code> and two integers <code>n1</code> and <code>n2</code>. You have the two strings <code>str1 = [s1, n1]</code> and <code>str2 = [s2, n2]</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum integer </em><code>m</code><em> such that </em><code>str = [str2, m]</code><em> can be obtained from </em><code>str1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
|
||||
<strong>Output:</strong> 2
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 100</code></li>
|
||||
<li><code>s1</code> and <code>s2</code> consist of lowercase English letters.</li>
|
||||
<li><code>1 <= n1, n2 <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
19
算法题(国内版)/problem (English)/翻转对(English) [reverse-pairs].html
Normal file
19
算法题(国内版)/problem (English)/翻转对(English) [reverse-pairs].html
Normal file
@@ -0,0 +1,19 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of <strong>reverse pairs</strong> in the array</em>.</p>
|
||||
|
||||
<p>A reverse pair is a pair <code>(i, j)</code> where <code>0 <= i < j < nums.length</code> and <code>nums[i] > 2 * nums[j]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [1,3,2,3,1]
|
||||
<strong>Output:</strong> 2
|
||||
</pre><p><strong>Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> nums = [2,4,3,5,1]
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>In the video game Fallout 4, the quest <strong>"Road to Freedom"</strong> requires players to reach a metal dial called the <strong>"Freedom Trail Ring"</strong> and use the dial to spell a specific keyword to open the door.</p>
|
||||
|
||||
<p>Given a string <code>ring</code> that represents the code engraved on the outer ring and another string <code>key</code> that represents the keyword that needs to be spelled, return <em>the minimum number of steps to spell all the characters in the keyword</em>.</p>
|
||||
|
||||
<p>Initially, the first character of the ring is aligned at the <code>"12:00"</code> direction. You should spell all the characters in <code>key</code> one by one by rotating <code>ring</code> clockwise or anticlockwise to make each character of the string key aligned at the <code>"12:00"</code> direction and then by pressing the center button.</p>
|
||||
|
||||
<p>At the stage of rotating the ring to spell the key character <code>key[i]</code>:</p>
|
||||
|
||||
<ol>
|
||||
<li>You can rotate the ring clockwise or anticlockwise by one place, which counts as <strong>one step</strong>. The final purpose of the rotation is to align one of <code>ring</code>'s characters at the <code>"12:00"</code> direction, where this character must equal <code>key[i]</code>.</li>
|
||||
<li>If the character <code>key[i]</code> has been aligned at the <code>"12:00"</code> direction, press the center button to spell, which also counts as <strong>one step</strong>. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="width: 450px; height: 450px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> ring = "godding", key = "gd"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
For the first key character 'g', since it is already in place, we just need 1 step to spell this character.
|
||||
For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo".
|
||||
Also, we need 1 more step for spelling.
|
||||
So the final output is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ring = "godding", key = "godding"
|
||||
<strong>Output:</strong> 13
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ring.length, key.length <= 100</code></li>
|
||||
<li><code>ring</code> and <code>key</code> consist of only lower case English letters.</li>
|
||||
<li>It is guaranteed that <code>key</code> could always be spelled by rotating <code>ring</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You have <code>n</code> super washing machines on a line. Initially, each washing machine has some dresses or is empty.</p>
|
||||
|
||||
<p>For each move, you could choose any <code>m</code> (<code>1 <= m <= n</code>) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.</p>
|
||||
|
||||
<p>Given an integer array <code>machines</code> representing the number of dresses in each washing machine from left to right on the line, return <em>the minimum number of moves to make all the washing machines have the same number of dresses</em>. If it is not possible to do it, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> machines = [1,0,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
1st move: 1 0 <-- 5 => 1 1 4
|
||||
2nd move: 1 <-- 1 <-- 4 => 2 1 3
|
||||
3rd move: 2 1 <-- 3 => 2 2 2
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> machines = [0,3,0]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1st move: 0 <-- 3 0 => 1 2 0
|
||||
2nd move: 1 2 --> 0 => 1 1 1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> machines = [0,2,0]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong>
|
||||
It's impossible to make all three washing machines have the same number of dresses.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == machines.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given the <code>root</code> of a binary tree and an integer <code>targetSum</code>, return <em>the number of paths where the sum of the values along the path equals</em> <code>targetSum</code>.</p>
|
||||
|
||||
<p>The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg" style="width: 450px; height: 386px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The paths that sum to 8 are shown.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
|
||||
<strong>Output:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[0, 1000]</code>.</li>
|
||||
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
|
||||
<li><code>-1000 <= targetSum <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>Given an array of strings <code>words</code> (<strong>without duplicates</strong>), return <em>all the <strong>concatenated words</strong> in the given list of</em> <code>words</code>.</p>
|
||||
|
||||
<p>A <strong>concatenated word</strong> is defined as a string that is comprised entirely of at least two shorter words in the given array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
|
||||
<strong>Output:</strong> ["catsdogcats","dogcatsdog","ratcatdogcat"]
|
||||
<strong>Explanation:</strong> "catsdogcats" can be concatenated by "cats", "dog" and "cats";
|
||||
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
|
||||
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cat","dog","catdog"]
|
||||
<strong>Output:</strong> ["catdog"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= words[i].length <= 30</code></li>
|
||||
<li><code>words[i]</code> consists of only lowercase English letters.</li>
|
||||
<li><code>0 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,0]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 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,38 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> <em>if </em><code>nums</code><em> has a continuous subarray of size <strong>at least two</strong> whose elements sum up to a multiple of</em> <code>k</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
|
||||
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given an integer array <code>nums</code>, return all the different possible increasing subsequences of the given array with <strong>at least two elements</strong>. You may return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p>The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,6,7,7]
|
||||
<strong>Output:</strong> [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,3,2,1]
|
||||
<strong>Output:</strong> [[4,4]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 15</code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
|
||||
<strong>Output:</strong> "apple"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abpcplea", dictionary = ["a","b","c"]
|
||||
<strong>Output:</strong> "a"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>1 <= dictionary.length <= 1000</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 1000</code></li>
|
||||
<li><code>s</code> and <code>dictionary[i]</code> consist of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given a string <code>s</code>, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abab"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> It is the substring "ab" twice.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aba"
|
||||
<strong>Output:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabcabcabc"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> It is the substring "abc" four times or the substring "abcabc" twice.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
40
算法题(国内版)/problem (English)/键盘行(English) [keyboard-row].html
Normal file
40
算法题(国内版)/problem (English)/键盘行(English) [keyboard-row].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>Given an array of strings <code>words</code>, return <em>the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below</em>.</p>
|
||||
|
||||
<p>In the <strong>American keyboard</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>the first row consists of the characters <code>"qwertyuiop"</code>,</li>
|
||||
<li>the second row consists of the characters <code>"asdfghjkl"</code>, and</li>
|
||||
<li>the third row consists of the characters <code>"zxcvbnm"</code>.</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/keyboard.png" style="width: 800px; max-width: 600px; height: 267px;" />
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["Hello","Alaska","Dad","Peace"]
|
||||
<strong>Output:</strong> ["Alaska","Dad"]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["omk"]
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["adsdf","sfd"]
|
||||
<strong>Output:</strong> ["adsdf","sfd"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 20</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> consists of English letters (both lowercase and uppercase). </li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
|
||||
|
||||
<p>Return <em>the number of combinations that make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>0</code>.</p>
|
||||
|
||||
<p>You may assume that you have an infinite number of each kind of coin.</p>
|
||||
|
||||
<p>The answer is <strong>guaranteed</strong> to fit into a signed <strong>32-bit</strong> integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = 5, coins = [1,2,5]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> there are four ways to make up the amount:
|
||||
5=5
|
||||
5=2+2+1
|
||||
5=2+1+1+1
|
||||
5=1+1+1+1+1
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = 3, coins = [2]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> the amount of 3 cannot be made up just with coins of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> amount = 10, coins = [10]
|
||||
<strong>Output:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= coins.length <= 300</code></li>
|
||||
<li><code>1 <= coins[i] <= 5000</code></li>
|
||||
<li>All the values of <code>coins</code> are <strong>unique</strong>.</li>
|
||||
<li><code>0 <= amount <= 5000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an integer array <code>nums</code>. Two players are playing a game with this array: player 1 and player 2.</p>
|
||||
|
||||
<p>Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of <code>0</code>. At each turn, the player takes one of the numbers from either end of the array (i.e., <code>nums[0]</code> or <code>nums[nums.length - 1]</code>) which reduces the size of the array by <code>1</code>. The player adds the chosen number to their score. The game ends when there are no more elements in the array.</p>
|
||||
|
||||
<p>Return <code>true</code> if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return <code>true</code>. You may assume that both players are playing optimally.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,2]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Initially, player 1 can choose between 1 and 2.
|
||||
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
|
||||
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
|
||||
Hence, player 1 will never be the winner and you need to return false.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,233,7]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Player 1 first chooses 1. Then player 2 has to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
|
||||
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>Given a string <code>queryIP</code>, return <code>"IPv4"</code> if IP is a valid IPv4 address, <code>"IPv6"</code> if IP is a valid IPv6 address or <code>"Neither"</code> if IP is not a correct IP of any type.</p>
|
||||
|
||||
<p><strong>A valid IPv4</strong> address is an IP in the form <code>"x<sub>1</sub>.x<sub>2</sub>.x<sub>3</sub>.x<sub>4</sub>"</code> where <code>0 <= x<sub>i</sub> <= 255</code> and <code>x<sub>i</sub></code> <strong>cannot contain</strong> leading zeros. For example, <code>"192.168.1.1"</code> and <code>"192.168.1.0"</code> are valid IPv4 addresses but <code>"192.168.01.1"</code>, while <code>"192.168.1.00"</code> and <code>"192.168@1.1"</code> are invalid IPv4 addresses.</p>
|
||||
|
||||
<p><strong>A valid IPv6</strong> address is an IP in the form <code>"x<sub>1</sub>:x<sub>2</sub>:x<sub>3</sub>:x<sub>4</sub>:x<sub>5</sub>:x<sub>6</sub>:x<sub>7</sub>:x<sub>8</sub>"</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x<sub>i</sub>.length <= 4</code></li>
|
||||
<li><code>x<sub>i</sub></code> is a <strong>hexadecimal string</strong> which may contain digits, lower-case English letter (<code>'a'</code> to <code>'f'</code>) and upper-case English letters (<code>'A'</code> to <code>'F'</code>).</li>
|
||||
<li>Leading zeros are allowed in <code>x<sub>i</sub></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, "<code>2001:0db8:85a3:0000:0000:8a2e:0370:7334"</code> and "<code>2001:db8:85a3:0:0:8A2E:0370:7334"</code> are valid IPv6 addresses, while "<code>2001:0db8:85a3::8A2E:037j:7334"</code> and "<code>02001:0db8:85a3:0000:0000:8a2e:0370:7334"</code> are invalid IPv6 addresses.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> queryIP = "172.16.254.1"
|
||||
<strong>Output:</strong> "IPv4"
|
||||
<strong>Explanation:</strong> This is a valid IPv4 address, return "IPv4".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
|
||||
<strong>Output:</strong> "IPv6"
|
||||
<strong>Explanation:</strong> This is a valid IPv6 address, return "IPv6".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> queryIP = "256.256.256.256"
|
||||
<strong>Output:</strong> "Neither"
|
||||
<strong>Explanation:</strong> This is neither a IPv4 address nor a IPv6 address.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>queryIP</code> consists only of English letters, digits and the characters <code>'.'</code> and <code>':'</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user