leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1130.cpp (504B)


      1 class Solution {
      2   public:
      3     int mctFromLeafValues(vector<int> &arr) {
      4         int res = 0;
      5         vector<int> stack = {INT_MAX};
      6         for (int n : arr) {
      7             while (stack.back() <= n) {
      8                 int mid = stack.back();
      9                 stack.pop_back();
     10                 res += mid * min(stack.back(), n);
     11             }
     12             stack.push_back(n);
     13         }
     14 
     15         for (int i = 2; i < stack.size(); i++)
     16             res += stack[i] * stack[i - 1];
     17 
     18         return res;
     19     }
     20 };