Tuesday, May 17, 2022

561. Array Partition I

一刷 05/2022

Time O(nlogn)

Space O(1)


Runtime: 13 ms, faster than 85.57% of Java online submissions for Array Partition I.
Memory Usage: 44 MB, less than 97.44% of Java online submissions for Array Partition I.

 class Solution {

    public int arrayPairSum(int[] nums) {

        // make them as close as possible

        if (nums == null || nums.length == 0) {

            return 0;

        }

        Arrays.sort(nums);

        int sum = 0;

        for (int i = 0; i < nums.length; i += 2) {

            sum += nums[i];

        }

        return sum;

    }

}

No comments:

Post a Comment