Customer satisfaction

Alice is the biggest seller in the town who sells notebooks. She has N customers to satisfy. Each customer has three parameters LiRi, and Zi denoting the arrival time, departure time, and quantity of notebooks required.

Each customer has to be supplied a total of Zi notebooks between Li and Ri inclusive. What is the minimum rate of W notebooks per unit time by which if Alice produces so that it satisfies the demand of each customer?

Note that Alice does not need to supply Zi to customer i for per unit time but the total of Zi between Li and Ri and distribution does not need to be uniform.

Example:

Input: n=3, 2 2 1
2 3 3
2 3 1
Output: 2

Approach

Java

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class CustomerSatisfaction {
    public static void main(String[] args) {
        int n = 3;
        HashMap<IntegerLongm = new HashMap<>();
        // declare all array
        int lA[] = { 221 };
        int rA[] = { 233 };
        int zA[] = { 231 };

        // put in hash map
        for (int i = 0; i < n; i++) {
            int l = lA[i];
            int r = rA[i];
            int z = zA[i];
            Long c = m.getOrDefault(r, 0L) + z;
            m.put(r, c);

        }

        TreeMap<IntegerLongtm = new TreeMap<>(m);
        long res = 0L;
        long served = 0L;
        for (Map.Entry<IntegerLonge : tm.entrySet()) {
            served += e.getValue();
            // condition check
            if (served > res * e.getKey()) {
                res = served / e.getKey();
                if (served % e.getKey() > 0)
                    res++;
            }
        }
        System.out.println(res);
    }

}


No comments:

Post a Comment