javascript Solution easy to understand O(n) using stack (prefix and sufix)
\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar sumOfBeauties = function(nums) {\n \n    const n = nums.length\n     \n    //we gonna use stack 
```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar sumOfBeauties = function(nums) {\n \n    const n = nums.length\n     \n    //we gonna use stack to maintain the smalest right value to statisfy our first condions\n    let sufix=[nums[n-1]]\n    for(let i = n-2;i>=2;i--){\n        const min=Math.min(sufix[sufix.length-1],nums[i])\n        sufix.push(min)   \n    }\n    \n    //pref will maintain the bigest left value  to statisfy our first condions\n    let pref=nums[0]\n    let sum=0\n    // we gonna itirate our nums array starting from index 1 to index n-2\n    for(let i=1;i<n-1;i++){\n        const current=nums[i]\n        let sufi=sufix.pop()//take the tope value in the stack\n        \n        if(pref<current && sufi>current ) sum+=2 //first condition\n            \n        else if(current >nums[i-1] && current<nums[i+1]) sum+=1; //second condition if first is not satisfied \n        \n        \n        pref=Math.max(pref,current)\n    }\n    \n    return sum \n};\n```
['Stack', 'JavaScript']