Recursive Java solution


  • 0
    A

    Not the fastest solution, but as a newbie, I found this an easy way to practice recursion

    public class Solution {
         public int recurse(int[] nums, int i) {
            if(i <= 1) return nums[0];
            return Math.min(nums[i],nums[i-1]) + recurse(nums,i-2);
        }
        
        public int arrayPairSum(int[] nums) {
            if(nums.length == 2) return nums[0];
            Arrays.sort(nums);
            return recurse(nums, nums.length-1);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.