Array Partition I - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from collections import deque
# 280ms
def arrayPairSum(nums):
    nums.sort()
    ans = []
    temp = 0
 
    q = deque(nums)
    if len(q) % 2 != 0:
        q.pop()
        temp = q.pop()
        q.pop()
 
    while q:
        x = q.popleft()
        y = q.popleft()
        val = min(x, y)
        ans.append(val)
 
    if temp != 0:
        ans.append(temp)
 
    return sum(ans)
 
 
nums = [626512]
arrayPairSum(nums)
cs

 

'알고리즘 문제풀이 with 파이썬 > LeetCode' 카테고리의 다른 글

[배열] Best Time to Buy and Sell Stock  (0) 2021.09.11
[배열] Product of Array Except Self  (0) 2021.09.11
[배열] 3Sum  (0) 2021.09.09
[배열] Trapping Rain Water  (0) 2021.09.05
[배열] Two Sum  (0) 2021.09.04

+ Recent posts