-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transform it.txt
58 lines (45 loc) · 1.75 KB
/
Transform it.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
You are the boss of the company having N employees. Given an array A of N integers representing representing initial salary of employees. Since you are the Boss you have to make increments in the salary of employees. You have to make increments exactly B times. In a single increment, you can select any one employee and increase its salary by its initial salary.
You have to perform B increments in such a way that the largest salary among the employees(after B increments) is minimized.
Return an integer corresponding to the minimum possible largest salary after B increments.
Input Format
The first argument given is the integer array A.
The second argument given is an integer B.
Output Format
Return an integer corresponding to the minimum possible largest salary after B increments.
Constraints
1 <= N <= 10^6
-10^4 <= A[i] <= 10^4
0 <= B <= 10^5
Example:
Input :
A = [1, 2, 3, 4]
B = 3
Output: 4
Explanation :
After the 1st increment, the salaries of the employees would change to [2, 2, 3, 4]
After the 2nd increment, the salaries of the employees would change to [3, 2, 3, 4]
After the 3rd increment, the salaries of the employees would change to [4, 2, 3, 4]
struct comp{
bool operator()(pair<int,int> &a,pair<int,int> &b){
return a.first+a.second>b.first+b.second;
}
};
int Solution::solve(vector<int> &A, int B) {
int n=A.size();
priority_queue<pair<int,int>,vector<pair<int,int> >,comp> pq;
for(int i=0;i<n;i++){
pq.push({A[i],A[i]});
}
for(int i=0;i<B;i++){
pair<int,int> temp=pq.top();
temp.second +=temp.first;
pq.pop();
pq.push(temp);
}
int ans=INT_MIN;
while(!pq.empty()){
ans=max(ans,pq.top().second);
pq.pop();
}
return ans;
}