mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-03 14:32:54 +08:00
update
This commit is contained in:
File diff suppressed because it is too large
Load Diff
62
leetcode/originData/[no content]market-analysis-iii.json
Normal file
62
leetcode/originData/[no content]market-analysis-iii.json
Normal file
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "3205",
|
||||
"questionFrontendId": "2922",
|
||||
"boundTopicId": null,
|
||||
"title": "Market Analysis III",
|
||||
"titleSlug": "market-analysis-iii",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 5,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"62\", \"totalSubmission\": \"119\", \"totalAcceptedRaw\": 62, \"totalSubmissionRaw\": 119, \"acRate\": \"52.1%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\": {\"Users\": [\"seller_id\", \"join_date\", \"favorite_brand\"],\"Orders\":[\"order_id\",\"order_date\",\"item_id\",\"seller_id\"],\"Items\":[\"item_id\",\"item_brand\"]},\"rows\":{\"Users\":[[1,\"2019-01-01\",\"Lenovo\"],[2,\"2019-02-09\",\"Samsung\"],[3,\"2019-01-19\",\"LG\"]],\"Orders\":[[1,\"2019-08-01\",4,2],[2,\"2019-08-02\",2,3],[3,\"2019-08-03\",3,3],[4,\"2019-08-04\",1,2],[5,\"2019-08-04\",4,2]],\"Items\":[[1,\"Samsung\"],[2,\"Lenovo\"],[3,\"LG\"],[4,\"HP\"]]}}",
|
||||
"metaData": "{\"mysql\": [\"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table If Not Exists Items (item_id int, item_brand varchar(10))\"], \"mssql\": [\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table Items (item_id int, item_brand varchar(10))\"], \"oraclesql\": [\"Create table Users (seller_id int, join_date date, favorite_brand varchar(10))\", \"Create table Orders (order_id int, order_date date, item_id int, seller_id int)\", \"Create table Items (item_id int, item_brand varchar(10))\", \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"], \"database\": true, \"languages\": [\"mysql\", \"mssql\", \"oraclesql\"], \"database_schema\": {\"Users\": {\"seller_id\": \"INT\", \"join_date\": \"DATE\", \"favorite_brand\": \"VARCHAR(10)\"}, \"Orders\": {\"order_id\": \"INT\", \"order_date\": \"DATE\", \"item_id\": \"INT\", \"seller_id\": \"INT\"}, \"Items\": {\"item_id\": \"INT\", \"item_brand\": \"VARCHAR(10)\"}}}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Users (seller_id int, join_date date, favorite_brand varchar(10))",
|
||||
"Create table If Not Exists Orders (order_id int, order_date date, item_id int, seller_id int)",
|
||||
"Create table If Not Exists Items (item_id int, item_brand varchar(10))",
|
||||
"Truncate table Users",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung')",
|
||||
"insert into Users (seller_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG')",
|
||||
"Truncate table Orders",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('1', '2019-08-01', '4', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('2', '2019-08-02', '2', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('3', '2019-08-03', '3', '3')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('4', '2019-08-04', '1', '2')",
|
||||
"insert into Orders (order_id, order_date, item_id, seller_id) values ('5', '2019-08-04', '4', '2')",
|
||||
"Truncate table Items",
|
||||
"insert into Items (item_id, item_brand) values ('1', 'Samsung')",
|
||||
"insert into Items (item_id, item_brand) values ('2', 'Lenovo')",
|
||||
"insert into Items (item_id, item_brand) values ('3', 'LG')",
|
||||
"insert into Items (item_id, item_brand) values ('4', 'HP')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
176
leetcode/originData/find-the-k-or-of-an-array.json
Normal file
176
leetcode/originData/find-the-k-or-of-an-array.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
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
49
leetcode/problem/find-the-k-or-of-an-array.html
Normal file
49
leetcode/problem/find-the-k-or-of-an-array.html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>K-or</strong> of <code>nums</code> is a non-negative integer that satisfies the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <code>i<sup>th</sup></code> bit is set in the K-or <strong>if and only if</strong> there are at least <code>k</code> elements of nums in which bit <code>i</code> is set.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong> K-or</strong> of</em> <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a bit <code>i</code> is set in <code>x</code> if <code>(2<sup>i</sup> AND x) == 2<sup>i</sup></code>, where <code>AND</code> is the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,12,9,8,9,15], k = 4
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Bit 0 is set at nums[0], nums[2], nums[4], and nums[5].
|
||||
Bit 1 is set at nums[0], and nums[5].
|
||||
Bit 2 is set at nums[0], nums[1], and nums[5].
|
||||
Bit 3 is set at nums[1], nums[2], nums[3], nums[4], and nums[5].
|
||||
Only bits 0 and 3 are set in at least k elements of the array, and bits i >= 4 are not set in any of the array's elements. Hence, the answer is 2^0 + 2^3 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,12,1,11,4,5], k = 6
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Since k == 6 == nums.length, the 6-or of the array is equal to the bitwise AND of all its elements. Hence, the answer is 2 AND 12 AND 1 AND 11 AND 4 AND 5 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,8,5,9,11,6,8], k = 1
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>31</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code>, and an integer <code>target</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>length of the longest subsequence</strong> of</em> <code>nums</code> <em>that sums up to</em> <code>target</code>. <em>If no such subsequence exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5], target = 9
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 subsequences with a sum equal to 9: [4,5], [1,3,5], and [2,3,4]. The longest subsequences are [1,3,5], and [2,3,4]. Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,3,2,1,5], target = 7
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> There are 5 subsequences with a sum equal to 7: [4,3], [4,1,2], [4,2,1], [1,1,5], and [1,3,2,1]. The longest subsequence is [1,3,2,1]. Hence, the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,5,4,5], target = 3
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that nums has no subsequence that sums up to 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>There exists an undirected tree rooted at node <code>0</code> with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given a 2D <strong>integer</strong> 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. You are also given a <strong>0-indexed</strong> array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> indicates the number of coins in the vertex <code>i</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.</p>
|
||||
|
||||
<p>Coins at <code>node<sub>i</sub></code> can be collected in one of the following ways:</p>
|
||||
|
||||
<ul>
|
||||
<li>Collect all the coins, but you will get <code>coins[i] - k</code> points. If <code>coins[i] - k</code> is negative then you will lose <code>abs(coins[i] - k)</code> points.</li>
|
||||
<li>Collect all the coins, but you will get <code>floor(coins[i] / 2)</code> points. If this way is used, then for all the <code>node<sub>j</sub></code> present in the subtree of <code>node<sub>i</sub></code>, <code>coins[j]</code> will get reduced to <code>floor(coins[j] / 2)</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum points</strong> you can get after collecting the coins from <strong>all</strong> the tree nodes.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png" style="width: 60px; height: 316px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong>
|
||||
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
|
||||
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
|
||||
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
|
||||
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
|
||||
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<strong class="example"> <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex2.png" style="width: 140px; height: 147px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == coins.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code><font face="monospace">0 <= coins[i] <= 10<sup>4</sup></font></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code><font face="monospace">0 <= edges[i][0], edges[i][1] < n</font></code></li>
|
||||
<li><code><font face="monospace">0 <= k <= 10<sup>4</sup></font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given two arrays <code>nums1</code> and <code>nums2</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You have to replace <strong>all</strong> the <code>0</code>'s in both arrays with <strong>strictly</strong> positive integers such that the sum of elements of both arrays becomes <strong>equal</strong>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> equal sum you can obtain, or </em><code>-1</code><em> if it is impossible</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,0,1,0], nums2 = [6,5,0]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong> We can replace 0's in the following way:
|
||||
- Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
|
||||
- Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
|
||||
Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,0,2,0], nums2 = [1,4]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It is impossible to make the sum of both arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> having length <code>n</code>, and an integer <code>k</code>.</p>
|
||||
|
||||
<p>You can perform the following <strong>increment</strong> operation <strong>any</strong> number of times (<strong>including zero</strong>):</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> in the range <code>[0, n - 1]</code>, and increase <code>nums[i]</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>An array is considered <strong>beautiful</strong> if, for any <strong>subarray</strong> with a size of <code>3</code> or <strong>more</strong>, its <strong>maximum</strong> element is <strong>greater than or equal</strong> to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of increment operations needed to make </em><code>nums</code><em> <strong>beautiful</strong>.</em></p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,0,0,2], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 1 and increase nums[1] by 1 -> [2,4,0,0,2].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,3].
|
||||
Choose index i = 4 and increase nums[4] by 1 -> [2,4,0,0,4].
|
||||
The subarrays with a size of 3 or more are: [2,4,0], [4,0,0], [0,0,4], [2,4,0,0], [4,0,0,4], [2,4,0,0,4].
|
||||
In all the subarrays, the maximum element is equal to k = 4, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 3 increment operations.
|
||||
Hence, the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,3,3], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can perform the following increment operations to make nums beautiful:
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,4,3].
|
||||
Choose index i = 2 and increase nums[2] by 1 -> [0,1,5,3].
|
||||
The subarrays with a size of 3 or more are: [0,1,5], [1,5,3], [0,1,5,3].
|
||||
In all the subarrays, the maximum element is equal to k = 5, so nums is now beautiful.
|
||||
It can be shown that nums cannot be made beautiful with fewer than 2 increment operations.
|
||||
Hence, the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2], k = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The only subarray with a size of 3 or more in this example is [1,1,2].
|
||||
The maximum element, 2, is already greater than k = 1, so we don't need any increment operation.
|
||||
Hence, the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> having an even length.</p>
|
||||
|
||||
<p>A string is <strong>beautiful</strong> if it's possible to partition it into one or more substrings such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each substring has an <strong>even length</strong>.</li>
|
||||
<li>Each substring contains <strong>only</strong> <code>1</code>'s or <strong>only</strong> <code>0</code>'s.</li>
|
||||
</ul>
|
||||
|
||||
<p>You can change any character in <code>s</code> to <code>0</code> or <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of changes required to make the string </em><code>s</code> <em>beautiful</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "1001"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We change s[1] to 1 and s[3] to 0 to get string "1100".
|
||||
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
|
||||
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "10"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We change s[1] to 1 to get string "11".
|
||||
It can be seen that the string "11" is beautiful because we can partition it into "11".
|
||||
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0000"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We don't need to make any changes as the string "0000" is beautiful already.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> has an even length.</li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,1]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>
|
||||
|
||||
<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 <= i <= j < nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>
|
||||
|
||||
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,1]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Six possible subarrays are:
|
||||
[1]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[1]: 1 distinct value
|
||||
[1,2]: 2 distinct values
|
||||
[2,1]: 2 distinct values
|
||||
[1,2,1]: 2 distinct values
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Three possible subarrays are:
|
||||
[2]: 1 distinct value
|
||||
[2]: 1 distinct value
|
||||
[2,2]: 1 distinct value
|
||||
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user