mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-03 06:22:54 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
161
leetcode/originData/apply-operations-to-make-string-empty.json
Normal file
161
leetcode/originData/apply-operations-to-make-string-empty.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/count-prefix-and-suffix-pairs-i.json
Normal file
161
leetcode/originData/count-prefix-and-suffix-pairs-i.json
Normal file
File diff suppressed because one or more lines are too long
163
leetcode/originData/count-prefix-and-suffix-pairs-ii.json
Normal file
163
leetcode/originData/count-prefix-and-suffix-pairs-ii.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
202
leetcode/originData/maximum-palindromes-after-operations.json
Normal file
202
leetcode/originData/maximum-palindromes-after-operations.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode/originData/modify-the-matrix.json
Normal file
171
leetcode/originData/modify-the-matrix.json
Normal file
File diff suppressed because one or more lines are too long
160
leetcode/originData/most-frequent-prime.json
Normal file
160
leetcode/originData/most-frequent-prime.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/type-of-triangle.json
Normal file
180
leetcode/originData/type-of-triangle.json
Normal file
File diff suppressed because one or more lines are too long
44
leetcode/problem/apply-operations-to-make-string-empty.html
Normal file
44
leetcode/problem/apply-operations-to-make-string-empty.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>You are given a string <code>s</code>.</p>
|
||||
|
||||
<p>Consider performing the following operation until <code>s</code> becomes <strong>empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>For <strong>every</strong> alphabet character from <code>'a'</code> to <code>'z'</code>, remove the <strong>first</strong> occurrence of that character in <code>s</code> (if it exists).</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, let initially <code>s = "aabcbbca"</code>. We do the following operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>a</strong></u>a<strong><u>bc</u></strong>bbca"</code>. The resulting string is <code>s = "abbca"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ab</strong></u>b<u><strong>c</strong></u>a"</code>. The resulting string is <code>s = "ba"</code>.</li>
|
||||
<li>Remove the underlined characters <code>s = "<u><strong>ba</strong></u>"</code>. The resulting string is <code>s = ""</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the value of the string </em><code>s</code><em> right <strong>before</strong> applying the <strong>last</strong> operation</em>. In the example above, answer is <code>"ba"</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabcbbca"
|
||||
<strong>Output:</strong> "ba"
|
||||
<strong>Explanation:</strong> Explained in the statement.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd"
|
||||
<strong>Output:</strong> "abcd"
|
||||
<strong>Explanation:</strong> We do the following operation:
|
||||
- Remove the underlined characters s = "<u><strong>abcd</strong></u>". The resulting string is s = "".
|
||||
The string just before the last operation is "abcd".
|
||||
</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 only of lowercase English letters.</li>
|
||||
</ul>
|
51
leetcode/problem/count-prefix-and-suffix-pairs-i.html
Normal file
51
leetcode/problem/count-prefix-and-suffix-pairs-i.html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i, j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 50</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
52
leetcode/problem/count-prefix-and-suffix-pairs-ii.html
Normal file
52
leetcode/problem/count-prefix-and-suffix-pairs-ii.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
|
||||
|
||||
<p>Let's define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, <code>isPrefixAndSuffix("aba", "ababa")</code> is <code>true</code> because <code>"aba"</code> is a prefix of <code>"ababa"</code> and also a suffix, but <code>isPrefixAndSuffix("abc", "abcd")</code> is <code>false</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i<em>, </em>j)</code><em> such that </em><code>i < j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["a","aba","ababa","aa"]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
|
||||
i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
|
||||
i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
|
||||
i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
|
||||
Therefore, the answer is 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["pa","papa","ma","mama"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In this example, the counted index pairs are:
|
||||
i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
|
||||
i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
|
||||
Therefore, the answer is 2. </pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abab","ab"]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
|
||||
Therefore, the answer is 0.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= words[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
<li>The sum of the lengths of all <code>words[i]</code> does not exceed <code>5 * 10<sup>5</sup></code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p>
|
||||
|
||||
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p>
|
||||
|
||||
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have a common prefix <code>565</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p>
|
||||
|
||||
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]):
|
||||
- The longest common prefix of (1, 1000) is 1.
|
||||
- The longest common prefix of (10, 1000) is 10.
|
||||
- The longest common prefix of (100, 1000) is 100.
|
||||
The longest common prefix is 100 with a length of 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
|
||||
Note that common prefixes between elements of the same array do not count.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr1[i], arr2[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>Initially, you can increase the value of <strong>any</strong> element in the array by <strong>at most</strong> <code>1</code>.</p>
|
||||
|
||||
<p>After that, you need to select <strong>one or more</strong> elements from the final array such that those elements are <strong>consecutive</strong> when sorted in increasing order. For example, the elements <code>[3, 4, 5]</code> are consecutive while <code>[3, 4, 6]</code> and <code>[1, 1, 2, 3]</code> are not.<!-- notionvc: 312f8c5d-40d0-4cd1-96cc-9e96a846735b --></p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of elements that you can select</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,5,1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can increase the elements at indices 0 and 3. The resulting array is nums = [3,1,5,2,1].
|
||||
We select the elements [<u><strong>3</strong></u>,<u><strong>1</strong></u>,5,<u><strong>2</strong></u>,1] and we sort them to obtain [1,2,3], which are consecutive.
|
||||
It can be shown that we cannot select more than 3 consecutive elements.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,7,10]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The maximum consecutive elements that we can select is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,4,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
|
||||
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
|
||||
We are unable to perform any more operations as nums contain only 1 element.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>Given an array of integers called <code>nums</code>, you can perform <strong>any</strong> of the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the first two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the last two elements of <code>nums</code> and delete them.</li>
|
||||
<li>Choose the first and the last elements of <code>nums</code> and delete them.</li>
|
||||
</ul>
|
||||
|
||||
<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
|
||||
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
|
||||
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
|
||||
We are unable to perform any more operations as nums is empty.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,6,1,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We perform the following operations:
|
||||
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
|
||||
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
|
||||
It can be proven that we can perform at most 2 operations.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
53
leetcode/problem/maximum-palindromes-after-operations.html
Normal file
53
leetcode/problem/maximum-palindromes-after-operations.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string array <code>words</code> having length <code>n</code> and containing <strong>0-indexed</strong> strings.</p>
|
||||
|
||||
<p>You are allowed to perform the following operation <strong>any</strong> number of times (<strong>including</strong> <strong>zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose integers <code>i</code>, <code>j</code>, <code>x</code>, and <code>y</code> such that <code>0 <= i, j < n</code>, <code>0 <= x < words[i].length</code>, <code>0 <= y < words[j].length</code>, and <strong>swap</strong> the characters <code>words[i][x]</code> and <code>words[j][y]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>maximum</strong> number of <span data-keyword="palindrome-string">palindromes</span> </em><code>words</code><em> can contain, after performing some operations.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> <code>i</code> and <code>j</code> may be equal during an operation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abbb","ba","aa"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"].
|
||||
All strings in words are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 3.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["abc","ab"]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>In this example, one way to get the maximum number of palindromes is:
|
||||
Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"].
|
||||
Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"].
|
||||
Both strings are now palindromes.
|
||||
Hence, the maximum number of palindromes achievable is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["cd","ef","a"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> In this example, there is no need to perform any operation.
|
||||
There is one palindrome in words "a".
|
||||
It can be shown that it is not possible to get more than one palindrome after any number of operations.
|
||||
Hence, the answer is 1.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
33
leetcode/problem/modify-the-matrix.html
Normal file
33
leetcode/problem/modify-the-matrix.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>matrix</code>, create a new <strong>0-indexed</strong> matrix called <code>answer</code>. Make <code>answer</code> equal to <code>matrix</code>, then replace each element with the value <code>-1</code> with the <strong>maximum</strong> element in its respective column.</p>
|
||||
|
||||
<p>Return <em>the matrix</em> <code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix1.png" style="width: 491px; height: 161px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[1,2,-1],[4,-1,6],[7,8,9]]
|
||||
<strong>Output:</strong> [[1,2,9],[4,8,6],[7,8,9]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8.
|
||||
- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/24/matrix2.png" style="width: 411px; height: 111px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> matrix = [[3,-1],[5,2]]
|
||||
<strong>Output:</strong> [[3,2],[5,2]]
|
||||
<strong>Explanation:</strong> The diagram above shows the elements that are changed (in blue).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == matrix.length</code></li>
|
||||
<li><code>n == matrix[i].length</code></li>
|
||||
<li><code>2 <= m, n <= 50</code></li>
|
||||
<li><code>-1 <= matrix[i][j] <= 100</code></li>
|
||||
<li>The input is generated such that each column contains at least one non-negative integer.</li>
|
||||
</ul>
|
64
leetcode/problem/most-frequent-prime.html
Normal file
64
leetcode/problem/most-frequent-prime.html
Normal file
@@ -0,0 +1,64 @@
|
||||
<p>You are given a <code>m x n</code> <strong>0-indexed </strong>2D<strong> </strong>matrix <code>mat</code>. From every cell, you can create numbers in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>There could be at most <code>8</code> paths from the cells namely: east, south-east, south, south-west, west, north-west, north, and north-east.</li>
|
||||
<li>Select a path from them and append digits in this path to the number being formed by traveling in this direction.</li>
|
||||
<li>Note that numbers are generated at every step, for example, if the digits along the path are <code>1, 9, 1</code>, then there will be three numbers generated along the way: <code>1, 19, 191</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the most frequent <span data-keyword="prime-number">prime number</span> <strong>greater</strong> than </em><code>10</code><em> out of all the numbers created by traversing the matrix or </em><code>-1</code><em> if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the <b>largest</b> among them.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> It is invalid to change the direction during the move.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<strong><img alt="" src="https://assets.leetcode.com/uploads/2024/02/15/south" style="width: 641px; height: 291px;" /> </strong>
|
||||
|
||||
<pre>
|
||||
<strong>
|
||||
Input:</strong> mat = [[1,1],[9,9],[1,1]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong>
|
||||
From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are:
|
||||
East: [11], South-East: [19], South: [19,191].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191].
|
||||
The most frequent prime number among all the created numbers is 19.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[7]]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[9,7,8],[4,6,5],[2,8,6]]
|
||||
<strong>Output:</strong> 97
|
||||
<strong>Explanation:</strong>
|
||||
Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942].
|
||||
Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79].
|
||||
Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879].
|
||||
Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47].
|
||||
Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68].
|
||||
Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58].
|
||||
Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268].
|
||||
Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85].
|
||||
Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658].
|
||||
The most frequent prime number among all the created numbers is 97.
|
||||
</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 <= 6</code></li>
|
||||
<li><code>1 <= mat[i][j] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>, and a <strong>0-indexed</strong> integer array <code>pattern</code> of size <code>m</code> consisting of integers <code>-1</code>, <code>0</code>, and <code>1</code>.</p>
|
||||
|
||||
<p>A <span data-keyword="subarray">subarray</span> <code>nums[i..j]</code> of size <code>m + 1</code> is said to match the <code>pattern</code> if the following conditions hold for each element <code>pattern[k]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums[i + k + 1] > nums[i + k]</code> if <code>pattern[k] == 1</code>.</li>
|
||||
<li><code>nums[i + k + 1] == nums[i + k]</code> if <code>pattern[k] == 0</code>.</li>
|
||||
<li><code>nums[i + k + 1] < nums[i + k]</code> if <code>pattern[k] == -1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the<strong> count</strong> of subarrays in</em> <code>nums</code> <em>that match the</em> <code>pattern</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5,6], pattern = [1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern.
|
||||
Hence, there are 4 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation: </strong>Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern.
|
||||
Hence, there are 2 subarrays in nums that match the pattern.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == nums.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= m == pattern.length < n</code></li>
|
||||
<li><code>-1 <= pattern[i] <= 1</code></li>
|
||||
</ul>
|
39
leetcode/problem/type-of-triangle.html
Normal file
39
leetcode/problem/type-of-triangle.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>3</code> which can form the sides of a triangle.</p>
|
||||
|
||||
<ul>
|
||||
<li>A triangle is called <strong>equilateral</strong> if it has all sides of equal length.</li>
|
||||
<li>A triangle is called <strong>isosceles</strong> if it has exactly two sides of equal length.</li>
|
||||
<li>A triangle is called <strong>scalene</strong> if all its sides are of different lengths.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>a string representing</em> <em>the type of triangle that can be formed </em><em>or </em><code>"none"</code><em> if it <strong>cannot</strong> form a triangle.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3,3]
|
||||
<strong>Output:</strong> "equilateral"
|
||||
<strong>Explanation:</strong> Since all the sides are of equal length, therefore, it will form an equilateral triangle.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5]
|
||||
<strong>Output:</strong> "scalene"
|
||||
<strong>Explanation:</strong>
|
||||
nums[0] + nums[1] = 3 + 4 = 7, which is greater than nums[2] = 5.
|
||||
nums[0] + nums[2] = 3 + 5 = 8, which is greater than nums[1] = 4.
|
||||
nums[1] + nums[2] = 4 + 5 = 9, which is greater than nums[0] = 3.
|
||||
Since the sum of the two sides is greater than the third side for all three cases, therefore, it can form a triangle.
|
||||
As all the sides are of different lengths, it will form a scalene triangle.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user