You are viewing a single comment's thread. Return to all comments →
long soln(int n, std::vector<int> arr) { auto cache = std::vector<int>(n); std::transform_inclusive_scan( arr.begin(), arr.end(), cache.begin(), [&, prev=0](int, bool comp) mutable { prev = comp ? (prev + 1) : 1; return prev; }, [&, prev=-1](int score) mutable { return score > exchange(prev, score); }, 1 ); return std::inner_product( cache.crbegin(), cache.crend() - 1, cache.crbegin() + 1, long{cache.back()}, std::plus<>(), [&, idx = arr.size() - 1](int next, int prev) mutable { auto prev_idx = idx - 1; long ret = (arr[prev_idx] > arr[idx--]) ? max(next + 1, prev) : prev; return ret; }); }
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →