JAVA SOLUTION| BEATS 99.94% | Prefix Min and Suffix Min
Intuition\n Describe your first thoughts on how to solve this problem. \nI thought it of solving in bruteforce, but I could find a best solution\n# Approach\n D
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI thought it of solving in bruteforce, but I could find a best solution\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSo we need to calculate prefix min and suffixmin\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumSum(int[] nums) {\n        int n = nums.length;\n        int[] prefixmin = new int[n];\n        int[] suffixmin = new int[n];\n        int minsofar = nums[0];\n        prefixmin[0] = nums[0];\n        for(int i = 1;i<n;i++)\n        {\n            minsofar = Math.min(minsofar,nums[i]);\n            prefixmin[i] = minsofar;\n        }\n        minsofar = nums[n-1];\n        suffixmin[n-1] = nums[n-1];\n        for(int i = n-2;i>=0;i--)\n        {\n            minsofar = Math.min(minsofar,nums[i]);\n            suffixmin[i] = minsofar;\n        }\n        int ans = Integer.MAX_VALUE;\n        int flag=0;\n        for(int i=1;i<n-1;i++)\n        {\n            if(prefixmin[i-1]<nums[i] && suffixmin[i+1]<nums[i])\n            {\n                ans = Math.min(prefixmin[i-1]+nums[i]+suffixmin[i+1],ans);\n                flag = 1;\n            }\n        }\n        if(flag==0) return -1;\n        return ans;\n    }\n}\n```
['Java']