Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d52468

Browse files
committed
Add solution #2740
1 parentf289eb4 commit5d52468

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,7 @@
20172017
2732|[Find a Good Subset of the Matrix](./solutions/2732-find-a-good-subset-of-the-matrix.js)|Hard|
20182018
2733|[Neither Minimum nor Maximum](./solutions/2733-neither-minimum-nor-maximum.js)|Easy|
20192019
2739|[Total Distance Traveled](./solutions/2739-total-distance-traveled.js)|Easy|
2020+
2740|[Find the Value of the Partition](./solutions/2740-find-the-value-of-the-partition.js)|Medium|
20202021
2780|[Minimum Index of a Valid Split](./solutions/2780-minimum-index-of-a-valid-split.js)|Medium|
20212022
2799|[Count Complete Subarrays in an Array](./solutions/2799-count-complete-subarrays-in-an-array.js)|Medium|
20222023
2818|[Apply Operations to Maximize Score](./solutions/2818-apply-operations-to-maximize-score.js)|Hard|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2740. Find the Value of the Partition
3+
* https://leetcode.com/problems/find-the-value-of-the-partition/
4+
* Difficulty: Medium
5+
*
6+
* You are given a positive integer array nums.
7+
*
8+
* Partition nums into two arrays, nums1 and nums2, such that:
9+
* - Each element of the array nums belongs to either the array nums1 or the array nums2.
10+
* - Both arrays are non-empty.
11+
* - The value of the partition is minimized.
12+
*
13+
* The value of the partition is |max(nums1) - min(nums2)|.
14+
*
15+
* Here, max(nums1) denotes the maximum element of the array nums1, and min(nums2) denotes the
16+
* minimum element of the array nums2.
17+
*
18+
* Return the integer denoting the value of such partition.
19+
*/
20+
21+
/**
22+
*@param {number[]} nums
23+
*@return {number}
24+
*/
25+
varfindValueOfPartition=function(nums){
26+
nums.sort((a,b)=>a-b);
27+
letresult=Infinity;
28+
29+
for(leti=1;i<nums.length;i++){
30+
result=Math.min(result,nums[i]-nums[i-1]);
31+
}
32+
33+
returnresult;
34+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp