Skip to content

Commit a2233ea

Browse files
solves array partition I
1 parent 96231cd commit a2233ea

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
| 551 | [Student Attendance Record I](https://leetcode.com/problems/student-attendance-record-i) | Easy | [![Java](assets/java.png)](src/StudentAttendanceRecordI.java) [![Python](assets/python.png)](python/student_attendance_record_I.py) |
148148
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii) | Easy | [![Java](assets/java.png)](src/ReverseWordsInStringIII.java) [![Python](assets/python.png)](python/reverse_words_in_string_iii.py) |
149149
| 559 | [Maximum Depth of N-Ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree) | Easy | [![Java](assets/java.png)](src/MaximumDepthOfNAryTree.java) [![Python](assets/python.png)](python/maximum_depth_of_n_ary_tree.py) |
150-
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i) | Easy | |
150+
| 561 | [Array Partition I](https://leetcode.com/problems/array-partition-i) | Easy | [![Java](assets/java.png)](src/ArrayPartitionI.java) [![Python](assets/python.png)](python/array_partiton_I.py) |
151151
| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt) | Easy | |
152152
| 566 | [Reshape The Matrix](https://leetcode.com/problems/reshape-the-matrix) | Easy | |
153153
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | |

python/array_partiton_I.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def arrayPairSum(self, nums: List[int]) -> int:
6+
nums.sort()
7+
result = 0
8+
for i in range(0, len(nums) // 2):
9+
result += min(nums[2 * i], nums[2 * i + 1])
10+
return result

src/ArrayPartitionI.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.Arrays;
2+
3+
public class ArrayPartitionI {
4+
public int arrayPairSum(int[] nums) {
5+
Arrays.sort(nums);
6+
int result = 0;
7+
for (int index = 0 ; index < nums.length ; index += 2) {
8+
result += Math.min(nums[index], nums[index + 1]);
9+
}
10+
return result;
11+
}
12+
}

0 commit comments

Comments
 (0)