category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

2398. Maximum Number of Robots Within Budget

class Solution {
    public:
    int maximumRobots(vector<int> &times, vector<int> &costs, long long budget) {
        long long l = 0, j, sum = 0, n = times.size();

        multiset<int> st;
        long long ans = 0;

        for (int r = 0; r < n; ++r) {
            sum += costs[r];
            st.insert(times[r]);

            long long current_budget = (*st.rbegin()) + sum * (r - l + 1);
            if (l <= r && budget < current_budget) {
                sum -= costs[l];
                st.erase(st.find(times[l++]));
                current_budget = st.empty() ? 0 : (*st.rbegin()) + sum * (r - l + 1);
            }
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};