Stock Maximize Discussions | Algorithms | HackerRank
  • + 0 comments

    Just go from end to beginning

    long stockmax(vector<int> p) {
        long ans = 0; int max_num = 0;
        for(int i = p.size() - 1; i >= 0; i--) 
        {
            max_num = max(max_num, p[i]);
            ans += max(0, max_num - p[i]);
        }
        return ans;
    }