mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 14:12:17 +08:00
update
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<p>You are given an integer <code>n</code>. You roll a fair 6-sided dice <code>n</code> times. Determine the total number of <strong>distinct</strong> sequences of rolls possible such that the following conditions are satisfied:</p>
|
||||
|
||||
<ol>
|
||||
<li>The <strong>greatest common divisor</strong> of any <strong>adjacent</strong> values in the sequence is equal to <code>1</code>.</li>
|
||||
<li>There is <strong>at least</strong> a gap of <code>2</code> rolls between <strong>equal</strong> valued rolls. More formally, if the value of the <code>i<sup>th</sup></code> roll is <strong>equal</strong> to the value of the <code>j<sup>th</sup></code> roll, then <code>abs(i - j) > 2</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the<strong> total number</strong> of distinct sequences possible</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Two sequences are considered distinct if at least one element is different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 184
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
|
||||
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
|
||||
(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed).
|
||||
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
|
||||
There are a total of 184 distinct sequences possible, so we return 184.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> Some of the possible sequences are (1, 2), (2, 1), (3, 2).
|
||||
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
|
||||
There are a total of 22 distinct sequences possible, so we return 22.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>Given two integers <code>num</code> and <code>k</code>, consider a set of positive integers with the following properties:</p>
|
||||
|
||||
<ul>
|
||||
<li>The units digit of each integer is <code>k</code>.</li>
|
||||
<li>The sum of the integers is <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible size of such a set, or </em><code>-1</code><em> if no such set exists.</em></p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The set can contain multiple instances of the same integer, and the sum of an empty set is considered <code>0</code>.</li>
|
||||
<li>The <strong>units digit</strong> of a number is the rightmost digit of the number.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 58, k = 9
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
|
||||
Another valid set is [19,39].
|
||||
It can be shown that 2 is the minimum possible size of a valid set.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 37, k = 2
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 0, k = 7
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The sum of an empty set is considered 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num <= 3000</code></li>
|
||||
<li><code>0 <= k <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>There is an undirected connected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
|
||||
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> where <code>nums[i]</code> represents the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Remove two <strong>distinct</strong> edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:</p>
|
||||
|
||||
<ol>
|
||||
<li>Get the XOR of all the values of the nodes for <strong>each</strong> of the three components respectively.</li>
|
||||
<li>The <strong>difference</strong> between the <strong>largest</strong> XOR value and the <strong>smallest</strong> XOR value is the <strong>score</strong> of the pair.</li>
|
||||
</ol>
|
||||
|
||||
<ul>
|
||||
<li>For example, say the three components have the node values: <code>[4,5,7]</code>, <code>[1,9]</code>, and <code>[3,3,3]</code>. The three XOR values are <code>4 ^ 5 ^ 7 = <u><strong>6</strong></u></code>, <code>1 ^ 9 = <u><strong>8</strong></u></code>, and <code>3 ^ 3 ^ 3 = <u><strong>3</strong></u></code>. The largest XOR value is <code>8</code> and the smallest XOR value is <code>3</code>. The score is then <code>8 - 3 = 5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> score of any possible pair of edge removals on the given tree</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png" style="width: 193px; height: 190px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
|
||||
- The 2<sup>nd</sup> component has node [0] with value [1]. Its XOR value is 1 = 1.
|
||||
- The 3<sup>rd</sup> component has node [2] with value [5]. Its XOR value is 5 = 5.
|
||||
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
|
||||
It can be shown that no other pair of removals will obtain a smaller score than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png" style="width: 287px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The diagram above shows a way to make a pair of removals.
|
||||
- The 1<sup>st</sup> component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
|
||||
- The 2<sup>nd</sup> component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
|
||||
- The 3<sup>rd</sup> component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
|
||||
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
|
||||
We cannot obtain a smaller score than 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given a string of English letters <code>s</code>, return <em>the <strong>greatest </strong>English letter which occurs as <strong>both</strong> a lowercase and uppercase letter in</em> <code>s</code>. The returned letter should be in <strong>uppercase</strong>. If no such letter exists, return <em>an empty string</em>.</p>
|
||||
|
||||
<p>An English letter <code>b</code> is <strong>greater</strong> than another letter <code>a</code> if <code>b</code> appears <strong>after</strong> <code>a</code> in the English alphabet.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l<strong><u>Ee</u></strong>TcOd<u><strong>E</strong></u>"
|
||||
<strong>Output:</strong> "E"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'E' is the only letter to appear in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "a<strong><u>rR</u></strong>AzFif"
|
||||
<strong>Output:</strong> "R"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'R' is the greatest letter to appear in both lower and upper case.
|
||||
Note that 'A' and 'F' also appear in both lower and upper case, but 'R' is greater than 'F' or 'A'.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AbCdEfGhIjK"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong>
|
||||
There is no letter that appears in both lower and upper case.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase and uppercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>A square matrix is said to be an <strong>X-Matrix</strong> if <strong>both</strong> of the following conditions hold:</p>
|
||||
|
||||
<ol>
|
||||
<li>All the elements in the diagonals of the matrix are <strong>non-zero</strong>.</li>
|
||||
<li>All other elements are 0.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given a 2D integer array <code>grid</code> of size <code>n x n</code> representing a square matrix, return <code>true</code><em> if </em><code>grid</code><em> is an X-Matrix</em>. Otherwise, return <code>false</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg" style="width: 311px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg" style="width: 238px; height: 246px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[5,7,0],[0,3,1],[0,5,0]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Refer to the diagram above.
|
||||
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
|
||||
Thus, grid is not an X-Matrix.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 100</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given two integers <code>m</code> and <code>n</code> that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array <code>prices</code>, where <code>prices[i] = [h<sub>i</sub>, w<sub>i</sub>, price<sub>i</sub>]</code> indicates you can sell a rectangular piece of wood of height <code>h<sub>i</sub></code> and width <code>w<sub>i</sub></code> for <code>price<sub>i</sub></code> dollars.</p>
|
||||
|
||||
<p>To cut a piece of wood, you must make a vertical or horizontal cut across the <strong>entire</strong> height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to <code>prices</code>. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you <strong>cannot</strong> rotate a piece to swap its height and width.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> money you can earn after cutting an </em><code>m x n</code><em> piece of wood</em>.</p>
|
||||
|
||||
<p>Note that you can cut the piece of wood as many times as you want.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex1.png" style="width: 239px; height: 150px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
|
||||
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 14 + 3 + 2 = 19 money earned.
|
||||
It can be shown that 19 is the maximum amount of money that can be earned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/04/27/ex2new.png" style="width: 250px; height: 175px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> m = 4, n = 6, prices = [[3,2,10],[1,4,2],[4,1,3]]
|
||||
<strong>Output:</strong> 32
|
||||
<strong>Explanation:</strong> The diagram above shows a possible scenario. It consists of:
|
||||
- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 = 30.
|
||||
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
|
||||
This obtains a total of 30 + 2 = 32 money earned.
|
||||
It can be shown that 32 is the maximum amount of money that can be earned.
|
||||
Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>1 <= prices.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>prices[i].length == 3</code></li>
|
||||
<li><code>1 <= h<sub>i</sub> <= m</code></li>
|
||||
<li><code>1 <= w<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>All the shapes of wood <code>(h<sub>i</sub>, w<sub>i</sub>)</code> are pairwise <strong>distinct</strong>.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the length of the <strong>longest</strong> subsequence of </em><code>s</code><em> that makes up a <strong>binary</strong> number less than or equal to</em> <code>k</code>.</p>
|
||||
|
||||
<p>Note:</p>
|
||||
|
||||
<ul>
|
||||
<li>The subsequence can contain <strong>leading zeroes</strong>.</li>
|
||||
<li>The empty string is considered to be equal to <code>0</code>.</li>
|
||||
<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001010", k = 5
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
|
||||
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
|
||||
The length of this subsequence is 5, so 5 is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "00101001", k = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> "000001" is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
|
||||
The length of this subsequence is 6, so 6 is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>
|
||||
|
||||
<p>You can choose two integers <code>left</code> and <code>right</code> where <code>0 <= left <= right < n</code> and <strong>swap</strong> the subarray <code>nums1[left...right]</code> with the subarray <code>nums2[left...right]</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums1 = [1,2,3,4,5]</code> and <code>nums2 = [11,12,13,14,15]</code> and you choose <code>left = 1</code> and <code>right = 2</code>, <code>nums1</code> becomes <code>[1,<strong><u>12,13</u></strong>,4,5]</code> and <code>nums2</code> becomes <code>[11,<strong><u>2,3</u></strong>,14,15]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You may choose to apply the mentioned operation <strong>once</strong> or not do anything.</p>
|
||||
|
||||
<p>The <strong>score</strong> of the arrays is the <strong>maximum</strong> of <code>sum(nums1)</code> and <code>sum(nums2)</code>, where <code>sum(arr)</code> is the sum of all the elements in the array <code>arr</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum possible score</strong></em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array. <code>arr[left...right]</code> denotes the subarray that contains the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [60,60,60], nums2 = [10,90,10]
|
||||
<strong>Output:</strong> 210
|
||||
<strong>Explanation:</strong> Choosing left = 1 and right = 1, we have nums1 = [60,<u><strong>90</strong></u>,60] and nums2 = [10,<u><strong>60</strong></u>,10].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
|
||||
<strong>Output:</strong> 220
|
||||
<strong>Explanation:</strong> Choosing left = 3, right = 4, we have nums1 = [20,40,20,<u><strong>40,20</strong></u>] and nums2 = [50,20,50,<u><strong>70,30</strong></u>].
|
||||
The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [7,11,13], nums2 = [1,1,1]
|
||||
<strong>Output:</strong> 31
|
||||
<strong>Explanation:</strong> We choose not to swap any subarray.
|
||||
The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, select <strong>any</strong> non-negative integer <code>x</code> and an index <code>i</code>, then <strong>update</strong> <code>nums[i]</code> to be equal to <code>nums[i] AND (nums[i] XOR x)</code>.</p>
|
||||
|
||||
<p>Note that <code>AND</code> is the bitwise AND operation and <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible bitwise XOR of all elements of </em><code>nums</code><em> after applying the operation <strong>any number</strong> of times</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,4,6]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
|
||||
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
|
||||
It can be shown that 7 is the maximum possible bitwise XOR.
|
||||
Note that other operations may be used to achieve a bitwise XOR of 7.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,9,2]
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> Apply the operation zero times.
|
||||
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
|
||||
It can be shown that 11 is the maximum possible bitwise XOR.</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>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>There is a street with <code>n * 2</code> <strong>plots</strong>, where there are <code>n</code> plots on each side of the street. The plots on each side are numbered from <code>1</code> to <code>n</code>. On each plot, a house can be placed.</p>
|
||||
|
||||
<p>Return <em>the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>Note that if a house is placed on the <code>i<sup>th</sup></code> plot on one side of the street, a house can also be placed on the <code>i<sup>th</sup></code> plot on the other side of the street.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Possible arrangements:
|
||||
1. All plots are empty.
|
||||
2. A house is placed on one side of the street.
|
||||
3. A house is placed on the other side of the street.
|
||||
4. Two houses are placed, one on each side of the street.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="width: 500px; height: 500px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> The 9 possible arrangements are shown in the diagram above.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> nodes, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>number of pairs</strong> of different nodes that are <strong>unreachable</strong> from each other</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-3.png" style="width: 267px; height: 169px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1],[0,2],[1,2]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no pairs of nodes that are unreachable from each other. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/05/tc-2.png" style="width: 295px; height: 269px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong> There are 14 pairs of nodes that are unreachable from each other:
|
||||
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
|
||||
Therefore, we return 14.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= edges.length <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code>s</code>, where every <strong>two</strong> consecutive vertical bars <code>'|'</code> are grouped into a <strong>pair</strong>. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> <code>'|'</code> make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> <code>'|'</code> make a pair, and so forth.</p>
|
||||
|
||||
<p>Return <em>the number of </em><code>'*'</code><em> in </em><code>s</code><em>, <strong>excluding</strong> the </em><code>'*'</code><em> between each pair of </em><code>'|'</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that each <code>'|'</code> will belong to <strong>exactly</strong> one pair.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "l|*e*et|c**o|*de|"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>l</u>|*e*et|<u>c**o</u>|*de|".
|
||||
The characters between the first and second '|' are excluded from the answer.
|
||||
Also, the characters between the third and fourth '|' are excluded from the answer.
|
||||
There are 2 asterisks considered. Therefore, we return 2.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "iamprogrammer"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> In this example, there are no asterisks in s. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "yo|uar|e**|b|e***au|tifu|l"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The considered characters are underlined: "<u>yo</u>|uar|<u>e**</u>|b|<u>e***au</u>|tifu|<u>l</u>". There are 5 asterisks considered. Therefore, we return 5.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters, vertical bars <code>'|'</code>, and asterisks <code>'*'</code>.</li>
|
||||
<li><code>s</code> contains an <strong>even</strong> number of vertical bars <code>'|'</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user