Question 2: There exist n microservices, each with a specific number of running pods…
- User
Question 2:
There exist n microservices, each with a specific number of running pods denoted by
pods[i]for every 1 ≤ i ≤ n. It is essential for these microservices to maintain a pairwise distinct number of running pods. To accomplish this, the following action can be taken:- Select any microservice indexed as ith and raise its running pod count by 1. The expense associated with this action is defined as
cost[i].
Determine the minimum cost necessary to meet this stipulated criterion.
Example:
Given that n = 5, the array pods = [5, 2, 5, 3, 3], and cost = [3, 7, 8, 6, 9], the optimal sequence of operations is as follows:
-
Select
pods[0] = 5and increment it by 1, incurring a cost of 3. This results inpods = [6, 2, 5, 3, 3]. -
Choose
pods[3] = 3and increase it by 1, with a cost of 6. Consequently,pods = [6, 2, 5, 4, 3].
Therefore, the array pods = [6, 2, 5, 4, 3] contains pairwise distinct numbers of active pods.
The total cost amounts to 3 + 6 = 9, representing the minimum possible expenditure.
Function Description: Complete the function getMinCost(pods, cost).
Sample Input For Custom Testing:
3 8 6 8 9 5 7
Sample Output:
7
Explanation:
An ideal sequence of operations is as follows: Start by selecting pods[2] = 8 and increment it by 1, incurring a cost of 7. Consequently, pods = [8, 6, 9].
for that i have written code from collections import defaultdict import heapq
def getMinCost(pods, cost):
mp = defaultdict(list) for p, c in zip(pods, cost): mp[p].append(c) ans = 0 curr = min(mp.keys()) sm = 0 se = [] while True: if not se: if curr not in mp: next_keys = [k for k in mp.keys() if k > curr] if not next_keys: break curr = min(next_keys) if curr in mp: for x in mp[curr]: heapq.heappush(se, -x) sm += x if se: sm += heapq.heappop(se) ans += sm curr += 1 return ansbut this take more time and give TLE so reduce this
- Select any microservice indexed as ith and raise its running pod count by 1. The expense associated with this action is defined as
- Assistant