FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/561_Array_Partition_I.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
561_Array_Partition_I.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
20 lines (18 loc) · 557 Bytes
Breadcrumbs
leetcode
/
java
/
561_Array_Partition_I.java
Copy path
File metadata and controls
20 lines (18 loc) · 557 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
https://leetcode.com/problems/array-partition-i/
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2),
..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Sort and sum even index nums.
Time - O(n log n)
Space - O(1)
*/
class
Solution
{
public
int
arrayPairSum
(
int
[]
nums
) {
Arrays
.
sort
(
nums
);
int
result
=
0
;
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
+=
2
) {
result
+=
nums
[
i
];
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL