Alice is the biggest seller in the town who sells notebooks. She has customers to satisfy. Each customer has three parameters , , and denoting the arrival time, departure time, and quantity of notebooks required.
Each customer has to be supplied a total of notebooks between and inclusive. What is the minimum rate of 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 to customer for per unit time but the total of between and and distribution does not need to be uniform.
Example:
Input: n=3, 2 2 12 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<Integer, Long> m = new HashMap<>();// declare all arrayint lA[] = { 2, 2, 1 };int rA[] = { 2, 3, 3 };int zA[] = { 2, 3, 1 };// put in hash mapfor (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<Integer, Long> tm = new TreeMap<>(m);long res = 0L;long served = 0L;for (Map.Entry<Integer, Long> e : tm.entrySet()) {served += e.getValue();// condition checkif (served > res * e.getKey()) {res = served / e.getKey();if (served % e.getKey() > 0)res++;}}System.out.println(res);}}
No comments:
Post a Comment