id
int64 1
3.71k
| title
stringlengths 3
79
| difficulty
stringclasses 3
values | description
stringlengths 430
25.4k
| tags
stringlengths 0
131
| language
stringclasses 19
values | solution
stringlengths 47
20.6k
|
|---|---|---|---|---|---|---|
3,634
|
Minimum Removals to Balance Array
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An array is considered <strong>balanced</strong> if the value of its <strong>maximum</strong> element is <strong>at most</strong> <code>k</code> times the <strong>minimum</strong> element.</p>
<p>You may remove <strong>any</strong> number of elements from <code>nums</code>βββββββ without making it <strong>empty</strong>.</p>
<p>Return the <strong>minimum</strong> number of elements to remove so that the remaining array is balanced.</p>
<p><strong>Note:</strong> An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[2] = 5</code> to get <code>nums = [2, 1]</code>.</li>
<li>Now <code>max = 2</code>, <code>min = 1</code> and <code>max <= min * k</code> as <code>2 <= 1 * 2</code>. Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,6,2,9], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[0] = 1</code> and <code>nums[3] = 9</code> to get <code>nums = [6, 2]</code>.</li>
<li>Now <code>max = 6</code>, <code>min = 2</code> and <code>max <= min * k</code> as <code>6 <= 2 * 3</code>. Thus, the answer is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since <code>nums</code> is already balanced as <code>6 <= 4 * 2</code>, no elements need to be removed.</li>
</ul>
</div>
<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>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
|
Array; Sorting; Sliding Window
|
Go
|
func minRemoval(nums []int, k int) int {
sort.Ints(nums)
n := len(nums)
cnt := 0
for i := 0; i < n; i++ {
j := n
if int64(nums[i])*int64(k) <= int64(nums[n-1]) {
target := int64(nums[i])*int64(k) + 1
j = sort.Search(n, func(x int) bool {
return int64(nums[x]) >= target
})
}
cnt = max(cnt, j-i)
}
return n - cnt
}
|
3,634
|
Minimum Removals to Balance Array
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An array is considered <strong>balanced</strong> if the value of its <strong>maximum</strong> element is <strong>at most</strong> <code>k</code> times the <strong>minimum</strong> element.</p>
<p>You may remove <strong>any</strong> number of elements from <code>nums</code>βββββββ without making it <strong>empty</strong>.</p>
<p>Return the <strong>minimum</strong> number of elements to remove so that the remaining array is balanced.</p>
<p><strong>Note:</strong> An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[2] = 5</code> to get <code>nums = [2, 1]</code>.</li>
<li>Now <code>max = 2</code>, <code>min = 1</code> and <code>max <= min * k</code> as <code>2 <= 1 * 2</code>. Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,6,2,9], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[0] = 1</code> and <code>nums[3] = 9</code> to get <code>nums = [6, 2]</code>.</li>
<li>Now <code>max = 6</code>, <code>min = 2</code> and <code>max <= min * k</code> as <code>6 <= 2 * 3</code>. Thus, the answer is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since <code>nums</code> is already balanced as <code>6 <= 4 * 2</code>, no elements need to be removed.</li>
</ul>
</div>
<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>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
|
Array; Sorting; Sliding Window
|
Java
|
class Solution {
public int minRemoval(int[] nums, int k) {
Arrays.sort(nums);
int cnt = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
int j = n;
if (1L * nums[i] * k <= nums[n - 1]) {
j = Arrays.binarySearch(nums, nums[i] * k + 1);
j = j < 0 ? -j - 1 : j;
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
}
|
3,634
|
Minimum Removals to Balance Array
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An array is considered <strong>balanced</strong> if the value of its <strong>maximum</strong> element is <strong>at most</strong> <code>k</code> times the <strong>minimum</strong> element.</p>
<p>You may remove <strong>any</strong> number of elements from <code>nums</code>βββββββ without making it <strong>empty</strong>.</p>
<p>Return the <strong>minimum</strong> number of elements to remove so that the remaining array is balanced.</p>
<p><strong>Note:</strong> An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[2] = 5</code> to get <code>nums = [2, 1]</code>.</li>
<li>Now <code>max = 2</code>, <code>min = 1</code> and <code>max <= min * k</code> as <code>2 <= 1 * 2</code>. Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,6,2,9], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[0] = 1</code> and <code>nums[3] = 9</code> to get <code>nums = [6, 2]</code>.</li>
<li>Now <code>max = 6</code>, <code>min = 2</code> and <code>max <= min * k</code> as <code>6 <= 2 * 3</code>. Thus, the answer is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since <code>nums</code> is already balanced as <code>6 <= 4 * 2</code>, no elements need to be removed.</li>
</ul>
</div>
<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>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
|
Array; Sorting; Sliding Window
|
Python
|
class Solution:
def minRemoval(self, nums: List[int], k: int) -> int:
nums.sort()
cnt = 0
for i, x in enumerate(nums):
j = bisect_right(nums, k * x)
cnt = max(cnt, j - i)
return len(nums) - cnt
|
3,634
|
Minimum Removals to Balance Array
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>An array is considered <strong>balanced</strong> if the value of its <strong>maximum</strong> element is <strong>at most</strong> <code>k</code> times the <strong>minimum</strong> element.</p>
<p>You may remove <strong>any</strong> number of elements from <code>nums</code>βββββββ without making it <strong>empty</strong>.</p>
<p>Return the <strong>minimum</strong> number of elements to remove so that the remaining array is balanced.</p>
<p><strong>Note:</strong> An array of size 1 is considered balanced as its maximum and minimum are equal, and the condition always holds true.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,5], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[2] = 5</code> to get <code>nums = [2, 1]</code>.</li>
<li>Now <code>max = 2</code>, <code>min = 1</code> and <code>max <= min * k</code> as <code>2 <= 1 * 2</code>. Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,6,2,9], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Remove <code>nums[0] = 1</code> and <code>nums[3] = 9</code> to get <code>nums = [6, 2]</code>.</li>
<li>Now <code>max = 6</code>, <code>min = 2</code> and <code>max <= min * k</code> as <code>6 <= 2 * 3</code>. Thus, the answer is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,6], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Since <code>nums</code> is already balanced as <code>6 <= 4 * 2</code>, no elements need to be removed.</li>
</ul>
</div>
<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>9</sup></code></li>
<li><code>1 <= k <= 10<sup>5</sup></code></li>
</ul>
|
Array; Sorting; Sliding Window
|
TypeScript
|
function minRemoval(nums: number[], k: number): number {
nums.sort((a, b) => a - b);
const n = nums.length;
let cnt = 0;
for (let i = 0; i < n; ++i) {
let j = n;
if (nums[i] * k <= nums[n - 1]) {
const target = nums[i] * k + 1;
j = _.sortedIndexBy(nums, target, x => x);
}
cnt = Math.max(cnt, j - i);
}
return n - cnt;
}
|
3,635
|
Earliest Finish Time for Land and Water Rides II
|
Medium
|
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> – the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> – how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> – the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> – how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 → water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 → land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 → water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 → land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 → land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 → water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong>βββββββ</strong></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 10<sup>5</sup></code></li>
</ul>
|
Greedy; Array; Two Pointers; Binary Search; Sorting
|
C++
|
class Solution {
public:
int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
return min(x, y);
}
int calc(vector<int>& a1, vector<int>& t1, vector<int>& a2, vector<int>& t2) {
int minEnd = INT_MAX;
for (int i = 0; i < a1.size(); ++i) {
minEnd = min(minEnd, a1[i] + t1[i]);
}
int ans = INT_MAX;
for (int i = 0; i < a2.size(); ++i) {
ans = min(ans, max(minEnd, a2[i]) + t2[i]);
}
return ans;
}
};
|
3,635
|
Earliest Finish Time for Land and Water Rides II
|
Medium
|
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> – the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> – how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> – the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> – how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 → water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 → land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 → water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 → land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 → land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 → water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong>βββββββ</strong></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 10<sup>5</sup></code></li>
</ul>
|
Greedy; Array; Two Pointers; Binary Search; Sorting
|
Go
|
func earliestFinishTime(landStartTime []int, landDuration []int, waterStartTime []int, waterDuration []int) int {
x := calc(landStartTime, landDuration, waterStartTime, waterDuration)
y := calc(waterStartTime, waterDuration, landStartTime, landDuration)
return min(x, y)
}
func calc(a1 []int, t1 []int, a2 []int, t2 []int) int {
minEnd := math.MaxInt32
for i := 0; i < len(a1); i++ {
minEnd = min(minEnd, a1[i]+t1[i])
}
ans := math.MaxInt32
for i := 0; i < len(a2); i++ {
ans = min(ans, max(minEnd, a2[i])+t2[i])
}
return ans
}
|
3,635
|
Earliest Finish Time for Land and Water Rides II
|
Medium
|
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> – the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> – how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> – the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> – how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 → water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 → land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 → water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 → land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 → land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 → water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong>βββββββ</strong></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 10<sup>5</sup></code></li>
</ul>
|
Greedy; Array; Two Pointers; Binary Search; Sorting
|
Java
|
class Solution {
public int earliestFinishTime(
int[] landStartTime, int[] landDuration, int[] waterStartTime, int[] waterDuration) {
int x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
int y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
return Math.min(x, y);
}
private int calc(int[] a1, int[] t1, int[] a2, int[] t2) {
int minEnd = Integer.MAX_VALUE;
for (int i = 0; i < a1.length; ++i) {
minEnd = Math.min(minEnd, a1[i] + t1[i]);
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < a2.length; ++i) {
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
}
return ans;
}
}
|
3,635
|
Earliest Finish Time for Land and Water Rides II
|
Medium
|
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> – the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> – how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> – the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> – how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 → water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 → land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 → water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 → land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 → land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 → water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong>βββββββ</strong></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 10<sup>5</sup></code></li>
</ul>
|
Greedy; Array; Two Pointers; Binary Search; Sorting
|
Python
|
class Solution:
def earliestFinishTime(
self,
landStartTime: List[int],
landDuration: List[int],
waterStartTime: List[int],
waterDuration: List[int],
) -> int:
def calc(a1, t1, a2, t2):
min_end = min(a + t for a, t in zip(a1, t1))
return min(max(a, min_end) + t for a, t in zip(a2, t2))
x = calc(landStartTime, landDuration, waterStartTime, waterDuration)
y = calc(waterStartTime, waterDuration, landStartTime, landDuration)
return min(x, y)
|
3,635
|
Earliest Finish Time for Land and Water Rides II
|
Medium
|
<p data-end="143" data-start="53">You are given two categories of theme park attractions: <strong data-end="122" data-start="108">land rides</strong> and <strong data-end="142" data-start="127">water rides</strong>.</p>
<ul>
<li data-end="163" data-start="147"><strong data-end="161" data-start="147">Land rides</strong>
<ul>
<li data-end="245" data-start="168"><code data-end="186" data-start="168">landStartTime[i]</code> – the earliest time the <code>i<sup>th</sup></code> land ride can be boarded.</li>
<li data-end="306" data-start="250"><code data-end="267" data-start="250">landDuration[i]</code> – how long the <code>i<sup>th</sup></code> land ride lasts.</li>
</ul>
</li>
<li><strong data-end="325" data-start="310">Water rides</strong>
<ul>
<li><code data-end="351" data-start="332">waterStartTime[j]</code> – the earliest time the <code>j<sup>th</sup></code> water ride can be boarded.</li>
<li><code data-end="434" data-start="416">waterDuration[j]</code> – how long the <code>j<sup>th</sup></code> water ride lasts.</li>
</ul>
</li>
</ul>
<p data-end="569" data-start="476">A tourist must experience <strong data-end="517" data-start="502">exactly one</strong> ride from <strong data-end="536" data-start="528">each</strong> category, in <strong data-end="566" data-start="550">either order</strong>.</p>
<ul>
<li data-end="641" data-start="573">A ride may be started at its opening time or <strong data-end="638" data-start="618">any later moment</strong>.</li>
<li data-end="715" data-start="644">If a ride is started at time <code data-end="676" data-start="673">t</code>, it finishes at time <code data-end="712" data-start="698">t + duration</code>.</li>
<li data-end="834" data-start="718">Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.</li>
</ul>
<p data-end="917" data-start="836">Return the <strong data-end="873" data-start="847">earliest possible time</strong> at which the tourist can finish both rides.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul>
<li data-end="181" data-start="145">Plan A (land ride 0 → water ride 0):
<ul>
<li data-end="272" data-start="186">Start land ride 0 at time <code data-end="234" data-start="212">landStartTime[0] = 2</code>. Finish at <code data-end="271" data-start="246">2 + landDuration[0] = 6</code>.</li>
<li data-end="392" data-start="277">Water ride 0 opens at time <code data-end="327" data-start="304">waterStartTime[0] = 6</code>. Start immediately at <code data-end="353" data-start="350">6</code>, finish at <code data-end="391" data-start="365">6 + waterDuration[0] = 9</code>.</li>
</ul>
</li>
<li data-end="432" data-start="396">Plan B (water ride 0 → land ride 1):
<ul>
<li data-end="526" data-start="437">Start water ride 0 at time <code data-end="487" data-start="464">waterStartTime[0] = 6</code>. Finish at <code data-end="525" data-start="499">6 + waterDuration[0] = 9</code>.</li>
<li data-end="632" data-start="531">Land ride 1 opens at <code data-end="574" data-start="552">landStartTime[1] = 8</code>. Start at time <code data-end="593" data-start="590">9</code>, finish at <code data-end="631" data-start="605">9 + landDuration[1] = 10</code>.</li>
</ul>
</li>
<li data-end="672" data-start="636">Plan C (land ride 1 → water ride 0):
<ul>
<li data-end="763" data-start="677">Start land ride 1 at time <code data-end="725" data-start="703">landStartTime[1] = 8</code>. Finish at <code data-end="762" data-start="737">8 + landDuration[1] = 9</code>.</li>
<li data-end="873" data-start="768">Water ride 0 opened at <code data-end="814" data-start="791">waterStartTime[0] = 6</code>. Start at time <code data-end="833" data-start="830">9</code>, finish at <code data-end="872" data-start="845">9 + waterDuration[0] = 12</code>.</li>
</ul>
</li>
<li data-end="913" data-start="877">Plan D (water ride 0 → land ride 0):
<ul>
<li data-end="1007" data-start="918">Start water ride 0 at time <code data-end="968" data-start="945">waterStartTime[0] = 6</code>. Finish at <code data-end="1006" data-start="980">6 + waterDuration[0] = 9</code>.</li>
<li data-end="1114" data-start="1012">Land ride 0 opened at <code data-end="1056" data-start="1034">landStartTime[0] = 2</code>. Start at time <code data-end="1075" data-start="1072">9</code>, finish at <code data-end="1113" data-start="1087">9 + landDuration[0] = 13</code>.</li>
</ul>
</li>
</ul>
<p data-end="1161" data-is-last-node="" data-is-only-node="" data-start="1116">Plan A gives the earliest finish time of 9.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">14</span></p>
<p><strong>Explanation:</strong>βββββββ</p>
<ul data-end="1589" data-start="1086">
<li data-end="1124" data-start="1088">Plan A (water ride 0 → land ride 0):
<ul>
<li data-end="1219" data-start="1129">Start water ride 0 at time <code data-end="1179" data-start="1156">waterStartTime[0] = 1</code>. Finish at <code data-end="1218" data-start="1191">1 + waterDuration[0] = 11</code>.</li>
<li data-end="1338" data-start="1224">Land ride 0 opened at <code data-end="1268" data-start="1246">landStartTime[0] = 5</code>. Start immediately at <code data-end="1295" data-start="1291">11</code> and finish at <code data-end="1337" data-start="1310">11 + landDuration[0] = 14</code>.</li>
</ul>
</li>
<li data-end="1378" data-start="1342">Plan B (land ride 0 → water ride 0):
<ul>
<li data-end="1469" data-start="1383">Start land ride 0 at time <code data-end="1431" data-start="1409">landStartTime[0] = 5</code>. Finish at <code data-end="1468" data-start="1443">5 + landDuration[0] = 8</code>.</li>
<li data-end="1589" data-start="1474">Water ride 0 opened at <code data-end="1520" data-start="1497">waterStartTime[0] = 1</code>. Start immediately at <code data-end="1546" data-start="1543">8</code> and finish at <code data-end="1588" data-start="1561">8 + waterDuration[0] = 18</code>.</li>
</ul>
</li>
</ul>
<p data-end="1640" data-is-last-node="" data-is-only-node="" data-start="1591">Plan A provides the earliest finish time of 14.<strong>βββββββ</strong></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="38" data-start="16"><code data-end="36" data-start="16">1 <= n, m <= 5 * 10<sup>4</sup></code></li>
<li data-end="93" data-start="41"><code data-end="91" data-start="41">landStartTime.length == landDuration.length == n</code></li>
<li data-end="150" data-start="96"><code data-end="148" data-start="96">waterStartTime.length == waterDuration.length == m</code></li>
<li data-end="237" data-start="153"><code data-end="235" data-start="153">1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 10<sup>5</sup></code></li>
</ul>
|
Greedy; Array; Two Pointers; Binary Search; Sorting
|
TypeScript
|
function earliestFinishTime(
landStartTime: number[],
landDuration: number[],
waterStartTime: number[],
waterDuration: number[],
): number {
const x = calc(landStartTime, landDuration, waterStartTime, waterDuration);
const y = calc(waterStartTime, waterDuration, landStartTime, landDuration);
return Math.min(x, y);
}
function calc(a1: number[], t1: number[], a2: number[], t2: number[]): number {
let minEnd = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < a1.length; i++) {
minEnd = Math.min(minEnd, a1[i] + t1[i]);
}
let ans = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < a2.length; i++) {
ans = Math.min(ans, Math.max(minEnd, a2[i]) + t2[i]);
}
return ans;
}
|
3,637
|
Trionic Array I
|
Easy
|
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 < p < q < n − 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n − 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 < 3 < 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 > 4 > 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 < 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 <= n <= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 <= nums[i] <= 1000</code></li>
</ul>
|
Array
|
C++
|
class Solution {
public:
bool isTrionic(vector<int>& nums) {
int n = nums.size();
int p = 0;
while (p < n - 2 && nums[p] < nums[p + 1]) {
p++;
}
if (p == 0) {
return false;
}
int q = p;
while (q < n - 1 && nums[q] > nums[q + 1]) {
q++;
}
if (q == p || q == n - 1) {
return false;
}
while (q < n - 1 && nums[q] < nums[q + 1]) {
q++;
}
return q == n - 1;
}
};
|
3,637
|
Trionic Array I
|
Easy
|
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 < p < q < n − 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n − 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 < 3 < 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 > 4 > 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 < 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 <= n <= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 <= nums[i] <= 1000</code></li>
</ul>
|
Array
|
Go
|
func isTrionic(nums []int) bool {
n := len(nums)
p := 0
for p < n-2 && nums[p] < nums[p+1] {
p++
}
if p == 0 {
return false
}
q := p
for q < n-1 && nums[q] > nums[q+1] {
q++
}
if q == p || q == n-1 {
return false
}
for q < n-1 && nums[q] < nums[q+1] {
q++
}
return q == n-1
}
|
3,637
|
Trionic Array I
|
Easy
|
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 < p < q < n − 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n − 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 < 3 < 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 > 4 > 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 < 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 <= n <= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 <= nums[i] <= 1000</code></li>
</ul>
|
Array
|
Java
|
class Solution {
public boolean isTrionic(int[] nums) {
int n = nums.length;
int p = 0;
while (p < n - 2 && nums[p] < nums[p + 1]) {
p++;
}
if (p == 0) {
return false;
}
int q = p;
while (q < n - 1 && nums[q] > nums[q + 1]) {
q++;
}
if (q == p || q == n - 1) {
return false;
}
while (q < n - 1 && nums[q] < nums[q + 1]) {
q++;
}
return q == n - 1;
}
}
|
3,637
|
Trionic Array I
|
Easy
|
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 < p < q < n − 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n − 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 < 3 < 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 > 4 > 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 < 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 <= n <= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 <= nums[i] <= 1000</code></li>
</ul>
|
Array
|
Python
|
class Solution:
def isTrionic(self, nums: List[int]) -> bool:
n = len(nums)
p = 0
while p < n - 2 and nums[p] < nums[p + 1]:
p += 1
if p == 0:
return False
q = p
while q < n - 1 and nums[q] > nums[q + 1]:
q += 1
if q == p or q == n - 1:
return False
while q < n - 1 and nums[q] < nums[q + 1]:
q += 1
return q == n - 1
|
3,637
|
Trionic Array I
|
Easy
|
<p data-end="128" data-start="0">You are given an integer array <code data-end="37" data-start="31">nums</code> of length <code data-end="51" data-start="48">n</code>.</p>
<p data-end="128" data-start="0">An array is <strong data-end="76" data-start="65">trionic</strong> if there exist indices <code data-end="117" data-start="100">0 < p < q < n − 1</code> such that:</p>
<ul>
<li data-end="170" data-start="132"><code data-end="144" data-start="132">nums[0...p]</code> is <strong>strictly</strong> increasing,</li>
<li data-end="211" data-start="173"><code data-end="185" data-start="173">nums[p...q]</code> is <strong>strictly</strong> decreasing,</li>
<li data-end="252" data-start="214"><code data-end="228" data-start="214">nums[q...n − 1]</code> is <strong>strictly</strong> increasing.</li>
</ul>
<p data-end="315" data-is-last-node="" data-is-only-node="" data-start="254">Return <code data-end="267" data-start="261">true</code> if <code data-end="277" data-start="271">nums</code> is trionic, otherwise return <code data-end="314" data-start="307">false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,5,4,2,6]</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>Pick <code data-end="91" data-start="84">p = 2</code>, <code data-end="100" data-start="93">q = 4</code>:</p>
<ul>
<li><code data-end="130" data-start="108">nums[0...2] = [1, 3, 5]</code> is strictly increasing (<code data-end="166" data-start="155">1 < 3 < 5</code>).</li>
<li><code data-end="197" data-start="175">nums[2...4] = [5, 4, 2]</code> is strictly decreasing (<code data-end="233" data-start="222">5 > 4 > 2</code>).</li>
<li><code data-end="262" data-start="242">nums[4...5] = [2, 6]</code> is strictly increasing (<code data-end="294" data-start="287">2 < 6</code>).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to pick <code>p</code> and <code>q</code> to form the required three segments.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="41" data-start="26"><code data-end="39" data-start="26">3 <= n <= 100</code></li>
<li data-end="70" data-start="44"><code data-end="70" data-start="44">-1000 <= nums[i] <= 1000</code></li>
</ul>
|
Array
|
TypeScript
|
function isTrionic(nums: number[]): boolean {
const n = nums.length;
let p = 0;
while (p < n - 2 && nums[p] < nums[p + 1]) {
p++;
}
if (p === 0) {
return false;
}
let q = p;
while (q < n - 1 && nums[q] > nums[q + 1]) {
q++;
}
if (q === p || q === n - 1) {
return false;
}
while (q < n - 1 && nums[q] < nums[q + 1]) {
q++;
}
return q === n - 1;
}
|
3,638
|
Maximum Balanced Shipments
|
Medium
|
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10<sup>9</sup></code></li>
</ul>
|
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
|
C++
|
class Solution {
public:
int maxBalancedShipments(vector<int>& weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
};
|
3,638
|
Maximum Balanced Shipments
|
Medium
|
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10<sup>9</sup></code></li>
</ul>
|
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
|
Go
|
func maxBalancedShipments(weight []int) (ans int) {
mx := 0
for _, x := range weight {
mx = max(mx, x)
if x < mx {
ans++
mx = 0
}
}
return
}
|
3,638
|
Maximum Balanced Shipments
|
Medium
|
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10<sup>9</sup></code></li>
</ul>
|
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
|
Java
|
class Solution {
public int maxBalancedShipments(int[] weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = Math.max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
}
|
3,638
|
Maximum Balanced Shipments
|
Medium
|
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10<sup>9</sup></code></li>
</ul>
|
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
|
Python
|
class Solution:
def maxBalancedShipments(self, weight: List[int]) -> int:
ans = mx = 0
for x in weight:
mx = max(mx, x)
if x < mx:
ans += 1
mx = 0
return ans
|
3,638
|
Maximum Balanced Shipments
|
Medium
|
<p data-end="365" data-start="23">You are given an integer array <code data-end="62" data-start="54">weight</code> of length <code data-end="76" data-start="73">n</code>, representing the weights of <code data-end="109" data-start="106">n</code> parcels arranged in a straight line. A <strong data-end="161" data-start="149">shipment</strong> is defined as a contiguous subarray of parcels. A shipment is considered <strong data-end="247" data-start="235">balanced</strong> if the weight of the <strong data-end="284" data-start="269">last parcel</strong> is <strong>strictly less</strong> than the <strong data-end="329" data-start="311">maximum weight</strong> among all parcels in that shipment.</p>
<p data-end="528" data-start="371">Select a set of <strong data-end="406" data-start="387">non-overlapping</strong>, contiguous, balanced shipments such that <strong data-end="496" data-start="449">each parcel appears in at most one shipment</strong> (parcels may remain unshipped).</p>
<p data-end="587" data-start="507">Return the <strong data-end="545" data-start="518">maximum possible number</strong> of balanced shipments that can be formed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [2,5,1,4,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="136" data-start="62">We can form the maximum of two balanced shipments as follows:</p>
<ul>
<li data-end="163" data-start="140">Shipment 1: <code>[2, 5, 1]</code>
<ul>
<li data-end="195" data-start="168">Maximum parcel weight = 5</li>
<li data-end="275" data-start="200">Last parcel weight = 1, which is strictly less than 5. Thus, it's balanced.</li>
</ul>
</li>
<li data-end="299" data-start="279">Shipment 2: <code>[4, 3]</code>
<ul>
<li data-end="331" data-start="304">Maximum parcel weight = 4</li>
<li data-end="411" data-start="336">Last parcel weight = 3, which is strictly less than 4. Thus, it's balanced.</li>
</ul>
</li>
</ul>
<p data-end="519" data-start="413">It is impossible to partition the parcels to achieve more than two balanced shipments, so the answer is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weight = [4,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="635" data-start="574">No balanced shipment can be formed in this case:</p>
<ul>
<li data-end="772" data-start="639">A shipment <code>[4, 4]</code> has maximum weight 4 and the last parcel's weight is also 4, which is not strictly less. Thus, it's not balanced.</li>
<li data-end="885" data-start="775">Single-parcel shipments <code>[4]</code> have the last parcel weight equal to the maximum parcel weight, thus not balanced.</li>
</ul>
<p data-end="958" data-is-last-node="" data-is-only-node="" data-start="887">As there is no way to form even one balanced shipment, the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="8706" data-start="8671"><code data-end="8704" data-start="8671">2 <= n <= 10<sup>5</sup></code></li>
<li data-end="8733" data-start="8709"><code data-end="8733" data-start="8709">1 <= weight[i] <= 10<sup>9</sup></code></li>
</ul>
|
Stack; Greedy; Array; Dynamic Programming; Monotonic Stack
|
TypeScript
|
function maxBalancedShipments(weight: number[]): number {
let [ans, mx] = [0, 0];
for (const x of weight) {
mx = Math.max(mx, x);
if (x < mx) {
ans++;
mx = 0;
}
}
return ans;
}
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
C++
|
class Solution {
public:
int longestSubarray(vector<int>& nums, int k) {
unordered_map<int, int> cnt;
int ans = 0, cur = 0, l = 0;
for (int r = 0; r < nums.size(); ++r) {
if (++cnt[nums[r]] == 2) {
++cur;
}
while (cur > k) {
if (--cnt[nums[l++]] == 1) {
--cur;
}
}
ans = max(ans, r - l + 1);
}
return ans;
}
};
|
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
Go
|
func longestSubarray(nums []int, k int) (ans int) {
cnt := make(map[int]int)
cur, l := 0, 0
for r := 0; r < len(nums); r++ {
if cnt[nums[r]]++; cnt[nums[r]] == 2 {
cur++
}
for cur > k {
if cnt[nums[l]]--; cnt[nums[l]] == 1 {
cur--
}
l++
}
ans = max(ans, r-l+1)
}
return
}
|
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
Java
|
class Solution {
public int longestSubarray(int[] nums, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
int ans = 0, cur = 0, l = 0;
for (int r = 0; r < nums.length; ++r) {
if (cnt.merge(nums[r], 1, Integer::sum) == 2) {
++cur;
}
while (cur > k) {
if (cnt.merge(nums[l++], -1, Integer::sum) == 1) {
--cur;
}
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
|
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
Python
|
class Solution:
def longestSubarray(self, nums: List[int], k: int) -> int:
cnt = defaultdict(int)
ans = cur = l = 0
for r, x in enumerate(nums):
cnt[x] += 1
cur += cnt[x] == 2
while cur > k:
cnt[nums[l]] -= 1
cur -= cnt[nums[l]] == 1
l += 1
ans = max(ans, r - l + 1)
return ans
|
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
Rust
|
use std::collections::HashMap;
impl Solution {
pub fn longest_subarray(nums: Vec<i32>, k: i32) -> i32 {
let mut cnt = HashMap::new();
let mut ans = 0;
let mut cur = 0;
let mut l = 0;
for r in 0..nums.len() {
let entry = cnt.entry(nums[r]).or_insert(0);
*entry += 1;
if *entry == 2 {
cur += 1;
}
while cur > k {
let entry = cnt.entry(nums[l]).or_insert(0);
*entry -= 1;
if *entry == 1 {
cur -= 1;
}
l += 1;
}
ans = ans.max(r - l + 1);
}
ans as i32
}
}
|
|
3,641
|
Longest Semi-Repeating Subarray
|
Medium
|
<p>You are given an integer arrayβ―<code>nums</code> of lengthβ―<code>n</code> and an integerβ―<code>k</code>.</p>
<p>A <strong>semiβrepeating</strong> subarray is a contiguous subarray in which at mostβ―<code>k</code>β―elements repeat (i.e., appear more than once).</p>
<p>Return the length of the longest <strong>semiβrepeating</strong> subarray inβ―<code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[2, 3, 1, 2, 3, 4]</code>, which has two repeating elements (2 and 3).</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1, 1, 1, 1, 1]</code>, which has only one repeating element (1).</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1,1], k = 0</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>The longest semi-repeating subarray is <code>[1]</code>, which has no repeating elements.</p>
</div>
<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>
<li><code>0 <= k <= nums.length</code></li>
</ul>
|
TypeScript
|
function longestSubarray(nums: number[], k: number): number {
const cnt: Map<number, number> = new Map();
let [ans, cur, l] = [0, 0, 0];
for (let r = 0; r < nums.length; r++) {
cnt.set(nums[r], (cnt.get(nums[r]) || 0) + 1);
if (cnt.get(nums[r]) === 2) {
cur++;
}
while (cur > k) {
cnt.set(nums[l], cnt.get(nums[l])! - 1);
if (cnt.get(nums[l]) === 1) {
cur--;
}
l++;
}
ans = Math.max(ans, r - l + 1);
}
return ans;
}
|
|
3,642
|
Find Books with Polarized Opinions
|
Easy
|
<p>Table: <code>books</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| book_id | int |
| title | varchar |
| author | varchar |
| genre | varchar |
| pages | int |
+-------------+---------+
book_id is the unique ID for this table.
Each row contains information about a book including its genre and page count.
</pre>
<p>Table: <code>reading_sessions</code></p>
<pre>
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| session_id | int |
| book_id | int |
| reader_name | varchar |
| pages_read | int |
| session_rating | int |
+----------------+---------+
session_id is the unique ID for this table.
Each row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5.
</pre>
<p>Write a solution to find books that have <strong>polarized opinions</strong> - books that receive both very high ratings and very low ratings from different readers.</p>
<ul>
<li>A book has polarized opinions if it has <code>at least one rating ≥ 4</code> and <code>at least one rating ≤ 2</code></li>
<li>Only consider books that have <strong>at least </strong><code>5</code><strong> reading sessions</strong></li>
<li>Calculate the <strong>rating spread</strong> as (<code>highest_rating - lowest_rating</code>)</li>
<li>Calculate the <strong>polarization score</strong> as the number of extreme ratings (<code>ratings ≤ 2 or ≥ 4</code>) divided by total sessions</li>
<li><strong>Only include</strong> books where <code>polarization score ≥ 0.6</code> (at least <code>60%</code> extreme ratings)</li>
</ul>
<p>Return <em>the result table ordered by polarization score in <strong>descending</strong> order, then by title in <strong>descending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>books table:</p>
<pre class="example-io">
+---------+------------------------+---------------+----------+-------+
| book_id | title | author | genre | pages |
+---------+------------------------+---------------+----------+-------+
| 1 | The Great Gatsby | F. Scott | Fiction | 180 |
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 281 |
| 3 | 1984 | George Orwell | Dystopian| 328 |
| 4 | Pride and Prejudice | Jane Austen | Romance | 432 |
| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 277 |
+---------+------------------------+---------------+----------+-------+
</pre>
<p>reading_sessions table:</p>
<pre class="example-io">
+------------+---------+-------------+------------+----------------+
| session_id | book_id | reader_name | pages_read | session_rating |
+------------+---------+-------------+------------+----------------+
| 1 | 1 | Alice | 50 | 5 |
| 2 | 1 | Bob | 60 | 1 |
| 3 | 1 | Carol | 40 | 4 |
| 4 | 1 | David | 30 | 2 |
| 5 | 1 | Emma | 45 | 5 |
| 6 | 2 | Frank | 80 | 4 |
| 7 | 2 | Grace | 70 | 4 |
| 8 | 2 | Henry | 90 | 5 |
| 9 | 2 | Ivy | 60 | 4 |
| 10 | 2 | Jack | 75 | 4 |
| 11 | 3 | Kate | 100 | 2 |
| 12 | 3 | Liam | 120 | 1 |
| 13 | 3 | Mia | 80 | 2 |
| 14 | 3 | Noah | 90 | 1 |
| 15 | 3 | Olivia | 110 | 4 |
| 16 | 3 | Paul | 95 | 5 |
| 17 | 4 | Quinn | 150 | 3 |
| 18 | 4 | Ruby | 140 | 3 |
| 19 | 5 | Sam | 80 | 1 |
| 20 | 5 | Tara | 70 | 2 |
+------------+---------+-------------+------------+----------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
| book_id | title | author | genre | pages | rating_spread | polarization_score |
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
| 1 | The Great Gatsby | F. Scott | Fiction | 180 | 4 | 1.00 |
| 3 | 1984 | George Orwell | Dystopian | 328 | 4 | 1.00 |
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>The Great Gatsby (book_id = 1):</strong>
<ul>
<li>Has 5 reading sessions (meets minimum requirement)</li>
<li>Ratings: 5, 1, 4, 2, 5</li>
<li>Has ratings ≥ 4: 5, 4, 5 (3 sessions)</li>
<li>Has ratings ≤ 2: 1, 2 (2 sessions)</li>
<li>Rating spread: 5 - 1 = 4</li>
<li>Extreme ratings (≤2 or ≥4): All 5 sessions (5, 1, 4, 2, 5)</li>
<li>Polarization score: 5/5 = 1.00 (≥ 0.6, qualifies)</li>
</ul>
</li>
<li><strong>1984 (book_id = 3):</strong>
<ul>
<li>Has 6 reading sessions (meets minimum requirement)</li>
<li>Ratings: 2, 1, 2, 1, 4, 5</li>
<li>Has ratings ≥ 4: 4, 5 (2 sessions)</li>
<li>Has ratings ≤ 2: 2, 1, 2, 1 (4 sessions)</li>
<li>Rating spread: 5 - 1 = 4</li>
<li>Extreme ratings (≤2 or ≥4): All 6 sessions (2, 1, 2, 1, 4, 5)</li>
<li>Polarization score: 6/6 = 1.00 (≥ 0.6, qualifies)</li>
</ul>
</li>
<li><strong>Books not included:</strong>
<ul>
<li>To Kill a Mockingbird (book_id = 2): All ratings are 4-5, no low ratings (≤2)</li>
<li>Pride and Prejudice (book_id = 4): Only 2 sessions (< 5 minimum)</li>
<li>The Catcher in the Rye (book_id = 5): Only 2 sessions (< 5 minimum)</li>
</ul>
</li>
</ul>
<p>The result table is ordered by polarization score in descending order, then by book title in descending order.</p>
</div>
|
Database
|
Python
|
import pandas as pd
from decimal import Decimal, ROUND_HALF_UP
def find_polarized_books(
books: pd.DataFrame, reading_sessions: pd.DataFrame
) -> pd.DataFrame:
df = books.merge(reading_sessions, on="book_id")
agg_df = (
df.groupby(["book_id", "title", "author", "genre", "pages"])
.agg(
max_rating=("session_rating", "max"),
min_rating=("session_rating", "min"),
rating_spread=("session_rating", lambda x: x.max() - x.min()),
count_sessions=("session_rating", "count"),
low_or_high_count=("session_rating", lambda x: ((x <= 2) | (x >= 4)).sum()),
)
.reset_index()
)
agg_df["polarization_score"] = agg_df.apply(
lambda r: float(
Decimal(r["low_or_high_count"] / r["count_sessions"]).quantize(
Decimal("0.01"), rounding=ROUND_HALF_UP
)
),
axis=1,
)
result = agg_df[
(agg_df["count_sessions"] >= 5)
& (agg_df["max_rating"] >= 4)
& (agg_df["min_rating"] <= 2)
& (agg_df["polarization_score"] >= 0.6)
]
return result.sort_values(
by=["polarization_score", "title"], ascending=[False, False]
)[
[
"book_id",
"title",
"author",
"genre",
"pages",
"rating_spread",
"polarization_score",
]
]
|
3,642
|
Find Books with Polarized Opinions
|
Easy
|
<p>Table: <code>books</code></p>
<pre>
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| book_id | int |
| title | varchar |
| author | varchar |
| genre | varchar |
| pages | int |
+-------------+---------+
book_id is the unique ID for this table.
Each row contains information about a book including its genre and page count.
</pre>
<p>Table: <code>reading_sessions</code></p>
<pre>
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| session_id | int |
| book_id | int |
| reader_name | varchar |
| pages_read | int |
| session_rating | int |
+----------------+---------+
session_id is the unique ID for this table.
Each row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5.
</pre>
<p>Write a solution to find books that have <strong>polarized opinions</strong> - books that receive both very high ratings and very low ratings from different readers.</p>
<ul>
<li>A book has polarized opinions if it has <code>at least one rating ≥ 4</code> and <code>at least one rating ≤ 2</code></li>
<li>Only consider books that have <strong>at least </strong><code>5</code><strong> reading sessions</strong></li>
<li>Calculate the <strong>rating spread</strong> as (<code>highest_rating - lowest_rating</code>)</li>
<li>Calculate the <strong>polarization score</strong> as the number of extreme ratings (<code>ratings ≤ 2 or ≥ 4</code>) divided by total sessions</li>
<li><strong>Only include</strong> books where <code>polarization score ≥ 0.6</code> (at least <code>60%</code> extreme ratings)</li>
</ul>
<p>Return <em>the result table ordered by polarization score in <strong>descending</strong> order, then by title in <strong>descending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>books table:</p>
<pre class="example-io">
+---------+------------------------+---------------+----------+-------+
| book_id | title | author | genre | pages |
+---------+------------------------+---------------+----------+-------+
| 1 | The Great Gatsby | F. Scott | Fiction | 180 |
| 2 | To Kill a Mockingbird | Harper Lee | Fiction | 281 |
| 3 | 1984 | George Orwell | Dystopian| 328 |
| 4 | Pride and Prejudice | Jane Austen | Romance | 432 |
| 5 | The Catcher in the Rye | J.D. Salinger | Fiction | 277 |
+---------+------------------------+---------------+----------+-------+
</pre>
<p>reading_sessions table:</p>
<pre class="example-io">
+------------+---------+-------------+------------+----------------+
| session_id | book_id | reader_name | pages_read | session_rating |
+------------+---------+-------------+------------+----------------+
| 1 | 1 | Alice | 50 | 5 |
| 2 | 1 | Bob | 60 | 1 |
| 3 | 1 | Carol | 40 | 4 |
| 4 | 1 | David | 30 | 2 |
| 5 | 1 | Emma | 45 | 5 |
| 6 | 2 | Frank | 80 | 4 |
| 7 | 2 | Grace | 70 | 4 |
| 8 | 2 | Henry | 90 | 5 |
| 9 | 2 | Ivy | 60 | 4 |
| 10 | 2 | Jack | 75 | 4 |
| 11 | 3 | Kate | 100 | 2 |
| 12 | 3 | Liam | 120 | 1 |
| 13 | 3 | Mia | 80 | 2 |
| 14 | 3 | Noah | 90 | 1 |
| 15 | 3 | Olivia | 110 | 4 |
| 16 | 3 | Paul | 95 | 5 |
| 17 | 4 | Quinn | 150 | 3 |
| 18 | 4 | Ruby | 140 | 3 |
| 19 | 5 | Sam | 80 | 1 |
| 20 | 5 | Tara | 70 | 2 |
+------------+---------+-------------+------------+----------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
| book_id | title | author | genre | pages | rating_spread | polarization_score |
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
| 1 | The Great Gatsby | F. Scott | Fiction | 180 | 4 | 1.00 |
| 3 | 1984 | George Orwell | Dystopian | 328 | 4 | 1.00 |
+---------+------------------+---------------+-----------+-------+---------------+--------------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>The Great Gatsby (book_id = 1):</strong>
<ul>
<li>Has 5 reading sessions (meets minimum requirement)</li>
<li>Ratings: 5, 1, 4, 2, 5</li>
<li>Has ratings ≥ 4: 5, 4, 5 (3 sessions)</li>
<li>Has ratings ≤ 2: 1, 2 (2 sessions)</li>
<li>Rating spread: 5 - 1 = 4</li>
<li>Extreme ratings (≤2 or ≥4): All 5 sessions (5, 1, 4, 2, 5)</li>
<li>Polarization score: 5/5 = 1.00 (≥ 0.6, qualifies)</li>
</ul>
</li>
<li><strong>1984 (book_id = 3):</strong>
<ul>
<li>Has 6 reading sessions (meets minimum requirement)</li>
<li>Ratings: 2, 1, 2, 1, 4, 5</li>
<li>Has ratings ≥ 4: 4, 5 (2 sessions)</li>
<li>Has ratings ≤ 2: 2, 1, 2, 1 (4 sessions)</li>
<li>Rating spread: 5 - 1 = 4</li>
<li>Extreme ratings (≤2 or ≥4): All 6 sessions (2, 1, 2, 1, 4, 5)</li>
<li>Polarization score: 6/6 = 1.00 (≥ 0.6, qualifies)</li>
</ul>
</li>
<li><strong>Books not included:</strong>
<ul>
<li>To Kill a Mockingbird (book_id = 2): All ratings are 4-5, no low ratings (≤2)</li>
<li>Pride and Prejudice (book_id = 4): Only 2 sessions (< 5 minimum)</li>
<li>The Catcher in the Rye (book_id = 5): Only 2 sessions (< 5 minimum)</li>
</ul>
</li>
</ul>
<p>The result table is ordered by polarization score in descending order, then by book title in descending order.</p>
</div>
|
Database
|
SQL
|
# Write your MySQL query statement below
SELECT
book_id,
title,
author,
genre,
pages,
(MAX(session_rating) - MIN(session_rating)) AS rating_spread,
ROUND((SUM(session_rating <= 2) + SUM(session_rating >= 4)) / COUNT(1), 2) polarization_score
FROM
books
JOIN reading_sessions USING (book_id)
GROUP BY book_id
HAVING
COUNT(1) >= 5
AND MAX(session_rating) >= 4
AND MIN(session_rating) <= 2
AND polarization_score >= 0.6
ORDER BY polarization_score DESC, title DESC;
|
3,643
|
Flip Square Submatrix Vertically
|
Easy
|
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexm2drawio.png" style="width: 350px; height: 68px;" />βββββββ
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
<li><code>0 <= x < m</code></li>
<li><code>0 <= y < n</code></li>
<li><code>1 <= k <= min(m - x, n - y)</code></li>
</ul>
|
Array; Two Pointers; Matrix
|
C++
|
class Solution {
public:
vector<vector<int>> reverseSubmatrix(vector<vector<int>>& grid, int x, int y, int k) {
for (int i = x; i < x + k / 2; i++) {
int i2 = x + k - 1 - (i - x);
for (int j = y; j < y + k; j++) {
swap(grid[i][j], grid[i2][j]);
}
}
return grid;
}
};
|
3,643
|
Flip Square Submatrix Vertically
|
Easy
|
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexm2drawio.png" style="width: 350px; height: 68px;" />βββββββ
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
<li><code>0 <= x < m</code></li>
<li><code>0 <= y < n</code></li>
<li><code>1 <= k <= min(m - x, n - y)</code></li>
</ul>
|
Array; Two Pointers; Matrix
|
Go
|
func reverseSubmatrix(grid [][]int, x int, y int, k int) [][]int {
for i := x; i < x+k/2; i++ {
i2 := x + k - 1 - (i - x)
for j := y; j < y+k; j++ {
grid[i][j], grid[i2][j] = grid[i2][j], grid[i][j]
}
}
return grid
}
|
3,643
|
Flip Square Submatrix Vertically
|
Easy
|
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexm2drawio.png" style="width: 350px; height: 68px;" />βββββββ
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
<li><code>0 <= x < m</code></li>
<li><code>0 <= y < n</code></li>
<li><code>1 <= k <= min(m - x, n - y)</code></li>
</ul>
|
Array; Two Pointers; Matrix
|
Java
|
class Solution {
public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
for (int i = x; i < x + k / 2; i++) {
int i2 = x + k - 1 - (i - x);
for (int j = y; j < y + k; j++) {
int t = grid[i][j];
grid[i][j] = grid[i2][j];
grid[i2][j] = t;
}
}
return grid;
}
}
|
3,643
|
Flip Square Submatrix Vertically
|
Easy
|
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexm2drawio.png" style="width: 350px; height: 68px;" />βββββββ
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
<li><code>0 <= x < m</code></li>
<li><code>0 <= y < n</code></li>
<li><code>1 <= k <= min(m - x, n - y)</code></li>
</ul>
|
Array; Two Pointers; Matrix
|
Python
|
class Solution:
def reverseSubmatrix(
self, grid: List[List[int]], x: int, y: int, k: int
) -> List[List[int]]:
for i in range(x, x + k // 2):
i2 = x + k - 1 - (i - x)
for j in range(y, y + k):
grid[i][j], grid[i2][j] = grid[i2][j], grid[i][j]
return grid
|
3,643
|
Flip Square Submatrix Vertically
|
Easy
|
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, and three integers <code>x</code>, <code>y</code>, and <code>k</code>.</p>
<p>The integers <code>x</code> and <code>y</code> represent the row and column indices of the <strong>top-left</strong> corner of a <strong>square</strong> submatrix and the integer <code>k</code> represents the size (side length) of the square submatrix.</p>
<p>Your task is to flip the submatrix by reversing the order of its rows vertically.</p>
<p>Return the updated matrix.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexmdrawio.png" style="width: 300px; height: 116px;" />
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = </span>[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]<span class="example-io">, x = 1, y = 0, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[[1,2,3,4],[13,14,15,8],[9,10,11,12],[5,6,7,16]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3643.Flip%20Square%20Submatrix%20Vertically/images/gridexm2drawio.png" style="width: 350px; height: 68px;" />βββββββ
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[3,4,2,3],[2,3,4,2]], x = 0, y = 2, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[[3,4,4,2],[2,3,2,3]]</span></p>
<p><strong>Explanation:</strong></p>
<p>The diagram above shows the grid before and after the transformation.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 50</code></li>
<li><code>1 <= grid[i][j] <= 100</code></li>
<li><code>0 <= x < m</code></li>
<li><code>0 <= y < n</code></li>
<li><code>1 <= k <= min(m - x, n - y)</code></li>
</ul>
|
Array; Two Pointers; Matrix
|
TypeScript
|
function reverseSubmatrix(grid: number[][], x: number, y: number, k: number): number[][] {
for (let i = x; i < x + Math.floor(k / 2); i++) {
const i2 = x + k - 1 - (i - x);
for (let j = y; j < y + k; j++) {
[grid[i][j], grid[i2][j]] = [grid[i2][j], grid[i][j]];
}
}
return grid;
}
|
3,644
|
Maximum K to Sort a Permutation
|
Medium
|
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>
|
Bit Manipulation; Array
|
C++
|
class Solution {
public:
int sortPermutation(vector<int>& nums) {
int ans = -1;
for (int i = 0; i < nums.size(); ++i) {
if (i != nums[i]) {
ans &= nums[i];
}
}
return max(ans, 0);
}
};
|
3,644
|
Maximum K to Sort a Permutation
|
Medium
|
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>
|
Bit Manipulation; Array
|
Go
|
func sortPermutation(nums []int) int {
ans := -1
for i, x := range nums {
if i != x {
ans &= x
}
}
return max(ans, 0)
}
|
3,644
|
Maximum K to Sort a Permutation
|
Medium
|
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>
|
Bit Manipulation; Array
|
Java
|
class Solution {
public int sortPermutation(int[] nums) {
int ans = -1;
for (int i = 0; i < nums.length; ++i) {
if (i != nums[i]) {
ans &= nums[i];
}
}
return Math.max(ans, 0);
}
}
|
3,644
|
Maximum K to Sort a Permutation
|
Medium
|
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>
|
Bit Manipulation; Array
|
Python
|
class Solution:
def sortPermutation(self, nums: List[int]) -> int:
ans = -1
for i, x in enumerate(nums):
if i != x:
ans &= x
return max(ans, 0)
|
3,644
|
Maximum K to Sort a Permutation
|
Medium
|
<p>You are given an integer array <code>nums</code> of length <code>n</code>, where <code>nums</code> is a <strong><span data-keyword="permutation-array">permutation</span></strong> of the numbers in the range <code>[0..n - 1]</code>.</p>
<p>You may swap elements at indices <code>i</code> and <code>j</code> <strong>only if</strong> <code>nums[i] AND nums[j] == k</code>, where <code>AND</code> denotes the bitwise AND operation and <code>k</code> is a <strong>non-negative</strong> integer.</p>
<p>Return the <strong>maximum</strong> value of <code>k</code> such that the array can be sorted in <strong>non-decreasing</strong> order using any number of such swaps. If <code>nums</code> is already sorted, return 0.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,3,2,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 1</code>. Swapping <code>nums[1] = 3</code> and <code>nums[3] = 1</code> is allowed since <code>nums[1] AND nums[3] == 1</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,1,3,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>Choose <code>k = 2</code>. Swapping <code>nums[2] = 3</code> and <code>nums[3] = 2</code> is allowed since <code>nums[2] AND nums[3] == 2</code>, resulting in a sorted permutation: <code>[0, 1, 2, 3]</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,2,1,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Only <code>k = 0</code> allows sorting since no greater <code>k</code> allows the required swaps where <code>nums[i] AND nums[j] == k</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= n - 1</code></li>
<li><code>nums</code> is a permutation of integers from <code>0</code> to <code>n - 1</code>.</li>
</ul>
|
Bit Manipulation; Array
|
TypeScript
|
function sortPermutation(nums: number[]): number {
let ans = -1;
for (let i = 0; i < nums.length; ++i) {
if (i != nums[i]) {
ans &= nums[i];
}
}
return Math.max(ans, 0);
}
|
3,645
|
Maximum Total from Optimal Activation Order
|
Medium
|
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] <= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[0, 1]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:βββββββ<strong>βββββββ</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == value.length == limit.length <= 10<sup>5</sup></code></li>
<li><code>1 <= value[i] <= 10<sup>5</sup></code>βββββββ</li>
<li><code>1 <= limit[i] <= n</code></li>
</ul>
|
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
|
C++
|
class Solution {
public:
long long maxTotal(vector<int>& value, vector<int>& limit) {
unordered_map<int, vector<int>> g;
int n = value.size();
for (int i = 0; i < n; ++i) {
g[limit[i]].push_back(value[i]);
}
long long ans = 0;
for (auto& [lim, vs] : g) {
sort(vs.begin(), vs.end(), greater<int>());
for (int i = 0; i < min(lim, (int) vs.size()); ++i) {
ans += vs[i];
}
}
return ans;
}
};
|
3,645
|
Maximum Total from Optimal Activation Order
|
Medium
|
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] <= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[0, 1]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:βββββββ<strong>βββββββ</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == value.length == limit.length <= 10<sup>5</sup></code></li>
<li><code>1 <= value[i] <= 10<sup>5</sup></code>βββββββ</li>
<li><code>1 <= limit[i] <= n</code></li>
</ul>
|
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
|
Go
|
func maxTotal(value []int, limit []int) (ans int64) {
g := make(map[int][]int)
for i := range value {
g[limit[i]] = append(g[limit[i]], value[i])
}
for lim, vs := range g {
slices.SortFunc(vs, func(a, b int) int { return b - a })
for i := 0; i < min(lim, len(vs)); i++ {
ans += int64(vs[i])
}
}
return
}
|
3,645
|
Maximum Total from Optimal Activation Order
|
Medium
|
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] <= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[0, 1]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:βββββββ<strong>βββββββ</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == value.length == limit.length <= 10<sup>5</sup></code></li>
<li><code>1 <= value[i] <= 10<sup>5</sup></code>βββββββ</li>
<li><code>1 <= limit[i] <= n</code></li>
</ul>
|
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
|
Java
|
class Solution {
public long maxTotal(int[] value, int[] limit) {
Map<Integer, List<Integer>> g = new HashMap<>();
for (int i = 0; i < value.length; ++i) {
g.computeIfAbsent(limit[i], k -> new ArrayList<>()).add(value[i]);
}
long ans = 0;
for (var e : g.entrySet()) {
int lim = e.getKey();
var vs = e.getValue();
vs.sort((a, b) -> b - a);
for (int i = 0; i < Math.min(lim, vs.size()); ++i) {
ans += vs.get(i);
}
}
return ans;
}
}
|
3,645
|
Maximum Total from Optimal Activation Order
|
Medium
|
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] <= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[0, 1]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:βββββββ<strong>βββββββ</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == value.length == limit.length <= 10<sup>5</sup></code></li>
<li><code>1 <= value[i] <= 10<sup>5</sup></code>βββββββ</li>
<li><code>1 <= limit[i] <= n</code></li>
</ul>
|
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
|
Python
|
class Solution:
def maxTotal(self, value: List[int], limit: List[int]) -> int:
g = defaultdict(list)
for v, lim in zip(value, limit):
g[lim].append(v)
ans = 0
for lim, vs in g.items():
vs.sort()
ans += sum(vs[-lim:])
return ans
|
3,645
|
Maximum Total from Optimal Activation Order
|
Medium
|
<p>You are given two integer arrays <code>value</code> and <code>limit</code>, both of length <code>n</code>.</p>
<p>Initially, all elements are <strong>inactive</strong>. You may activate them in any order.</p>
<ul>
<li>To activate an inactive element at index <code>i</code>, the number of <strong>currently</strong> active elements must be <strong>strictly less</strong> than <code>limit[i]</code>.</li>
<li>When you activate the element at index <code>i</code>, it adds <code>value[i]</code> to the <strong>total</strong> activation value (i.e., the sum of <code>value[i]</code> for all elements that have undergone activation operations).</li>
<li>After each activation, if the number of <strong>currently</strong> active elements becomes <code>x</code>, then <strong>all</strong> elements <code>j</code> with <code>limit[j] <= x</code> become <strong>permanently</strong> inactive, even if they are already active.</li>
</ul>
<p>Return the <strong>maximum</strong> <strong>total</strong> you can obtain by choosing the activation order optimally.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [3,5,8], limit = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table>
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 1</code> as <code>limit[1] = 1</code></td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1]</td>
<td align="center" style="border: 1px solid black;">8</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">8</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 0</code> as <code>limit[0] = 2</code></td>
<td align="center" style="border: 1px solid black;">[0, 1]</td>
<td align="center" style="border: 1px solid black;">16</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 16.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,2,6], limit = [1,1,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">6</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 2</code> as <code>limit[j] = 1</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2]</td>
<td align="center" style="border: 1px solid black;">6</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">value = [4,1,5,2], limit = [3,3,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>One optimal activation order is:βββββββ<strong>βββββββ</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;">Activated <code>i</code></th>
<th align="center" style="border: 1px solid black;"><code>value[i]</code></th>
<th align="center" style="border: 1px solid black;">Active Before <code>i</code></th>
<th align="center" style="border: 1px solid black;">Active After <code>i</code></th>
<th align="center" style="border: 1px solid black;">Becomes Inactive <code>j</code></th>
<th align="center" style="border: 1px solid black;">Inactive Elements</th>
<th align="center" style="border: 1px solid black;">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">5</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[ ]</td>
<td align="center" style="border: 1px solid black;">5</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;"><code>j = 2</code> as <code>limit[2] = 2</code></td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">9</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2]</td>
<td align="center" style="border: 1px solid black;">10</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;"><code>j = 0, 1, 3</code> as <code>limit[j] = 3</code></td>
<td align="center" style="border: 1px solid black;">[0, 1, 2, 3]</td>
<td align="center" style="border: 1px solid black;">12</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible total is 12.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == value.length == limit.length <= 10<sup>5</sup></code></li>
<li><code>1 <= value[i] <= 10<sup>5</sup></code>βββββββ</li>
<li><code>1 <= limit[i] <= n</code></li>
</ul>
|
Greedy; Array; Two Pointers; Sorting; Heap (Priority Queue)
|
TypeScript
|
function maxTotal(value: number[], limit: number[]): number {
const g = new Map<number, number[]>();
for (let i = 0; i < value.length; i++) {
if (!g.has(limit[i])) {
g.set(limit[i], []);
}
g.get(limit[i])!.push(value[i]);
}
let ans = 0;
for (const [lim, vs] of g) {
vs.sort((a, b) => b - a);
ans += vs.slice(0, lim).reduce((acc, v) => acc + v, 0);
}
return ans;
}
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
C++
|
class Solution {
public:
int maxWeight(vector<int>& weights, int w1, int w2) {
vector<vector<int>> f(w1 + 1, vector<int>(w2 + 1));
for (int x : weights) {
for (int j = w1; j >= 0; --j) {
for (int k = w2; k >= 0; --k) {
if (x <= j) {
f[j][k] = max(f[j][k], f[j - x][k] + x);
}
if (x <= k) {
f[j][k] = max(f[j][k], f[j][k - x] + x);
}
}
}
}
return f[w1][w2];
}
};
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
Go
|
func maxWeight(weights []int, w1 int, w2 int) int {
f := make([][]int, w1+1)
for i := range f {
f[i] = make([]int, w2+1)
}
for _, x := range weights {
for j := w1; j >= 0; j-- {
for k := w2; k >= 0; k-- {
if x <= j {
f[j][k] = max(f[j][k], f[j-x][k]+x)
}
if x <= k {
f[j][k] = max(f[j][k], f[j][k-x]+x)
}
}
}
}
return f[w1][w2]
}
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
Java
|
class Solution {
public int maxWeight(int[] weights, int w1, int w2) {
int[][] f = new int[w1 + 1][w2 + 1];
for (int x : weights) {
for (int j = w1; j >= 0; --j) {
for (int k = w2; k >= 0; --k) {
if (x <= j) {
f[j][k] = Math.max(f[j][k], f[j - x][k] + x);
}
if (x <= k) {
f[j][k] = Math.max(f[j][k], f[j][k - x] + x);
}
}
}
}
return f[w1][w2];
}
}
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
Python
|
class Solution:
def maxWeight(self, weights: List[int], w1: int, w2: int) -> int:
f = [[0] * (w2 + 1) for _ in range(w1 + 1)]
max = lambda a, b: a if a > b else b
for x in weights:
for j in range(w1, -1, -1):
for k in range(w2, -1, -1):
if x <= j:
f[j][k] = max(f[j][k], f[j - x][k] + x)
if x <= k:
f[j][k] = max(f[j][k], f[j][k - x] + x)
return f[w1][w2]
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
Rust
|
impl Solution {
pub fn max_weight(weights: Vec<i32>, w1: i32, w2: i32) -> i32 {
let w1 = w1 as usize;
let w2 = w2 as usize;
let mut f = vec![vec![0; w2 + 1]; w1 + 1];
for &x in &weights {
let x = x as usize;
for j in (0..=w1).rev() {
for k in (0..=w2).rev() {
if x <= j {
f[j][k] = f[j][k].max(f[j - x][k] + x as i32);
}
if x <= k {
f[j][k] = f[j][k].max(f[j][k - x] + x as i32);
}
}
}
}
f[w1][w2]
}
}
|
3,647
|
Maximum Weight in Two Bags
|
Medium
|
<p>You are given an integer array <code>weights</code> and two integers <code>w1</code> and <code>w2</code> representing the <strong>maximum</strong> capacities of two bags.</p>
<p>Each item may be placed in <strong>at most</strong> one bag such that:</p>
<ul>
<li>Bag 1 holds <strong>at most</strong> <code>w1</code> total weight.</li>
<li>Bag 2 holds <strong>at most</strong> <code>w2</code> total weight.</li>
</ul>
<p>Return the <strong>maximum</strong> total weight that can be packed into the two bags.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [1,4,3,2], w1 = 5, w2 = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[2] = 3</code> and <code>weights[3] = 2</code> as <code>3 + 2 = 5 <= w1</code></li>
<li>Bag 2: Place <code>weights[1] = 4</code> as <code>4 <= w2</code></li>
<li>Total weight: <code>5 + 4 = 9</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [3,6,4,8], w1 = 9, w2 = 7</span></p>
<p><strong>Output:</strong> <span class="example-io">15</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Bag 1: Place <code>weights[3] = 8</code> as <code>8 <= w1</code></li>
<li>Bag 2: Place <code>weights[0] = 3</code> and <code>weights[2] = 4</code> as <code>3 + 4 = 7 <= w2</code></li>
<li>Total weight: <code>8 + 7 = 15</code></li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">weights = [5,7], w1 = 2, w2 = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No weight fits in either bag, thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= weights.length <= 100</code></li>
<li><code>1 <= weights[i] <= 100</code></li>
<li><code>1 <= w1, w2 <= 300</code></li>
</ul>
|
Array; Dynamic Programming
|
TypeScript
|
function maxWeight(weights: number[], w1: number, w2: number): number {
const f: number[][] = Array.from({ length: w1 + 1 }, () => Array(w2 + 1).fill(0));
for (const x of weights) {
for (let j = w1; j >= 0; j--) {
for (let k = w2; k >= 0; k--) {
if (x <= j) {
f[j][k] = Math.max(f[j][k], f[j - x][k] + x);
}
if (x <= k) {
f[j][k] = Math.max(f[j][k], f[j][k - x] + x);
}
}
}
}
return f[w1][w2];
}
|
3,652
|
Best Time to Buy and Sell Stock using Strategy
|
Medium
|
<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p>
<ul>
<li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li>
<li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where:
<ul>
<li><code>-1</code> indicates buying one unit of the stock.</li>
<li><code>0</code> indicates holding the stock.</li>
<li><code>1</code> indicates selling one unit of the stock.</li>
</ul>
</li>
</ul>
<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p>
<ul>
<li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li>
<li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li>
<li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li>
</ul>
<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p>
<p>Return the <strong>maximum</strong> possible profit you can achieve.</p>
<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 1]</td>
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
<td style="border: 1px solid black;">10</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>βββββββ.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<div class="example-block">
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[1, 1, 0]</td>
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
<td style="border: 1px solid black;">9</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 0]</td>
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[1, 0, 1]</td>
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
<td style="border: 1px solid black;">8</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p>
</div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
<li><code>-1 <= strategy[i] <= 1</code></li>
<li><code>2 <= k <= prices.length</code></li>
<li><code>k</code> is even</li>
</ul>
|
Array; Prefix Sum; Sliding Window
|
C++
|
class Solution {
public:
long long maxProfit(vector<int>& prices, vector<int>& strategy, int k) {
int n = prices.size();
vector<long long> s(n + 1), t(n + 1);
for (int i = 1; i <= n; i++) {
int a = prices[i - 1];
int b = strategy[i - 1];
s[i] = s[i - 1] + a * b;
t[i] = t[i - 1] + a;
}
long long ans = s[n];
for (int i = k; i <= n; i++) {
ans = max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
}
return ans;
}
};
|
3,652
|
Best Time to Buy and Sell Stock using Strategy
|
Medium
|
<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p>
<ul>
<li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li>
<li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where:
<ul>
<li><code>-1</code> indicates buying one unit of the stock.</li>
<li><code>0</code> indicates holding the stock.</li>
<li><code>1</code> indicates selling one unit of the stock.</li>
</ul>
</li>
</ul>
<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p>
<ul>
<li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li>
<li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li>
<li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li>
</ul>
<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p>
<p>Return the <strong>maximum</strong> possible profit you can achieve.</p>
<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 1]</td>
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
<td style="border: 1px solid black;">10</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>βββββββ.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<div class="example-block">
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[1, 1, 0]</td>
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
<td style="border: 1px solid black;">9</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 0]</td>
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[1, 0, 1]</td>
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
<td style="border: 1px solid black;">8</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p>
</div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
<li><code>-1 <= strategy[i] <= 1</code></li>
<li><code>2 <= k <= prices.length</code></li>
<li><code>k</code> is even</li>
</ul>
|
Array; Prefix Sum; Sliding Window
|
Go
|
func maxProfit(prices []int, strategy []int, k int) int64 {
n := len(prices)
s := make([]int64, n+1)
t := make([]int64, n+1)
for i := 1; i <= n; i++ {
a := prices[i-1]
b := strategy[i-1]
s[i] = s[i-1] + int64(a*b)
t[i] = t[i-1] + int64(a)
}
ans := s[n]
for i := k; i <= n; i++ {
ans = max(ans, s[n]-(s[i]-s[i-k])+(t[i]-t[i-k/2]))
}
return ans
}
|
3,652
|
Best Time to Buy and Sell Stock using Strategy
|
Medium
|
<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p>
<ul>
<li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li>
<li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where:
<ul>
<li><code>-1</code> indicates buying one unit of the stock.</li>
<li><code>0</code> indicates holding the stock.</li>
<li><code>1</code> indicates selling one unit of the stock.</li>
</ul>
</li>
</ul>
<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p>
<ul>
<li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li>
<li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li>
<li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li>
</ul>
<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p>
<p>Return the <strong>maximum</strong> possible profit you can achieve.</p>
<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 1]</td>
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
<td style="border: 1px solid black;">10</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>βββββββ.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<div class="example-block">
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[1, 1, 0]</td>
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
<td style="border: 1px solid black;">9</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 0]</td>
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[1, 0, 1]</td>
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
<td style="border: 1px solid black;">8</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p>
</div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
<li><code>-1 <= strategy[i] <= 1</code></li>
<li><code>2 <= k <= prices.length</code></li>
<li><code>k</code> is even</li>
</ul>
|
Array; Prefix Sum; Sliding Window
|
Java
|
class Solution {
public long maxProfit(int[] prices, int[] strategy, int k) {
int n = prices.length;
long[] s = new long[n + 1];
long[] t = new long[n + 1];
for (int i = 1; i <= n; i++) {
int a = prices[i - 1];
int b = strategy[i - 1];
s[i] = s[i - 1] + a * b;
t[i] = t[i - 1] + a;
}
long ans = s[n];
for (int i = k; i <= n; i++) {
ans = Math.max(ans, s[n] - (s[i] - s[i - k]) + (t[i] - t[i - k / 2]));
}
return ans;
}
}
|
3,652
|
Best Time to Buy and Sell Stock using Strategy
|
Medium
|
<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p>
<ul>
<li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li>
<li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where:
<ul>
<li><code>-1</code> indicates buying one unit of the stock.</li>
<li><code>0</code> indicates holding the stock.</li>
<li><code>1</code> indicates selling one unit of the stock.</li>
</ul>
</li>
</ul>
<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p>
<ul>
<li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li>
<li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li>
<li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li>
</ul>
<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p>
<p>Return the <strong>maximum</strong> possible profit you can achieve.</p>
<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 1]</td>
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
<td style="border: 1px solid black;">10</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>βββββββ.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<div class="example-block">
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[1, 1, 0]</td>
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
<td style="border: 1px solid black;">9</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 0]</td>
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[1, 0, 1]</td>
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
<td style="border: 1px solid black;">8</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p>
</div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
<li><code>-1 <= strategy[i] <= 1</code></li>
<li><code>2 <= k <= prices.length</code></li>
<li><code>k</code> is even</li>
</ul>
|
Array; Prefix Sum; Sliding Window
|
Python
|
class Solution:
def maxProfit(self, prices: List[int], strategy: List[int], k: int) -> int:
n = len(prices)
s = [0] * (n + 1)
t = [0] * (n + 1)
for i, (a, b) in enumerate(zip(prices, strategy), 1):
s[i] = s[i - 1] + a * b
t[i] = t[i - 1] + a
ans = s[n]
for i in range(k, n + 1):
ans = max(ans, s[n] - (s[i] - s[i - k]) + t[i] - t[i - k // 2])
return ans
|
3,652
|
Best Time to Buy and Sell Stock using Strategy
|
Medium
|
<p>You are given two integer arrays <code>prices</code> and <code>strategy</code>, where:</p>
<ul>
<li><code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</li>
<li><code>strategy[i]</code> represents a trading action on the <code>i<sup>th</sup></code> day, where:
<ul>
<li><code>-1</code> indicates buying one unit of the stock.</li>
<li><code>0</code> indicates holding the stock.</li>
<li><code>1</code> indicates selling one unit of the stock.</li>
</ul>
</li>
</ul>
<p>You are also given an <strong>even</strong> integer <code>k</code>, and may perform <strong>at most one</strong> modification to <code>strategy</code>. A modification consists of:</p>
<ul>
<li>Selecting exactly <code>k</code> <strong>consecutive</strong> elements in <code>strategy</code>.</li>
<li>Set the <strong>first</strong> <code>k / 2</code> elements to <code>0</code> (hold).</li>
<li>Set the <strong>last</strong> <code>k / 2</code> elements to <code>1</code> (sell).</li>
</ul>
<p>The <strong>profit</strong> is defined as the <strong>sum</strong> of <code>strategy[i] * prices[i]</code> across all days.</p>
<p>Return the <strong>maximum</strong> possible profit you can achieve.</p>
<p><strong>Note:</strong> There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [4,2,8], strategy = [-1,0,1], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 1]</td>
<td style="border: 1px solid black;">(0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8</td>
<td style="border: 1px solid black;">10</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[-1, 0, 1]</td>
<td style="border: 1px solid black;">(-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8</td>
<td style="border: 1px solid black;">4</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 10, which is achieved by modifying the subarray <code>[0, 1]</code>βββββββ.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">prices = [5,4,3], strategy = [1,1,0], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<div class="example-block">
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Modification</th>
<th style="border: 1px solid black;">Strategy</th>
<th style="border: 1px solid black;">Profit Calculation</th>
<th style="border: 1px solid black;">Profit</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">Original</td>
<td style="border: 1px solid black;">[1, 1, 0]</td>
<td style="border: 1px solid black;">(1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0</td>
<td style="border: 1px solid black;">9</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [0, 1]</td>
<td style="border: 1px solid black;">[0, 1, 0]</td>
<td style="border: 1px solid black;">(0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0</td>
<td style="border: 1px solid black;">4</td>
</tr>
<tr>
<td style="border: 1px solid black;">Modify [1, 2]</td>
<td style="border: 1px solid black;">[1, 0, 1]</td>
<td style="border: 1px solid black;">(1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3</td>
<td style="border: 1px solid black;">8</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible profit is 9, which is achieved without any modification.</p>
</div>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= prices.length == strategy.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
<li><code>-1 <= strategy[i] <= 1</code></li>
<li><code>2 <= k <= prices.length</code></li>
<li><code>k</code> is even</li>
</ul>
|
Array; Prefix Sum; Sliding Window
|
TypeScript
|
function maxProfit(prices: number[], strategy: number[], k: number): number {
const n = prices.length;
const s: number[] = Array(n + 1).fill(0);
const t: number[] = Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
const a = prices[i - 1];
const b = strategy[i - 1];
s[i] = s[i - 1] + a * b;
t[i] = t[i - 1] + a;
}
let ans = s[n];
for (let i = k; i <= n; i++) {
const val = s[n] - (s[i] - s[i - k]) + (t[i] - t[i - Math.floor(k / 2)]);
ans = Math.max(ans, val);
}
return ans;
}
|
3,657
|
Find Loyal Customers
|
Medium
|
<p>Table: <code>customer_transactions</code></p>
<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | decimal |
| transaction_type | varchar |
+------------------+---------+
transaction_id is the unique identifier for this table.
transaction_type can be either 'purchase' or 'refund'.
</pre>
<p>Write a solution to find <strong>loyal customers</strong>. A customer is considered <strong>loyal</strong> if they meet ALL the following criteria:</p>
<ul>
<li>Made <strong>at least</strong> <code><font face="monospace">3</font></code> purchase transactions.</li>
<li>Have been active for <strong>at least</strong> <code>30</code> days.</li>
<li>Their <strong>refund rate</strong> is less than <code>20%</code> .</li>
</ul>
<p>Return <em>the result table ordered by</em> <code>customer_id</code> <em>in <strong>ascending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>customer_transactions table:</p>
<pre class="example-io">
+----------------+-------------+------------------+--------+------------------+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
+----------------+-------------+------------------+--------+------------------+
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
| 7 | 102 | 2024-01-15 | 80.00 | refund |
| 8 | 102 | 2024-01-18 | 90.00 | refund |
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
| 18 | 104 | 2024-03-15 | 100.00 | refund |
+----------------+-------------+------------------+--------+------------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------+
| customer_id |
+-------------+
| 101 |
| 104 |
+-------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Customer 101</strong>:
<ul>
<li>Purchase transactions: 4 (IDs: 1, 2, 3, 4) </li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/4 = 0% (less than 20%) </li>
<li>Active period: Jan 5 to Feb 20 = 46 days (at least 30 days) </li>
<li>Qualifies as loyal </li>
</ul>
</li>
<li><strong>Customer 102</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 5, 6, 9) </li>
<li>Refund transactions: 2 (IDs: 7, 8)</li>
<li>Refund rate: 2/5 = 40% (exceeds 20%) </li>
<li>Not loyal </li>
</ul>
</li>
<li><strong>Customer 103</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 10, 11, 12) </li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/3 = 0% (less than 20%) </li>
<li>Active period: Jan 1 to Jan 3 = 2 days (less than 30 days) </li>
<li>Not loyal </li>
</ul>
</li>
<li><strong>Customer 104</strong>:
<ul>
<li>Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17) </li>
<li>Refund transactions: 1 (ID: 18)</li>
<li>Refund rate: 1/6 = 16.67% (less than 20%) </li>
<li>Active period: Jan 1 to Mar 15 = 73 days (at least 30 days) </li>
<li>Qualifies as loyal </li>
</ul>
</li>
</ul>
<p>The result table is ordered by customer_id in ascending order.</p>
</div>
|
Database
|
Python
|
import pandas as pd
def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
customer_transactions["transaction_date"] = pd.to_datetime(
customer_transactions["transaction_date"]
)
grouped = customer_transactions.groupby("customer_id")
agg_df = grouped.agg(
total_transactions=("transaction_type", "size"),
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
min_date=("transaction_date", "min"),
max_date=("transaction_date", "max"),
).reset_index()
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
result = (
agg_df[
(agg_df["total_transactions"] >= 3)
& (agg_df["refund_ratio"] < 0.2)
& (agg_df["date_diff"] >= 30)
][["customer_id"]]
.sort_values("customer_id")
.reset_index(drop=True)
)
return result
|
3,657
|
Find Loyal Customers
|
Medium
|
<p>Table: <code>customer_transactions</code></p>
<pre>
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | decimal |
| transaction_type | varchar |
+------------------+---------+
transaction_id is the unique identifier for this table.
transaction_type can be either 'purchase' or 'refund'.
</pre>
<p>Write a solution to find <strong>loyal customers</strong>. A customer is considered <strong>loyal</strong> if they meet ALL the following criteria:</p>
<ul>
<li>Made <strong>at least</strong> <code><font face="monospace">3</font></code> purchase transactions.</li>
<li>Have been active for <strong>at least</strong> <code>30</code> days.</li>
<li>Their <strong>refund rate</strong> is less than <code>20%</code> .</li>
</ul>
<p>Return <em>the result table ordered by</em> <code>customer_id</code> <em>in <strong>ascending</strong> order</em>.</p>
<p>The result format is in the following example.</p>
<p> </p>
<p><strong class="example">Example:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p>customer_transactions table:</p>
<pre class="example-io">
+----------------+-------------+------------------+--------+------------------+
| transaction_id | customer_id | transaction_date | amount | transaction_type |
+----------------+-------------+------------------+--------+------------------+
| 1 | 101 | 2024-01-05 | 150.00 | purchase |
| 2 | 101 | 2024-01-15 | 200.00 | purchase |
| 3 | 101 | 2024-02-10 | 180.00 | purchase |
| 4 | 101 | 2024-02-20 | 250.00 | purchase |
| 5 | 102 | 2024-01-10 | 100.00 | purchase |
| 6 | 102 | 2024-01-12 | 120.00 | purchase |
| 7 | 102 | 2024-01-15 | 80.00 | refund |
| 8 | 102 | 2024-01-18 | 90.00 | refund |
| 9 | 102 | 2024-02-15 | 130.00 | purchase |
| 10 | 103 | 2024-01-01 | 500.00 | purchase |
| 11 | 103 | 2024-01-02 | 450.00 | purchase |
| 12 | 103 | 2024-01-03 | 400.00 | purchase |
| 13 | 104 | 2024-01-01 | 200.00 | purchase |
| 14 | 104 | 2024-02-01 | 250.00 | purchase |
| 15 | 104 | 2024-02-15 | 300.00 | purchase |
| 16 | 104 | 2024-03-01 | 350.00 | purchase |
| 17 | 104 | 2024-03-10 | 280.00 | purchase |
| 18 | 104 | 2024-03-15 | 100.00 | refund |
+----------------+-------------+------------------+--------+------------------+
</pre>
<p><strong>Output:</strong></p>
<pre class="example-io">
+-------------+
| customer_id |
+-------------+
| 101 |
| 104 |
+-------------+
</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><strong>Customer 101</strong>:
<ul>
<li>Purchase transactions: 4 (IDs: 1, 2, 3, 4) </li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/4 = 0% (less than 20%) </li>
<li>Active period: Jan 5 to Feb 20 = 46 days (at least 30 days) </li>
<li>Qualifies as loyal </li>
</ul>
</li>
<li><strong>Customer 102</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 5, 6, 9) </li>
<li>Refund transactions: 2 (IDs: 7, 8)</li>
<li>Refund rate: 2/5 = 40% (exceeds 20%) </li>
<li>Not loyal </li>
</ul>
</li>
<li><strong>Customer 103</strong>:
<ul>
<li>Purchase transactions: 3 (IDs: 10, 11, 12) </li>
<li>Refund transactions: 0</li>
<li>Refund rate: 0/3 = 0% (less than 20%) </li>
<li>Active period: Jan 1 to Jan 3 = 2 days (less than 30 days) </li>
<li>Not loyal </li>
</ul>
</li>
<li><strong>Customer 104</strong>:
<ul>
<li>Purchase transactions: 5 (IDs: 13, 14, 15, 16, 17) </li>
<li>Refund transactions: 1 (ID: 18)</li>
<li>Refund rate: 1/6 = 16.67% (less than 20%) </li>
<li>Active period: Jan 1 to Mar 15 = 73 days (at least 30 days) </li>
<li>Qualifies as loyal </li>
</ul>
</li>
</ul>
<p>The result table is ordered by customer_id in ascending order.</p>
</div>
|
Database
|
SQL
|
# Write your MySQL query statement below
SELECT customer_id
FROM customer_transactions
GROUP BY 1
HAVING
COUNT(1) >= 3
AND SUM(transaction_type = 'refund') / COUNT(1) < 0.2
AND DATEDIFF(MAX(transaction_date), MIN(transaction_date)) >= 30
ORDER BY 1;
|
3,658
|
GCD of Odd and Even Sums
|
Easy
|
<p>You are given an integer <code>n</code>. Your task is to compute the <strong>GCD</strong> (greatest common divisor) of two values:</p>
<ul>
<li>
<p><code>sumOdd</code>: the sum of the first <code>n</code> odd numbers.</p>
</li>
<li>
<p><code>sumEven</code>: the sum of the first <code>n</code> even numbers.</p>
</li>
</ul>
<p>Return the GCD of <code>sumOdd</code> and <code>sumEven</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 4 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 = 16</code></li>
<li>Sum of the first 4 even numbers <code>sumEven = 2 + 4 + 6 + 8 = 20</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(16, 20) = 4</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 5 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 + 9 = 25</code></li>
<li>Sum of the first 5 even numbers <code>sumEven = 2 + 4 + 6 + 8 + 10 = 30</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(25, 30) = 5</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10βββββββ00</code></li>
</ul>
|
Math; Number Theory
|
C++
|
class Solution {
public:
int gcdOfOddEvenSums(int n) {
return n;
}
};
|
3,658
|
GCD of Odd and Even Sums
|
Easy
|
<p>You are given an integer <code>n</code>. Your task is to compute the <strong>GCD</strong> (greatest common divisor) of two values:</p>
<ul>
<li>
<p><code>sumOdd</code>: the sum of the first <code>n</code> odd numbers.</p>
</li>
<li>
<p><code>sumEven</code>: the sum of the first <code>n</code> even numbers.</p>
</li>
</ul>
<p>Return the GCD of <code>sumOdd</code> and <code>sumEven</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 4 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 = 16</code></li>
<li>Sum of the first 4 even numbers <code>sumEven = 2 + 4 + 6 + 8 = 20</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(16, 20) = 4</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 5 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 + 9 = 25</code></li>
<li>Sum of the first 5 even numbers <code>sumEven = 2 + 4 + 6 + 8 + 10 = 30</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(25, 30) = 5</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10βββββββ00</code></li>
</ul>
|
Math; Number Theory
|
Go
|
func gcdOfOddEvenSums(n int) int {
return n
}
|
3,658
|
GCD of Odd and Even Sums
|
Easy
|
<p>You are given an integer <code>n</code>. Your task is to compute the <strong>GCD</strong> (greatest common divisor) of two values:</p>
<ul>
<li>
<p><code>sumOdd</code>: the sum of the first <code>n</code> odd numbers.</p>
</li>
<li>
<p><code>sumEven</code>: the sum of the first <code>n</code> even numbers.</p>
</li>
</ul>
<p>Return the GCD of <code>sumOdd</code> and <code>sumEven</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 4 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 = 16</code></li>
<li>Sum of the first 4 even numbers <code>sumEven = 2 + 4 + 6 + 8 = 20</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(16, 20) = 4</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 5 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 + 9 = 25</code></li>
<li>Sum of the first 5 even numbers <code>sumEven = 2 + 4 + 6 + 8 + 10 = 30</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(25, 30) = 5</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10βββββββ00</code></li>
</ul>
|
Math; Number Theory
|
Java
|
class Solution {
public int gcdOfOddEvenSums(int n) {
return n;
}
}
|
3,658
|
GCD of Odd and Even Sums
|
Easy
|
<p>You are given an integer <code>n</code>. Your task is to compute the <strong>GCD</strong> (greatest common divisor) of two values:</p>
<ul>
<li>
<p><code>sumOdd</code>: the sum of the first <code>n</code> odd numbers.</p>
</li>
<li>
<p><code>sumEven</code>: the sum of the first <code>n</code> even numbers.</p>
</li>
</ul>
<p>Return the GCD of <code>sumOdd</code> and <code>sumEven</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 4 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 = 16</code></li>
<li>Sum of the first 4 even numbers <code>sumEven = 2 + 4 + 6 + 8 = 20</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(16, 20) = 4</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 5 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 + 9 = 25</code></li>
<li>Sum of the first 5 even numbers <code>sumEven = 2 + 4 + 6 + 8 + 10 = 30</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(25, 30) = 5</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10βββββββ00</code></li>
</ul>
|
Math; Number Theory
|
Python
|
class Solution:
def gcdOfOddEvenSums(self, n: int) -> int:
return n
|
3,658
|
GCD of Odd and Even Sums
|
Easy
|
<p>You are given an integer <code>n</code>. Your task is to compute the <strong>GCD</strong> (greatest common divisor) of two values:</p>
<ul>
<li>
<p><code>sumOdd</code>: the sum of the first <code>n</code> odd numbers.</p>
</li>
<li>
<p><code>sumEven</code>: the sum of the first <code>n</code> even numbers.</p>
</li>
</ul>
<p>Return the GCD of <code>sumOdd</code> and <code>sumEven</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 4 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 = 16</code></li>
<li>Sum of the first 4 even numbers <code>sumEven = 2 + 4 + 6 + 8 = 20</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(16, 20) = 4</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Sum of the first 5 odd numbers <code>sumOdd = 1 + 3 + 5 + 7 + 9 = 25</code></li>
<li>Sum of the first 5 even numbers <code>sumEven = 2 + 4 + 6 + 8 + 10 = 30</code></li>
</ul>
<p>Hence, <code>GCD(sumOdd, sumEven) = GCD(25, 30) = 5</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10βββββββ00</code></li>
</ul>
|
Math; Number Theory
|
TypeScript
|
function gcdOfOddEvenSums(n: number): number {
return n;
}
|
3,659
|
Partition Array Into K-Distinct Groups
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to determine whether it is possible to partition all elements of <code>nums</code> into one or more groups such that:</p>
<ul>
<li>Each group contains <strong>exactly</strong> <code>k</code> elements.</li>
<li>All elements in each group are <strong>distinct</strong>.</li>
<li>Each element in <code>nums</code> must be assigned to <strong>exactly</strong> one group.</li>
</ul>
<p>Return <code>true</code> if such a partition is possible, otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[1, 2]</code></li>
<li>Group 2: <code>[3, 4]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,2,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[2, 3]</code></li>
<li>Group 2: <code>[2, 5]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>We cannot form groups of <code>k = 3</code> distinct elements using all values exactly once.</p>
</div>
<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>
<li><code><sup>βββββββ</sup>1 <= k <= nums.length</code></li>
</ul>
|
Array; Hash Table; Counting
|
C++
|
class Solution {
public:
bool partitionArray(vector<int>& nums, int k) {
int n = nums.size();
if (n % k) {
return false;
}
int m = n / k;
int mx = *ranges::max_element(nums);
vector<int> cnt(mx + 1);
for (int x : nums) {
if (++cnt[x] > m) {
return false;
}
}
return true;
}
};
|
3,659
|
Partition Array Into K-Distinct Groups
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to determine whether it is possible to partition all elements of <code>nums</code> into one or more groups such that:</p>
<ul>
<li>Each group contains <strong>exactly</strong> <code>k</code> elements.</li>
<li>All elements in each group are <strong>distinct</strong>.</li>
<li>Each element in <code>nums</code> must be assigned to <strong>exactly</strong> one group.</li>
</ul>
<p>Return <code>true</code> if such a partition is possible, otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[1, 2]</code></li>
<li>Group 2: <code>[3, 4]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,2,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[2, 3]</code></li>
<li>Group 2: <code>[2, 5]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>We cannot form groups of <code>k = 3</code> distinct elements using all values exactly once.</p>
</div>
<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>
<li><code><sup>βββββββ</sup>1 <= k <= nums.length</code></li>
</ul>
|
Array; Hash Table; Counting
|
Go
|
func partitionArray(nums []int, k int) bool {
n := len(nums)
if n%k != 0 {
return false
}
m := n / k
mx := slices.Max(nums)
cnt := make([]int, mx+1)
for _, x := range nums {
if cnt[x]++; cnt[x] > m {
return false
}
}
return true
}
|
3,659
|
Partition Array Into K-Distinct Groups
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to determine whether it is possible to partition all elements of <code>nums</code> into one or more groups such that:</p>
<ul>
<li>Each group contains <strong>exactly</strong> <code>k</code> elements.</li>
<li>All elements in each group are <strong>distinct</strong>.</li>
<li>Each element in <code>nums</code> must be assigned to <strong>exactly</strong> one group.</li>
</ul>
<p>Return <code>true</code> if such a partition is possible, otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[1, 2]</code></li>
<li>Group 2: <code>[3, 4]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,2,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[2, 3]</code></li>
<li>Group 2: <code>[2, 5]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>We cannot form groups of <code>k = 3</code> distinct elements using all values exactly once.</p>
</div>
<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>
<li><code><sup>βββββββ</sup>1 <= k <= nums.length</code></li>
</ul>
|
Array; Hash Table; Counting
|
Java
|
class Solution {
public boolean partitionArray(int[] nums, int k) {
int n = nums.length;
if (n % k != 0) {
return false;
}
int m = n / k;
int mx = Arrays.stream(nums).max().getAsInt();
int[] cnt = new int[mx + 1];
for (int x : nums) {
if (++cnt[x] > m) {
return false;
}
}
return true;
}
}
|
3,659
|
Partition Array Into K-Distinct Groups
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to determine whether it is possible to partition all elements of <code>nums</code> into one or more groups such that:</p>
<ul>
<li>Each group contains <strong>exactly</strong> <code>k</code> elements.</li>
<li>All elements in each group are <strong>distinct</strong>.</li>
<li>Each element in <code>nums</code> must be assigned to <strong>exactly</strong> one group.</li>
</ul>
<p>Return <code>true</code> if such a partition is possible, otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[1, 2]</code></li>
<li>Group 2: <code>[3, 4]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,2,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[2, 3]</code></li>
<li>Group 2: <code>[2, 5]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>We cannot form groups of <code>k = 3</code> distinct elements using all values exactly once.</p>
</div>
<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>
<li><code><sup>βββββββ</sup>1 <= k <= nums.length</code></li>
</ul>
|
Array; Hash Table; Counting
|
Python
|
class Solution:
def partitionArray(self, nums: List[int], k: int) -> bool:
m, mod = divmod(len(nums), k)
if mod:
return False
return max(Counter(nums).values()) <= m
|
3,659
|
Partition Array Into K-Distinct Groups
|
Medium
|
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Your task is to determine whether it is possible to partition all elements of <code>nums</code> into one or more groups such that:</p>
<ul>
<li>Each group contains <strong>exactly</strong> <code>k</code> elements.</li>
<li>All elements in each group are <strong>distinct</strong>.</li>
<li>Each element in <code>nums</code> must be assigned to <strong>exactly</strong> one group.</li>
</ul>
<p>Return <code>true</code> if such a partition is possible, otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[1, 2]</code></li>
<li>Group 2: <code>[3, 4]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,2,2], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>One possible partition is to have 2 groups:</p>
<ul>
<li>Group 1: <code>[2, 3]</code></li>
<li>Group 2: <code>[2, 5]</code></li>
</ul>
<p>Each group contains <code>k = 2</code> distinct elements, and all elements are used exactly once.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,5,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
<p><strong>Explanation:</strong></p>
<p>We cannot form groups of <code>k = 3</code> distinct elements using all values exactly once.</p>
</div>
<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>
<li><code><sup>βββββββ</sup>1 <= k <= nums.length</code></li>
</ul>
|
Array; Hash Table; Counting
|
TypeScript
|
function partitionArray(nums: number[], k: number): boolean {
const n = nums.length;
if (n % k) {
return false;
}
const m = n / k;
const mx = Math.max(...nums);
const cnt: number[] = Array(mx + 1).fill(0);
for (const x of nums) {
if (++cnt[x] > m) {
return false;
}
}
return true;
}
|
3,660
|
Jump Game IX
|
Medium
|
<p>You are given an integer array <code>nums</code>.</p>
<p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p>
<ul>
<li>Jump to index <code>j</code> where <code>j > i</code> is allowed only if <code>nums[j] < nums[i]</code>.</li>
<li>Jump to index <code>j</code> where <code>j < i</code> is allowed only if <code>nums[j] > nums[i]</code>.</li>
</ul>
<p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p>
<p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: No jump increases the value.</li>
<li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li>
<li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
</ul>
<p>Thus, <code>ans = [2, 2, 3]</code>.</p>
<ul>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li>
<li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
<li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li>
</ul>
<p>Thus, <code>ans = [3, 3, 3]</code>.</p>
</div>
<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>9</sup>βββββββ</code></li>
</ul>
|
Array; Dynamic Programming
|
C++
|
class Solution {
public:
vector<int> maxValue(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
vector<int> preMax(n, nums[0]);
for (int i = 1; i < n; ++i) {
preMax[i] = max(preMax[i - 1], nums[i]);
}
int sufMin = 1 << 30;
for (int i = n - 1; i >= 0; --i) {
ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
sufMin = min(sufMin, nums[i]);
}
return ans;
}
};
|
3,660
|
Jump Game IX
|
Medium
|
<p>You are given an integer array <code>nums</code>.</p>
<p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p>
<ul>
<li>Jump to index <code>j</code> where <code>j > i</code> is allowed only if <code>nums[j] < nums[i]</code>.</li>
<li>Jump to index <code>j</code> where <code>j < i</code> is allowed only if <code>nums[j] > nums[i]</code>.</li>
</ul>
<p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p>
<p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: No jump increases the value.</li>
<li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li>
<li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
</ul>
<p>Thus, <code>ans = [2, 2, 3]</code>.</p>
<ul>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li>
<li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
<li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li>
</ul>
<p>Thus, <code>ans = [3, 3, 3]</code>.</p>
</div>
<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>9</sup>βββββββ</code></li>
</ul>
|
Array; Dynamic Programming
|
Go
|
func maxValue(nums []int) []int {
n := len(nums)
ans := make([]int, n)
preMax := make([]int, n)
preMax[0] = nums[0]
for i := 1; i < n; i++ {
preMax[i] = max(preMax[i-1], nums[i])
}
sufMin := 1 << 30
for i := n - 1; i >= 0; i-- {
if preMax[i] > sufMin {
ans[i] = ans[i+1]
} else {
ans[i] = preMax[i]
}
sufMin = min(sufMin, nums[i])
}
return ans
}
|
3,660
|
Jump Game IX
|
Medium
|
<p>You are given an integer array <code>nums</code>.</p>
<p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p>
<ul>
<li>Jump to index <code>j</code> where <code>j > i</code> is allowed only if <code>nums[j] < nums[i]</code>.</li>
<li>Jump to index <code>j</code> where <code>j < i</code> is allowed only if <code>nums[j] > nums[i]</code>.</li>
</ul>
<p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p>
<p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: No jump increases the value.</li>
<li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li>
<li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
</ul>
<p>Thus, <code>ans = [2, 2, 3]</code>.</p>
<ul>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li>
<li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
<li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li>
</ul>
<p>Thus, <code>ans = [3, 3, 3]</code>.</p>
</div>
<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>9</sup>βββββββ</code></li>
</ul>
|
Array; Dynamic Programming
|
Java
|
class Solution {
public int[] maxValue(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
int[] preMax = new int[n];
preMax[0] = nums[0];
for (int i = 1; i < n; ++i) {
preMax[i] = Math.max(preMax[i - 1], nums[i]);
}
int sufMin = 1 << 30;
for (int i = n - 1; i >= 0; --i) {
ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
sufMin = Math.min(sufMin, nums[i]);
}
return ans;
}
}
|
3,660
|
Jump Game IX
|
Medium
|
<p>You are given an integer array <code>nums</code>.</p>
<p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p>
<ul>
<li>Jump to index <code>j</code> where <code>j > i</code> is allowed only if <code>nums[j] < nums[i]</code>.</li>
<li>Jump to index <code>j</code> where <code>j < i</code> is allowed only if <code>nums[j] > nums[i]</code>.</li>
</ul>
<p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p>
<p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: No jump increases the value.</li>
<li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li>
<li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
</ul>
<p>Thus, <code>ans = [2, 2, 3]</code>.</p>
<ul>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li>
<li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
<li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li>
</ul>
<p>Thus, <code>ans = [3, 3, 3]</code>.</p>
</div>
<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>9</sup>βββββββ</code></li>
</ul>
|
Array; Dynamic Programming
|
Python
|
class Solution:
def maxValue(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [0] * n
pre_max = [nums[0]] * n
for i in range(1, n):
pre_max[i] = max(pre_max[i - 1], nums[i])
suf_min = inf
for i in range(n - 1, -1, -1):
ans[i] = ans[i + 1] if pre_max[i] > suf_min else pre_max[i]
suf_min = min(suf_min, nums[i])
return ans
|
3,660
|
Jump Game IX
|
Medium
|
<p>You are given an integer array <code>nums</code>.</p>
<p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p>
<ul>
<li>Jump to index <code>j</code> where <code>j > i</code> is allowed only if <code>nums[j] < nums[i]</code>.</li>
<li>Jump to index <code>j</code> where <code>j < i</code> is allowed only if <code>nums[j] > nums[i]</code>.</li>
</ul>
<p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p>
<p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: No jump increases the value.</li>
<li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li>
<li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
</ul>
<p>Thus, <code>ans = [2, 2, 3]</code>.</p>
<ul>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li>
<li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li>
<li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li>
</ul>
<p>Thus, <code>ans = [3, 3, 3]</code>.</p>
</div>
<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>9</sup>βββββββ</code></li>
</ul>
|
Array; Dynamic Programming
|
TypeScript
|
function maxValue(nums: number[]): number[] {
const n = nums.length;
const ans = Array(n).fill(0);
const preMax = Array(n).fill(nums[0]);
for (let i = 1; i < n; i++) {
preMax[i] = Math.max(preMax[i - 1], nums[i]);
}
let sufMin = 1 << 30;
for (let i = n - 1; i >= 0; i--) {
ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i];
sufMin = Math.min(sufMin, nums[i]);
}
return ans;
}
|
3,661
|
Maximum Walls Destroyed by Robots
|
Hard
|
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf">
<div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div>
</div>
<ul>
<li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li>
<li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot's bullet can travel.</li>
<li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li>
</ul>
<p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p>
<p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p>
<p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p>
<p>Notes:</p>
<ul>
<li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li>
<li>Robots are not destroyed by bullets.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [4], distance = [3], walls = [1,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li>
<li>Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li>
<li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li>
<li>Thus, the answer is 3.</li>
</ul>
</div>
<strong class="example">Example 3:</strong>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= robots.length == distance.length <= 10<sup>5</sup></code></li>
<li><code>1 <= walls.length <= 10<sup>5</sup></code></li>
<li><code>1 <= robots[i], walls[j] <= 10<sup>9</sup></code></li>
<li><code>1 <= distance[i] <= 10<sup>5</sup></code></li>
<li>All values in <code>robots</code> are <strong>unique</strong></li>
<li>All values in <code>walls</code> are <strong>unique</strong></li>
</ul>
|
Array; Binary Search; Dynamic Programming; Sorting
|
C++
|
class Solution {
public:
int maxWalls(vector<int>& robots, vector<int>& distance, vector<int>& walls) {
int n = robots.size();
vector<pair<int, int>> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = {robots[i], distance[i]};
}
ranges::sort(arr, {}, &pair<int, int>::first);
ranges::sort(walls);
vector f(n, vector<int>(2, -1));
auto dfs = [&](this auto&& dfs, int i, int j) -> int {
if (i < 0) {
return 0;
}
if (f[i][j] != -1) {
return f[i][j];
}
int left = arr[i].first - arr[i].second;
if (i > 0) {
left = max(left, arr[i - 1].first + 1);
}
int l = ranges::lower_bound(walls, left) - walls.begin();
int r = ranges::lower_bound(walls, arr[i].first + 1) - walls.begin();
int ans = dfs(i - 1, 0) + (r - l);
int right = arr[i].first + arr[i].second;
if (i + 1 < n) {
if (j == 0) {
right = min(right, arr[i + 1].first - arr[i + 1].second - 1);
} else {
right = min(right, arr[i + 1].first - 1);
}
}
l = ranges::lower_bound(walls, arr[i].first) - walls.begin();
r = ranges::lower_bound(walls, right + 1) - walls.begin();
ans = max(ans, dfs(i - 1, 1) + (r - l));
return f[i][j] = ans;
};
return dfs(n - 1, 1);
}
};
|
3,661
|
Maximum Walls Destroyed by Robots
|
Hard
|
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf">
<div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div>
</div>
<ul>
<li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li>
<li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot's bullet can travel.</li>
<li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li>
</ul>
<p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p>
<p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p>
<p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p>
<p>Notes:</p>
<ul>
<li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li>
<li>Robots are not destroyed by bullets.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [4], distance = [3], walls = [1,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li>
<li>Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li>
<li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li>
<li>Thus, the answer is 3.</li>
</ul>
</div>
<strong class="example">Example 3:</strong>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= robots.length == distance.length <= 10<sup>5</sup></code></li>
<li><code>1 <= walls.length <= 10<sup>5</sup></code></li>
<li><code>1 <= robots[i], walls[j] <= 10<sup>9</sup></code></li>
<li><code>1 <= distance[i] <= 10<sup>5</sup></code></li>
<li>All values in <code>robots</code> are <strong>unique</strong></li>
<li>All values in <code>walls</code> are <strong>unique</strong></li>
</ul>
|
Array; Binary Search; Dynamic Programming; Sorting
|
Go
|
func maxWalls(robots []int, distance []int, walls []int) int {
type pair struct {
x, d int
}
n := len(robots)
arr := make([]pair, n)
for i := 0; i < n; i++ {
arr[i] = pair{robots[i], distance[i]}
}
sort.Slice(arr, func(i, j int) bool {
return arr[i].x < arr[j].x
})
sort.Ints(walls)
f := make(map[[2]int]int)
var dfs func(int, int) int
dfs = func(i, j int) int {
if i < 0 {
return 0
}
key := [2]int{i, j}
if v, ok := f[key]; ok {
return v
}
left := arr[i].x - arr[i].d
if i > 0 {
left = max(left, arr[i-1].x+1)
}
l := sort.SearchInts(walls, left)
r := sort.SearchInts(walls, arr[i].x+1)
ans := dfs(i-1, 0) + (r - l)
right := arr[i].x + arr[i].d
if i+1 < n {
if j == 0 {
right = min(right, arr[i+1].x-arr[i+1].d-1)
} else {
right = min(right, arr[i+1].x-1)
}
}
l = sort.SearchInts(walls, arr[i].x)
r = sort.SearchInts(walls, right+1)
ans = max(ans, dfs(i-1, 1)+(r-l))
f[key] = ans
return ans
}
return dfs(n-1, 1)
}
|
3,661
|
Maximum Walls Destroyed by Robots
|
Hard
|
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf">
<div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div>
</div>
<ul>
<li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li>
<li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot's bullet can travel.</li>
<li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li>
</ul>
<p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p>
<p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p>
<p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p>
<p>Notes:</p>
<ul>
<li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li>
<li>Robots are not destroyed by bullets.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [4], distance = [3], walls = [1,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li>
<li>Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li>
<li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li>
<li>Thus, the answer is 3.</li>
</ul>
</div>
<strong class="example">Example 3:</strong>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= robots.length == distance.length <= 10<sup>5</sup></code></li>
<li><code>1 <= walls.length <= 10<sup>5</sup></code></li>
<li><code>1 <= robots[i], walls[j] <= 10<sup>9</sup></code></li>
<li><code>1 <= distance[i] <= 10<sup>5</sup></code></li>
<li>All values in <code>robots</code> are <strong>unique</strong></li>
<li>All values in <code>walls</code> are <strong>unique</strong></li>
</ul>
|
Array; Binary Search; Dynamic Programming; Sorting
|
Java
|
class Solution {
private Integer[][] f;
private int[][] arr;
private int[] walls;
private int n;
public int maxWalls(int[] robots, int[] distance, int[] walls) {
n = robots.length;
arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = robots[i];
arr[i][1] = distance[i];
}
Arrays.sort(arr, Comparator.comparingInt(a -> a[0]));
Arrays.sort(walls);
this.walls = walls;
f = new Integer[n][2];
return dfs(n - 1, 1);
}
private int dfs(int i, int j) {
if (i < 0) {
return 0;
}
if (f[i][j] != null) {
return f[i][j];
}
int left = arr[i][0] - arr[i][1];
if (i > 0) {
left = Math.max(left, arr[i - 1][0] + 1);
}
int l = lowerBound(walls, left);
int r = lowerBound(walls, arr[i][0] + 1);
int ans = dfs(i - 1, 0) + (r - l);
int right = arr[i][0] + arr[i][1];
if (i + 1 < n) {
if (j == 0) {
right = Math.min(right, arr[i + 1][0] - arr[i + 1][1] - 1);
} else {
right = Math.min(right, arr[i + 1][0] - 1);
}
}
l = lowerBound(walls, arr[i][0]);
r = lowerBound(walls, right + 1);
ans = Math.max(ans, dfs(i - 1, 1) + (r - l));
return f[i][j] = ans;
}
private int lowerBound(int[] arr, int target) {
int idx = Arrays.binarySearch(arr, target);
if (idx < 0) {
return -idx - 1;
}
return idx;
}
}
|
3,661
|
Maximum Walls Destroyed by Robots
|
Hard
|
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf">
<div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div>
</div>
<ul>
<li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li>
<li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot's bullet can travel.</li>
<li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li>
</ul>
<p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p>
<p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p>
<p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p>
<p>Notes:</p>
<ul>
<li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li>
<li>Robots are not destroyed by bullets.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [4], distance = [3], walls = [1,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li>
<li>Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li>
<li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li>
<li>Thus, the answer is 3.</li>
</ul>
</div>
<strong class="example">Example 3:</strong>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= robots.length == distance.length <= 10<sup>5</sup></code></li>
<li><code>1 <= walls.length <= 10<sup>5</sup></code></li>
<li><code>1 <= robots[i], walls[j] <= 10<sup>9</sup></code></li>
<li><code>1 <= distance[i] <= 10<sup>5</sup></code></li>
<li>All values in <code>robots</code> are <strong>unique</strong></li>
<li>All values in <code>walls</code> are <strong>unique</strong></li>
</ul>
|
Array; Binary Search; Dynamic Programming; Sorting
|
Python
|
class Solution:
def maxWalls(self, robots: List[int], distance: List[int], walls: List[int]) -> int:
n = len(robots)
arr = sorted(zip(robots, distance), key=lambda x: x[0])
walls.sort()
@cache
def dfs(i: int, j: int) -> int:
if i < 0:
return 0
left = arr[i][0] - arr[i][1]
if i > 0:
left = max(left, arr[i - 1][0] + 1)
l = bisect_left(walls, left)
r = bisect_left(walls, arr[i][0] + 1)
ans = dfs(i - 1, 0) + r - l
right = arr[i][0] + arr[i][1]
if i + 1 < n:
if j == 0:
right = min(right, arr[i + 1][0] - arr[i + 1][1] - 1)
else:
right = min(right, arr[i + 1][0] - 1)
l = bisect_left(walls, arr[i][0])
r = bisect_left(walls, right + 1)
ans = max(ans, dfs(i - 1, 1) + r - l)
return ans
return dfs(n - 1, 1)
|
3,661
|
Maximum Walls Destroyed by Robots
|
Hard
|
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf">
<div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div>
</div>
<ul>
<li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li>
<li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot's bullet can travel.</li>
<li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li>
</ul>
<p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p>
<p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p>
<p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p>
<p>Notes:</p>
<ul>
<li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li>
<li>Robots are not destroyed by bullets.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [4], distance = [3], walls = [1,10]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li>
<li>Thus, the answer is 1.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li>
<li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li>
<li>Thus, the answer is 3.</li>
</ul>
</div>
<strong class="example">Example 3:</strong>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= robots.length == distance.length <= 10<sup>5</sup></code></li>
<li><code>1 <= walls.length <= 10<sup>5</sup></code></li>
<li><code>1 <= robots[i], walls[j] <= 10<sup>9</sup></code></li>
<li><code>1 <= distance[i] <= 10<sup>5</sup></code></li>
<li>All values in <code>robots</code> are <strong>unique</strong></li>
<li>All values in <code>walls</code> are <strong>unique</strong></li>
</ul>
|
Array; Binary Search; Dynamic Programming; Sorting
|
TypeScript
|
function maxWalls(robots: number[], distance: number[], walls: number[]): number {
type Pair = [number, number];
const n = robots.length;
const arr: Pair[] = robots.map((r, i) => [r, distance[i]]);
_.sortBy(arr, p => p[0]).forEach((p, i) => (arr[i] = p));
walls.sort((a, b) => a - b);
const f: number[][] = Array.from({ length: n }, () => Array(2).fill(-1));
function dfs(i: number, j: number): number {
if (i < 0) {
return 0;
}
if (f[i][j] !== -1) {
return f[i][j];
}
let left = arr[i][0] - arr[i][1];
if (i > 0) left = Math.max(left, arr[i - 1][0] + 1);
let l = _.sortedIndex(walls, left);
let r = _.sortedIndex(walls, arr[i][0] + 1);
let ans = dfs(i - 1, 0) + (r - l);
let right = arr[i][0] + arr[i][1];
if (i + 1 < n) {
if (j === 0) {
right = Math.min(right, arr[i + 1][0] - arr[i + 1][1] - 1);
} else {
right = Math.min(right, arr[i + 1][0] - 1);
}
}
l = _.sortedIndex(walls, arr[i][0]);
r = _.sortedIndex(walls, right + 1);
ans = Math.max(ans, dfs(i - 1, 1) + (r - l));
f[i][j] = ans;
return ans;
}
return dfs(n - 1, 1);
}
|
3,662
|
Filter Characters by Frequency
|
Easy
|
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "aadbbcccca", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"dbb"</span></p>
<p><strong>Explanation:</strong></p>
<p>Character frequencies in <code>s</code>:</p>
<ul>
<li><code>'a'</code> appears 3 times</li>
<li><code>'d'</code> appears 1 time</li>
<li><code>'b'</code> appears 2 times</li>
<li><code>'c'</code> appears 4 times</li>
</ul>
<p>Only <code>'d'</code> and <code>'b'</code> appear fewer than 3 times. Preserving their order, the result is <code>"dbb"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "xyz", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"xyz"</span></p>
<p><strong>Explanation:</strong></p>
<p>All characters (<code>'x'</code>, <code>'y'</code>, <code>'z'</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 <= k <= s.length</code></li>
</ul>
|
Hash Table; String; Counting
|
C++
|
class Solution {
public:
string filterCharacters(string s, int k) {
int cnt[26]{};
for (char c : s) {
++cnt[c - 'a'];
}
string ans;
for (char c : s) {
if (cnt[c - 'a'] < k) {
ans.push_back(c);
}
}
return ans;
}
};
|
3,662
|
Filter Characters by Frequency
|
Easy
|
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "aadbbcccca", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"dbb"</span></p>
<p><strong>Explanation:</strong></p>
<p>Character frequencies in <code>s</code>:</p>
<ul>
<li><code>'a'</code> appears 3 times</li>
<li><code>'d'</code> appears 1 time</li>
<li><code>'b'</code> appears 2 times</li>
<li><code>'c'</code> appears 4 times</li>
</ul>
<p>Only <code>'d'</code> and <code>'b'</code> appear fewer than 3 times. Preserving their order, the result is <code>"dbb"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "xyz", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"xyz"</span></p>
<p><strong>Explanation:</strong></p>
<p>All characters (<code>'x'</code>, <code>'y'</code>, <code>'z'</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 <= k <= s.length</code></li>
</ul>
|
Hash Table; String; Counting
|
Go
|
func filterCharacters(s string, k int) string {
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
ans := []rune{}
for _, c := range s {
if cnt[c-'a'] < k {
ans = append(ans, c)
}
}
return string(ans)
}
|
3,662
|
Filter Characters by Frequency
|
Easy
|
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "aadbbcccca", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"dbb"</span></p>
<p><strong>Explanation:</strong></p>
<p>Character frequencies in <code>s</code>:</p>
<ul>
<li><code>'a'</code> appears 3 times</li>
<li><code>'d'</code> appears 1 time</li>
<li><code>'b'</code> appears 2 times</li>
<li><code>'c'</code> appears 4 times</li>
</ul>
<p>Only <code>'d'</code> and <code>'b'</code> appear fewer than 3 times. Preserving their order, the result is <code>"dbb"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "xyz", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"xyz"</span></p>
<p><strong>Explanation:</strong></p>
<p>All characters (<code>'x'</code>, <code>'y'</code>, <code>'z'</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 <= k <= s.length</code></li>
</ul>
|
Hash Table; String; Counting
|
Java
|
class Solution {
public String filterCharacters(String s, int k) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
StringBuilder ans = new StringBuilder();
for (char c : s.toCharArray()) {
if (cnt[c - 'a'] < k) {
ans.append(c);
}
}
return ans.toString();
}
}
|
3,662
|
Filter Characters by Frequency
|
Easy
|
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "aadbbcccca", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"dbb"</span></p>
<p><strong>Explanation:</strong></p>
<p>Character frequencies in <code>s</code>:</p>
<ul>
<li><code>'a'</code> appears 3 times</li>
<li><code>'d'</code> appears 1 time</li>
<li><code>'b'</code> appears 2 times</li>
<li><code>'c'</code> appears 4 times</li>
</ul>
<p>Only <code>'d'</code> and <code>'b'</code> appear fewer than 3 times. Preserving their order, the result is <code>"dbb"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "xyz", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"xyz"</span></p>
<p><strong>Explanation:</strong></p>
<p>All characters (<code>'x'</code>, <code>'y'</code>, <code>'z'</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 <= k <= s.length</code></li>
</ul>
|
Hash Table; String; Counting
|
Python
|
class Solution:
def filterCharacters(self, s: str, k: int) -> str:
cnt = Counter(s)
ans = []
for c in s:
if cnt[c] < k:
ans.append(c)
return "".join(ans)
|
3,662
|
Filter Characters by Frequency
|
Easy
|
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p>
<p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p>
<p>Return the resulting string. If no characters qualify, return an empty string.</p>
<p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "aadbbcccca", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"dbb"</span></p>
<p><strong>Explanation:</strong></p>
<p>Character frequencies in <code>s</code>:</p>
<ul>
<li><code>'a'</code> appears 3 times</li>
<li><code>'d'</code> appears 1 time</li>
<li><code>'b'</code> appears 2 times</li>
<li><code>'c'</code> appears 4 times</li>
</ul>
<p>Only <code>'d'</code> and <code>'b'</code> appear fewer than 3 times. Preserving their order, the result is <code>"dbb"</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "xyz", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"xyz"</span></p>
<p><strong>Explanation:</strong></p>
<p>All characters (<code>'x'</code>, <code>'y'</code>, <code>'z'</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 <= k <= s.length</code></li>
</ul>
|
Hash Table; String; Counting
|
TypeScript
|
function filterCharacters(s: string, k: number): string {
const cnt: Record<string, number> = {};
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
}
const ans: string[] = [];
for (const c of s) {
if (cnt[c] < k) {
ans.push(c);
}
}
return ans.join('');
}
|
3,663
|
Find The Least Frequent Digit
|
Easy
|
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
<p>Return the chosen digit as an integer.</p>
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup>βββββββ - 1</code></li>
</ul>
|
Array; Hash Table; Math; Counting
|
C++
|
class Solution {
public:
int getLeastFrequentDigit(int n) {
int cnt[10]{};
for (; n > 0; n /= 10) {
++cnt[n % 10];
}
int ans = 0, f = 1 << 30;
for (int x = 0; x < 10; ++x) {
if (cnt[x] > 0 && cnt[x] < f) {
f = cnt[x];
ans = x;
}
}
return ans;
}
};
|
3,663
|
Find The Least Frequent Digit
|
Easy
|
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
<p>Return the chosen digit as an integer.</p>
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup>βββββββ - 1</code></li>
</ul>
|
Array; Hash Table; Math; Counting
|
Go
|
func getLeastFrequentDigit(n int) (ans int) {
cnt := [10]int{}
for ; n > 0; n /= 10 {
cnt[n%10]++
}
f := 1 << 30
for x, v := range cnt {
if v > 0 && v < f {
f = v
ans = x
}
}
return
}
|
3,663
|
Find The Least Frequent Digit
|
Easy
|
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
<p>Return the chosen digit as an integer.</p>
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup>βββββββ - 1</code></li>
</ul>
|
Array; Hash Table; Math; Counting
|
Java
|
class Solution {
public int getLeastFrequentDigit(int n) {
int[] cnt = new int[10];
for (; n > 0; n /= 10) {
++cnt[n % 10];
}
int ans = 0, f = 1 << 30;
for (int x = 0; x < 10; ++x) {
if (cnt[x] > 0 && cnt[x] < f) {
f = cnt[x];
ans = x;
}
}
return ans;
}
}
|
3,663
|
Find The Least Frequent Digit
|
Easy
|
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
<p>Return the chosen digit as an integer.</p>
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup>βββββββ - 1</code></li>
</ul>
|
Array; Hash Table; Math; Counting
|
Python
|
class Solution:
def getLeastFrequentDigit(self, n: int) -> int:
cnt = [0] * 10
while n:
n, x = divmod(n, 10)
cnt[x] += 1
ans, f = 0, inf
for x, v in enumerate(cnt):
if 0 < v < f:
f = v
ans = x
return ans
|
3,663
|
Find The Least Frequent Digit
|
Easy
|
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
<p>Return the chosen digit as an integer.</p>
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 2<sup>31</sup>βββββββ - 1</code></li>
</ul>
|
Array; Hash Table; Math; Counting
|
TypeScript
|
function getLeastFrequentDigit(n: number): number {
const cnt: number[] = Array(10).fill(0);
for (; n; n = (n / 10) | 0) {
cnt[n % 10]++;
}
let [ans, f] = [0, Number.MAX_SAFE_INTEGER];
for (let x = 0; x < 10; ++x) {
if (cnt[x] > 0 && cnt[x] < f) {
f = cnt[x];
ans = x;
}
}
return ans;
}
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
C++
|
class Solution {
public:
vector<int> sortByAbsoluteValue(vector<int>& nums) {
sort(nums.begin(), nums.end(), [](int a, int b) {
return abs(a) < abs(b);
});
return nums;
}
};
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
Go
|
func sortByAbsoluteValue(nums []int) []int {
slices.SortFunc(nums, func(a, b int) int {
return abs(a) - abs(b)
})
return nums
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
Java
|
class Solution {
public int[] sortByAbsoluteValue(int[] nums) {
return Arrays.stream(nums)
.boxed()
.sorted(Comparator.comparingInt(Math::abs))
.mapToInt(Integer::intValue)
.toArray();
}
}
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
Python
|
class Solution:
def sortByAbsoluteValue(self, nums: List[int]) -> List[int]:
return sorted(nums, key=lambda x: abs(x))
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
Rust
|
impl Solution {
pub fn sort_by_absolute_value(mut nums: Vec<i32>) -> Vec<i32> {
nums.sort_by_key(|&x| x.abs());
nums
}
}
|
3,667
|
Sort Array By Absolute Value
|
Easy
|
<p>You are given an integer array <code>nums</code>.</p>
<p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p>
<p>Return <strong>any</strong> rearranged array that satisfies this condition.</p>
<p><strong>Note</strong>: The absolute value of an integer x is defined as:</p>
<ul>
<li><code>x</code> if <code>x >= 0</code></li>
<li><code>-x</code> if <code>x < 0</code></li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,-1,-4,1,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li>
<li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li>
<li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p>
<p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li>
<li>Rearranging them in increasing order, we get 100, 100.</li>
<li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li>
</ul>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-100 <= nums[i] <= 100</code></li>
</ul>
|
Array; Math; Two Pointers; Sorting
|
TypeScript
|
function sortByAbsoluteValue(nums: number[]): number[] {
return nums.sort((a, b) => Math.abs(a) - Math.abs(b));
}
|
3,668
|
Restore Finishing Order
|
Easy
|
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p>
<ul>
<li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li>
<li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li>
</ul>
<p>Return an array containing your friends' IDs in their <strong>finishing</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == order.length <= 100</code></li>
<li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li>
<li><code>1 <= friends.length <= min(8, n)</code></li>
<li><code>1 <= friends[i] <= n</code></li>
<li><code>friends</code> is strictly increasing</li>
</ul>
|
Array; Hash Table
|
C++
|
class Solution {
public:
vector<int> recoverOrder(vector<int>& order, vector<int>& friends) {
int n = order.size();
vector<int> d(n + 1);
for (int i = 0; i < n; ++i) {
d[order[i]] = i;
}
sort(friends.begin(), friends.end(), [&](int a, int b) {
return d[a] < d[b];
});
return friends;
}
};
|
3,668
|
Restore Finishing Order
|
Easy
|
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p>
<ul>
<li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li>
<li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li>
</ul>
<p>Return an array containing your friends' IDs in their <strong>finishing</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == order.length <= 100</code></li>
<li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li>
<li><code>1 <= friends.length <= min(8, n)</code></li>
<li><code>1 <= friends[i] <= n</code></li>
<li><code>friends</code> is strictly increasing</li>
</ul>
|
Array; Hash Table
|
Go
|
func recoverOrder(order []int, friends []int) []int {
n := len(order)
d := make([]int, n+1)
for i, x := range order {
d[x] = i
}
sort.Slice(friends, func(i, j int) bool {
return d[friends[i]] < d[friends[j]]
})
return friends
}
|
3,668
|
Restore Finishing Order
|
Easy
|
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p>
<ul>
<li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li>
<li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li>
</ul>
<p>Return an array containing your friends' IDs in their <strong>finishing</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == order.length <= 100</code></li>
<li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li>
<li><code>1 <= friends.length <= min(8, n)</code></li>
<li><code>1 <= friends[i] <= n</code></li>
<li><code>friends</code> is strictly increasing</li>
</ul>
|
Array; Hash Table
|
Java
|
class Solution {
public int[] recoverOrder(int[] order, int[] friends) {
int n = order.length;
int[] d = new int[n + 1];
for (int i = 0; i < n; ++i) {
d[order[i]] = i;
}
return Arrays.stream(friends)
.boxed()
.sorted((a, b) -> d[a] - d[b])
.mapToInt(Integer::intValue)
.toArray();
}
}
|
3,668
|
Restore Finishing Order
|
Easy
|
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p>
<ul>
<li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li>
<li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li>
</ul>
<p>Return an array containing your friends' IDs in their <strong>finishing</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == order.length <= 100</code></li>
<li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li>
<li><code>1 <= friends.length <= min(8, n)</code></li>
<li><code>1 <= friends[i] <= n</code></li>
<li><code>friends</code> is strictly increasing</li>
</ul>
|
Array; Hash Table
|
Python
|
class Solution:
def recoverOrder(self, order: List[int], friends: List[int]) -> List[int]:
d = {x: i for i, x in enumerate(order)}
return sorted(friends, key=lambda x: d[x])
|
3,668
|
Restore Finishing Order
|
Easy
|
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p>
<ul>
<li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li>
<li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li>
</ul>
<p>Return an array containing your friends' IDs in their <strong>finishing</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[5,2]</span></p>
<p><strong>Explanation:</strong></p>
<p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == order.length <= 100</code></li>
<li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li>
<li><code>1 <= friends.length <= min(8, n)</code></li>
<li><code>1 <= friends[i] <= n</code></li>
<li><code>friends</code> is strictly increasing</li>
</ul>
|
Array; Hash Table
|
TypeScript
|
function recoverOrder(order: number[], friends: number[]): number[] {
const n = order.length;
const d: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
d[order[i]] = i;
}
return friends.sort((a, b) => d[a] - d[b]);
}
|
3,669
|
Balanced K-Factor Decomposition
|
Medium
|
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p>
<p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 100, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,10]</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li>
<li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li>
<li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li>
<li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li>
</ul>
<p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="54" data-start="37"><code data-end="52" data-start="37">4 <= n <= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li>
<li data-end="71" data-start="57"><code data-end="69" data-start="57">2 <= k <= 5</code></li>
<li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li>
</ul>
|
Math; Backtracking; Number Theory
|
C++
|
class Solution {
public:
static const int MX = 100001;
static vector<vector<int>> g;
vector<int> ans;
vector<int> path;
int cur;
vector<int> minDifference(int n, int k) {
if (g.empty()) {
g.resize(MX);
for (int i = 1; i < MX; i++) {
for (int j = i; j < MX; j += i) {
g[j].push_back(i);
}
}
}
cur = INT_MAX;
ans.clear();
path.assign(k, 0);
dfs(k - 1, n, INT_MAX, 0);
return ans;
}
private:
void dfs(int i, int x, int mi, int mx) {
if (i == 0) {
int d = max(mx, x) - min(mi, x);
if (d < cur) {
cur = d;
path[i] = x;
ans = path;
}
return;
}
for (int y : g[x]) {
path[i] = y;
dfs(i - 1, x / y, min(mi, y), max(mx, y));
}
}
};
vector<vector<int>> Solution::g;
|
3,669
|
Balanced K-Factor Decomposition
|
Medium
|
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p>
<p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 100, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,10]</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li>
<li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li>
<li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li>
<li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li>
</ul>
<p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="54" data-start="37"><code data-end="52" data-start="37">4 <= n <= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li>
<li data-end="71" data-start="57"><code data-end="69" data-start="57">2 <= k <= 5</code></li>
<li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li>
</ul>
|
Math; Backtracking; Number Theory
|
Go
|
const MX = 100001
var g [][]int
func init() {
g = make([][]int, MX)
for i := 1; i < MX; i++ {
for j := i; j < MX; j += i {
g[j] = append(g[j], i)
}
}
}
var (
cur int
ans []int
path []int
)
func minDifference(n int, k int) []int {
cur = math.MaxInt32
ans = nil
path = make([]int, k)
dfs(k-1, n, math.MaxInt32, 0)
return ans
}
func dfs(i, x, mi, mx int) {
if i == 0 {
d := max(mx, x) - min(mi, x)
if d < cur {
cur = d
path[i] = x
ans = slices.Clone(path)
}
return
}
for _, y := range g[x] {
path[i] = y
dfs(i-1, x/y, min(mi, y), max(mx, y))
}
}
|
3,669
|
Balanced K-Factor Decomposition
|
Medium
|
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p>
<p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 100, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,10]</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li>
<li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li>
<li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li>
<li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li>
</ul>
<p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="54" data-start="37"><code data-end="52" data-start="37">4 <= n <= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li>
<li data-end="71" data-start="57"><code data-end="69" data-start="57">2 <= k <= 5</code></li>
<li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li>
</ul>
|
Math; Backtracking; Number Theory
|
Java
|
class Solution {
static final int MX = 100_001;
static List<Integer>[] g = new ArrayList[MX];
static {
for (int i = 0; i < MX; i++) {
g[i] = new ArrayList<>();
}
for (int i = 1; i < MX; i++) {
for (int j = i; j < MX; j += i) {
g[j].add(i);
}
}
}
private int cur;
private int[] ans;
private int[] path;
public int[] minDifference(int n, int k) {
cur = Integer.MAX_VALUE;
ans = null;
path = new int[k];
dfs(k - 1, n, Integer.MAX_VALUE, 0);
return ans;
}
private void dfs(int i, int x, int mi, int mx) {
if (i == 0) {
int d = Math.max(mx, x) - Math.min(mi, x);
if (d < cur) {
cur = d;
path[i] = x;
ans = path.clone();
}
return;
}
for (int y : g[x]) {
path[i] = y;
dfs(i - 1, x / y, Math.min(mi, y), Math.max(mx, y));
}
}
}
|
3,669
|
Balanced K-Factor Decomposition
|
Medium
|
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p>
<p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 100, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">[10,10]</span></p>
<p><strong>Explanation:</strong></p>
<p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li>
<li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li>
<li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li>
<li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li>
</ul>
<p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li data-end="54" data-start="37"><code data-end="52" data-start="37">4 <= n <= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li>
<li data-end="71" data-start="57"><code data-end="69" data-start="57">2 <= k <= 5</code></li>
<li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li>
</ul>
|
Math; Backtracking; Number Theory
|
Python
|
mx = 10**5 + 1
g = [[] for _ in range(mx)]
for i in range(1, mx):
for j in range(i, mx, i):
g[j].append(i)
class Solution:
def minDifference(self, n: int, k: int) -> List[int]:
def dfs(i: int, x: int, mi: int, mx: int):
if i == 0:
nonlocal cur, ans
d = max(mx, x) - min(mi, x)
if d < cur:
cur = d
path[i] = x
ans = path[:]
return
for y in g[x]:
path[i] = y
dfs(i - 1, x // y, min(mi, y), max(mx, y))
ans = None
path = [0] * k
cur = inf
dfs(k - 1, n, inf, 0)
return ans
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.