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)


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