leetcode

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

2909.cpp (597B)


      1 class Solution {
      2   public:
      3     int minimumSum(vector<int> &nums) const {
      4         static int dp[100001];
      5         const int n = size(nums);
      6 
      7         dp[0] = nums[0];
      8         for (int i = 1, mini = nums[0]; i < n; i++) {
      9             dp[i] = mini = min(mini, nums[i]);
     10         }
     11 
     12         int res = INT_MAX;
     13         for (int i = n - 2, mini = nums[n - 1]; i > 0; i--) {
     14             if (nums[i] > dp[i - 1] && nums[i] > mini) {
     15                 res = min(res, nums[i] + dp[i - 1] + mini);
     16             }
     17             mini = min(mini, nums[i]);
     18         }
     19 
     20         return res != INT_MAX ? res : -1;
     21     }
     22 };