mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
update
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
"translatedContent": "<p>给你一个整数数组 <code>instructions</code> ,你需要根据 <code>instructions</code> 中的元素创建一个有序数组。一开始你有一个空的数组 <code>nums</code> ,你需要 <strong>从左到右</strong> 遍历 <code>instructions</code> 中的元素,将它们依次插入 <code>nums</code> 数组中。每一次插入操作的 <strong>代价</strong> 是以下两者的 <strong>较小值</strong> :</p>\n\n<ul>\n\t<li><code>nums</code> 中 <strong>严格小于 </strong> <code>instructions[i]</code> 的数字数目。</li>\n\t<li><code>nums</code> 中 <strong>严格大于 </strong> <code>instructions[i]</code> 的数字数目。</li>\n</ul>\n\n<p>比方说,如果要将 <code>3</code> 插入到 <code>nums = [1,2,3,5]</code> ,那么插入操作的 <strong>代价</strong> 为 <code>min(2, 1)</code> (元素 <code>1</code> 和 <code>2</code> 小于 <code>3</code> ,元素 <code>5</code> 大于 <code>3</code> ),插入后 <code>nums</code> 变成 <code>[1,2,3,3,5]</code> 。</p>\n\n<p>请你返回将 <code>instructions</code> 中所有元素依次插入 <code>nums</code> 后的 <strong>总最小代价 </strong>。由于答案会很大,请将它对 <code>10<sup>9</sup> + 7</code> <b>取余</b> 后返回。</p>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre><b>输入:</b>instructions = [1,5,6,2]\n<b>输出:</b>1\n<b>解释:</b>一开始 nums = [] 。\n插入 1 ,代价为 min(0, 0) = 0 ,现在 nums = [1] 。\n插入 5 ,代价为 min(1, 0) = 0 ,现在 nums = [1,5] 。\n插入 6 ,代价为 min(2, 0) = 0 ,现在 nums = [1,5,6] 。\n插入 2 ,代价为 min(1, 2) = 1 ,现在 nums = [1,2,5,6] 。\n总代价为 0 + 0 + 0 + 1 = 1 。</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<pre><b>输入:</b>instructions = [1,2,3,6,5,4]\n<b>输出:</b>3\n<b>解释:</b>一开始 nums = [] 。\n插入 1 ,代价为 min(0, 0) = 0 ,现在 nums = [1] 。\n插入 2 ,代价为 min(1, 0) = 0 ,现在 nums = [1,2] 。\n插入 3 ,代价为 min(2, 0) = 0 ,现在 nums = [1,2,3] 。\n插入 6 ,代价为 min(3, 0) = 0 ,现在 nums = [1,2,3,6] 。\n插入 5 ,代价为 min(3, 1) = 1 ,现在 nums = [1,2,3,5,6] 。\n插入 4 ,代价为 min(3, 2) = 2 ,现在 nums = [1,2,3,4,5,6] 。\n总代价为 0 + 0 + 0 + 0 + 1 + 2 = 3 。\n</pre>\n\n<p><strong>示例 3:</strong></p>\n\n<pre><b>输入:</b>instructions = [1,3,3,3,2,4,2,1,2]\n<b>输出:</b>4\n<b>解释:</b>一开始 nums = [] 。\n插入 1 ,代价为 min(0, 0) = 0 ,现在 nums = [1] 。\n插入 3 ,代价为 min(1, 0) = 0 ,现在 nums = [1,3] 。\n插入 3 ,代价为 min(1, 0) = 0 ,现在 nums = [1,3,3] 。\n插入 3 ,代价为 min(1, 0) = 0 ,现在 nums = [1,3,3,3] 。\n插入 2 ,代价为 min(1, 3) = 1 ,现在 nums = [1,2,3,3,3] 。\n插入 4 ,代价为 min(5, 0) = 0 ,现在 nums = [1,2,3,3,3,4] 。\n插入 2 ,代价为 min(1, 4) = 1 ,现在 nums = [1,2,2,3,3,3,4] 。\n插入 1 ,代价为 min(0, 6) = 0 ,现在 nums = [1,1,2,2,3,3,3,4] 。\n插入 2 ,代价为 min(2, 4) = 2 ,现在 nums = [1,1,2,2,2,3,3,3,4] 。\n总代价为 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4 。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 <= instructions.length <= 10<sup>5</sup></code></li>\n\t<li><code>1 <= instructions[i] <= 10<sup>5</sup></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Hard",
|
||||
"likes": 37,
|
||||
"likes": 38,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
@@ -173,7 +173,7 @@
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"3.1K\", \"totalSubmission\": \"6.4K\", \"totalAcceptedRaw\": 3072, \"totalSubmissionRaw\": 6353, \"acRate\": \"48.4%\"}",
|
||||
"stats": "{\"totalAccepted\": \"3.2K\", \"totalSubmission\": \"6.6K\", \"totalAcceptedRaw\": 3177, \"totalSubmissionRaw\": 6583, \"acRate\": \"48.3%\"}",
|
||||
"hints": [
|
||||
"This problem is closely related to finding the number of inversions in an array",
|
||||
"if i know the position in which i will insert the i-th element in I can find the minimum cost to insert it"
|
||||
|
Reference in New Issue
Block a user