-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC3264.java
More file actions
64 lines (52 loc) · 1.66 KB
/
LC3264.java
File metadata and controls
64 lines (52 loc) · 1.66 KB
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
59
60
61
62
63
64
/*
* LC3264
*/
import java.util.*;
public class LC3264 {
public static int[] getFinalState(int[] nums, int k, int multiplier) {
// [element, index]
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
if (a[0] == b[0]) {
return a[1] - b[1];
}
return a[0] - b[0];
});
// insert element in pq
for (int i = 0; i < nums.length; i++) {
pq.offer(new int[] { nums[i], i });
}
// perform k time operations
while (k > 0) {
int arr[] = pq.poll();
nums[arr[1]] = nums[arr[1]] * multiplier;
pq.offer(new int[] { nums[arr[1]], arr[1] });
k--;
}
return nums;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Nums Array Size : ");
int n = sc.nextInt();
System.out.println();
int[] nums = new int[n];
System.out.println("Enter The Nums Array Elements : ");
for (int i = 0; i < nums.length; i++) {
System.out.printf("[%d] : ", i);
nums[i] = sc.nextInt();
}
System.out.println();
System.out.print("Enter The K : ");
int k = sc.nextInt();
System.out.println();
System.out.print("Enter The Multipliers : ");
int multiplier = sc.nextInt();
System.out.println();
int[] ans = getFinalState(nums, k, multiplier);
System.out.println("Answer : ");
for (int i = 0; i < ans.length; i++) {
System.out.printf("%d, ", ans[i]);
}
sc.close();
}
}