-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoursera.java
More file actions
150 lines (141 loc) · 4.61 KB
/
Coursera.java
File metadata and controls
150 lines (141 loc) · 4.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
public class Coursera {
public void finalPrice(int[] prices) {
//print two lines of output
// 1. the cost for all n items
// 2. indices of any non-discounted items space-separated
int n = prices.length;
int[] result = new int[n];
// initialization to result
for (int i = 0; i < n; i++) {
result[i] = prices[i];
}
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < n; i++) {
int price = prices[i];
while (!stack.isEmpty() && prices[stack.peek()] > price) {
int originalPrice = prices[stack.peek()];
result[stack.pop()] = originalPrice - price;
}
stack.push(i);
}
int total = 0;
for (int i = 0; i < n; i++) {
total += result[i];
}
System.out.println(total);
for (int i = 0; i < n; i++) {
if (result[i] == prices[i]) {
System.out.print(i);
if (i != n - 1) {
System.out.print(" ");
}
}
}
}
public long playList(int N, int L, int K) {
if (L > N) {
if (L >= K + 1) {
return 0;
}
}
int a = L / (K + 1);
int b = L % (K + 1);
long partA = 1;
long partB = 1;
for (int i = 0; i <= K; i++) {
partA *= N-i;
}
partA = (long) Math.pow(partA, a);
for (int i = 0; i < b; i++) {
partB *= N-i;
}
return (long) (partA * partB % (1e9 + 7));
}
public String[] royalGames(String[] names) {
Queue<String> pq = new PriorityQueue<String>(10, new Comparator<String>() {
public int compare(String s1, String s2) {
String[] name1 = s1.split(" ");
String[] name2 = s2.split(" ");
if (name1[0].equals(name2[0])) {
return getRomanNum(name2[1]) - getRomanNum(name1[1]);
} else {
return name1[0].compareTo(name2[0]);
}
}
});
for (int i = 0; i < names.length; i++) {
pq.offer(names[i]);
}
for (int i = 0; i < names.length; i++) {
names[i] = pq.poll();
}
return names;
}
private int getRomanNum(String r1) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int sum = 0;
for (int i = 0; i < r1.length(); i++) {
int curr = map.get(r1.charAt(i));
if (i < r1.length() - 1 && curr < map.get(r1.charAt(i + 1))) {
sum -= curr;
} else {
sum += curr;
}
}
return sum;
}
public int segment(int x, int[] arr) {
if (arr == null || x <= 0) {
return 0;
}
int max = Integer.MIN_VALUE;
Deque<Integer> deque = new ArrayDeque<>();
for (int i = 0; i < arr.length; i++) {
while(!deque.isEmpty() && deque.peek() < i - x + 1) deque.poll();
while(!deque.isEmpty() && arr[deque.peekLast()] > arr[i]) deque.pollLast();
deque.offer(i);
if (i + 1 >= x) {
max = Math.max(max, arr[deque.peek()]);
}
}
return max;
}
public static void main(String[] args) {
Coursera c = new Coursera();
/*finalPrice*/
// int[] prices = new int[]{5,1,3,4,6,2};
// c.finalPrice(prices);
/* playList*/
// System.out.println(c.playList(20, 9, 3));
// System.out.println(Math.pow(116280,2) * 20 % (1e9 + 7));
/* royalNames */
// String[] names = new String[]{"Albert II","Polo IV","Alw V","Elizabeth XXV", "Albert XL","Polo XLVI"};
// names = c.royalGames(names);
// for (int i = 0; i < names.length; i++) {
// System.out.println(names[i]);
// }
/* */
int x = 3;
int[] arr = new int[]{2,5,4,6,8};
System.out.println(c.segment(x, arr));
}
}