Java | Array | easy to understand
\n    //Runtime: 10 ms, faster than 61.96% of Java online submissions for Sum of Beauty in the Array.\n    //Memory Usage: 95.5 MB, less than 65.03% of Java onl
```\n    //Runtime: 10 ms, faster than 61.96% of Java online submissions for Sum of Beauty in the Array.\n    //Memory Usage: 95.5 MB, less than 65.03% of Java online submissions for Sum of Beauty in the Array.\n    //Time: O(2N); Space: O(N)\n    //Time: O(N); Space: O(N)\n    public int sumOfBeauties(int[] nums) {\n        int[] minInRight = new int[nums.length];\n        minInRight[nums.length - 1] = nums[nums.length - 1];\n        for (int i = nums.length - 2; i >= 2; i--)\n            minInRight[i] = Math.min(minInRight[i + 1], nums[i]);\n\n        int maxInLeft = nums[0], sum = 0;\n        for (int i = 1; i < nums.length - 1; i++){\n            if (nums[i] > maxInLeft && nums[i] < minInRight[i + 1])  sum += 2;\n            else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) sum += 1;\n\n            maxInLeft = Math.max(maxInLeft, nums[i]);\n        }\n        return sum;\n    }\n\t\n\t\n\t    //Runtime: 13 ms, faster than 36.81% of Java online submissions for Sum of Beauty in the Array.\n    //Memory Usage: 92.9 MB, less than 67.48% of Java online submissions for Sum of Beauty in the Array.\n    //Time: O(2N); Space: O(2N)\n    public int sumOfBeauties1(int[] nums) {\n        int[] max = new int[nums.length];\n        int[] min = new int[nums.length];\n        max[0] = nums[0];\n        min[nums.length - 1] = nums[nums.length - 1];\n\n        for (int i = 1; i < nums.length - 1; i++){\n            max[i] =  Math.max(max[i - 1], nums[i]);\n            int j = nums.length - 1 - i;\n            min[j] = Math.min(min[j + 1], nums[j]);\n        }\n\n        int sum = 0;\n        for (int i = 1; i < nums.length - 1; i++){\n            if (nums[i] > max[i - 1] && nums[i] < min[i + 1])  sum += 2;\n            else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) sum += 1;\n        }\n        return sum;\n    }\n```
['Java']