mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p>
|
||||
|
||||
<p>A string is <strong>encrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li>
|
||||
<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>A string is <strong>decrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li>
|
||||
<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>Implement the <code>Encrypter</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li>
|
||||
<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>
|
||||
<li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["Encrypter", "encrypt", "decrypt"]
|
||||
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
|
||||
<strong>Output</strong>
|
||||
[null, "eizfeiam", 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
|
||||
encrypter.encrypt("abcd"); // return "eizfeiam".
|
||||
// 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".
|
||||
encrypter.decrypt("eizfeiam"); // return 2.
|
||||
// "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'.
|
||||
// Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd".
|
||||
// 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= keys.length == values.length <= 26</code></li>
|
||||
<li><code>values[i].length == 2</code></li>
|
||||
<li><code>1 <= dictionary.length <= 100</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 100</code></li>
|
||||
<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= word1.length <= 2000</code></li>
|
||||
<li><code>1 <= word2.length <= 200</code></li>
|
||||
<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>
|
||||
<li><code>word2.length</code> is even.</li>
|
||||
<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>
|
||||
<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>matches</code> where <code>matches[i] = [winner<sub>i</sub>, loser<sub>i</sub>]</code> indicates that the player <code>winner<sub>i</sub></code> defeated player <code>loser<sub>i</sub></code> in a match.</p>
|
||||
|
||||
<p>Return <em>a list </em><code>answer</code><em> of size </em><code>2</code><em> where:</em></p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[0]</code> is a list of all players that have <strong>not</strong> lost any matches.</li>
|
||||
<li><code>answer[1]</code> is a list of all players that have lost exactly <strong>one</strong> match.</li>
|
||||
</ul>
|
||||
|
||||
<p>The values in the two lists should be returned in <strong>increasing</strong> order.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>You should only consider the players that have played <strong>at least one</strong> match.</li>
|
||||
<li>The testcases will be generated such that <strong>no</strong> two matches will have the <strong>same</strong> outcome.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
|
||||
<strong>Output:</strong> [[1,2,10],[4,5,7,8]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, and 10 have not lost any matches.
|
||||
Players 4, 5, 7, and 8 each have lost one match.
|
||||
Players 3, 6, and 9 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[2,3],[1,3],[5,4],[6,4]]
|
||||
<strong>Output:</strong> [[1,2,5,6],[]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, 5, and 6 have not lost any matches.
|
||||
Players 3 and 4 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matches.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>matches[i].length == 2</code></li>
|
||||
<li><code>1 <= winner<sub>i</sub>, loser<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>winner<sub>i</sub> != loser<sub>i</sub></code></li>
|
||||
<li>All <code>matches[i]</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is a digit between <code>0</code> and <code>9</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>The <strong>triangular sum</strong> of <code>nums</code> is the value of the only element present in <code>nums</code> after the following process terminates:</p>
|
||||
|
||||
<ol>
|
||||
<li>Let <code>nums</code> comprise of <code>n</code> elements. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n - 1</code>.</li>
|
||||
<li>For each index <code>i</code>, where <code>0 <= i < n - 1</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>(nums[i] + nums[i+1]) % 10</code>, where <code>%</code> denotes modulo operator.</li>
|
||||
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
|
||||
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the triangular sum of</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram depicts the process from which we obtain the triangular sum of the array.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Since there is only one element in nums, the triangular sum is the value of that element itself.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>You are <strong>building</strong> a string <code>s</code> of length <code>n</code> <strong>one</strong> character at a time, <strong>prepending</strong> each new character to the <strong>front</strong> of the string. The strings are labeled from <code>1</code> to <code>n</code>, where the string with length <code>i</code> is labeled <code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>s = "abaca"</code>, <code>s<sub>1</sub> == "a"</code>, <code>s<sub>2</sub> == "ca"</code>, <code>s<sub>3</sub> == "aca"</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> of <code>s<sub>i</sub></code> is the length of the <strong>longest common prefix</strong> between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).</p>
|
||||
|
||||
<p>Given the final string <code>s</code>, return<em> the <strong>sum</strong> of the <strong>score</strong> of every </em><code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "babab"
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>1</sub> == "b", the longest common prefix is "b" which has a score of 1.
|
||||
For s<sub>2</sub> == "ab", there is no common prefix so the score is 0.
|
||||
For s<sub>3</sub> == "bab", the longest common prefix is "bab" which has a score of 3.
|
||||
For s<sub>4</sub> == "abab", there is no common prefix so the score is 0.
|
||||
For s<sub>5</sub> == "babab", the longest common prefix is "babab" which has a score of 5.
|
||||
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "azbazbzaz"
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>2</sub> == "az", the longest common prefix is "az" which has a score of 2.
|
||||
For s<sub>6</sub> == "azbzaz", the longest common prefix is "azb" which has a score of 3.
|
||||
For s<sub>9</sub> == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9.
|
||||
For all other s<sub>i</sub>, the score is 0.
|
||||
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.
|
||||
</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 lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can take <strong>at most one</strong> pile of candies and some piles of candies may go unused.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum number of candies</strong> each child can get.</em></p>
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [5,8,6], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [2,5], k = 11
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candies.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candies[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>12</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>You are given two strings <code>current</code> and <code>correct</code> representing two <strong>24-hour times</strong>.</p>
|
||||
|
||||
<p>24-hour times are formatted as <code>"HH:MM"</code>, where <code>HH</code> is between <code>00</code> and <code>23</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 24-hour time is <code>00:00</code>, and the latest is <code>23:59</code>.</p>
|
||||
|
||||
<p>In one operation you can increase the time <code>current</code> by <code>1</code>, <code>5</code>, <code>15</code>, or <code>60</code> minutes. You can perform this operation <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> needed to convert </em><code>current</code><em> to </em><code>correct</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "02:30", correct = "04:35"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:
|
||||
</strong>We can convert current to correct in 3 operations as follows:
|
||||
- Add 60 minutes to current. current becomes "03:30".
|
||||
- Add 60 minutes to current. current becomes "04:30".
|
||||
- Add 5 minutes to current. current becomes "04:35".
|
||||
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "11:00", correct = "11:01"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We only have to add one minute to current, so the minimum number of operations needed is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>current</code> and <code>correct</code> are in the format <code>"HH:MM"</code></li>
|
||||
<li><code>current <= correct</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>A <strong>bit flip</strong> of a number <code>x</code> is choosing a bit in the binary representation of <code>x</code> and <strong>flipping</strong> it from either <code>0</code> to <code>1</code> or <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>x = 7</code>, the binary representation is <code>111</code> and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get <code>110</code>, flip the second bit from the right to get <code>101</code>, flip the fifth bit from the right (a leading zero) to get <code>10111</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given two integers <code>start</code> and <code>goal</code>, return<em> the <strong>minimum</strong> number of <strong>bit flips</strong> to convert </em><code>start</code><em> to </em><code>goal</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 10, goal = 7
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
|
||||
- Flip the first bit from the right: 101<u>0</u> -> 101<u>1</u>.
|
||||
- Flip the third bit from the right: 1<u>0</u>11 -> 1<u>1</u>11.
|
||||
- Flip the fourth bit from the right: <u>1</u>111 -> <u>0</u>111.
|
||||
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 3, goal = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
|
||||
- Flip the first bit from the right: 01<u>1</u> -> 01<u>0</u>.
|
||||
- Flip the second bit from the right: 0<u>1</u>0 -> 0<u>0</u>0.
|
||||
- Flip the third bit from the right: <u>0</u>00 -> <u>1</u>00.
|
||||
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= start, goal <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> which represents the types of buildings along a street where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] = '0'</code> denotes that the <code>i<sup>th</sup></code> building is an office and</li>
|
||||
<li><code>s[i] = '1'</code> denotes that the <code>i<sup>th</sup></code> building is a restaurant.</li>
|
||||
</ul>
|
||||
|
||||
<p>As a city official, you would like to <strong>select</strong> 3 buildings for random inspection. However, to ensure variety, <strong>no two consecutive</strong> buildings out of the <strong>selected</strong> buildings can be of the same type.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, given <code>s = "0<u><strong>0</strong></u>1<u><strong>1</strong></u>0<u><strong>1</strong></u>"</code>, we cannot select the <code>1<sup>st</sup></code>, <code>3<sup>rd</sup></code>, and <code>5<sup>th</sup></code> buildings as that would form <code>"0<strong><u>11</u></strong>"</code> which is <strong>not</strong> allowed due to having two consecutive buildings of the same type.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <b>number of valid ways</b> to select 3 buildings.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "001101"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The following sets of indices selected are valid:
|
||||
- [0,2,4] from "<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1" forms "010"
|
||||
- [0,3,4] from "<u><strong>0</strong></u>01<u><strong>10</strong></u>1" forms "010"
|
||||
- [1,2,4] from "0<u><strong>01</strong></u>1<u><strong>0</strong></u>1" forms "010"
|
||||
- [1,3,4] from "0<u><strong>0</strong></u>1<u><strong>10</strong></u>1" forms "010"
|
||||
- [2,4,5] from "00<u><strong>1</strong></u>1<u><strong>01</strong></u>" forms "101"
|
||||
- [3,4,5] from "001<u><strong>101</strong></u>" forms "101"
|
||||
No other selection is valid. Thus, there are 6 total ways.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> It can be shown that there are no valid selections.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user