|
4 | 4 |
|
5 | 5 | public class Main { |
6 | 6 |
|
7 | | - static class Man { |
8 | | - int quality; |
9 | | - int wage; |
10 | | - |
11 | | - Man(int a, int b) { |
12 | | - quality = a; |
13 | | - wage = b; |
14 | | - } |
15 | | - |
16 | | - double ratio() { |
17 | | - return (double) wage / quality; |
18 | | - } |
19 | | - } |
20 | | - |
21 | 7 | public static class Solution { |
22 | | - public double mincostToHireWorkers(int[] quality, int[] wage, int K) { |
23 | | - Queue<Man> queue = new PriorityQueue<>(new Comparator<Man>() { |
24 | | - @Override |
25 | | - public int compare(Man o1, Man o2) { |
26 | | - return o1.ratio() > o2.ratio() ? 1 : -1; |
27 | | - } |
28 | | - }); |
29 | | - for (int i = 0; i < quality.length; i++) { |
30 | | - queue.offer(new Man(quality[i], wage[i])); |
| 8 | + public boolean backspaceCompare(String S, String T) { |
| 9 | + if (S.length() < T.length()) { |
| 10 | + return backspaceCompare(T, S); |
| 11 | + } |
| 12 | + Stack<Character> stack1 = new Stack<>(); |
| 13 | + Stack<Character> stack2 = new Stack<>(); |
| 14 | + for (int i = 0; i < S.length(); i++) { |
| 15 | + helper(stack1, S, i); |
| 16 | + helper(stack2, T, i); |
31 | 17 | } |
32 | | - double money = Integer.MAX_VALUE, qsum = 0; |
33 | | - Queue<Integer> queue2 = new PriorityQueue<>(Comparator.reverseOrder()); |
34 | | - while (!queue.isEmpty()) { |
35 | | - Man man = queue.poll(); |
36 | | - qsum += man.quality; |
37 | | - queue2.offer(man.quality); |
38 | | - if (queue2.size() > K) { |
39 | | - qsum -= queue2.poll(); |
| 18 | + while (!stack1.isEmpty() && !stack2.isEmpty()) { |
| 19 | + if (!stack1.pop().equals(stack2.pop())) { |
| 20 | + return false; |
40 | 21 | } |
41 | | - if (queue2.size() == K) { |
42 | | - money = Math.min(money, qsum * man.ratio()); |
| 22 | + } |
| 23 | + return stack1.isEmpty() && stack2.isEmpty(); |
| 24 | + } |
| 25 | + |
| 26 | + private void helper(Stack<Character> stack, String s, int i) { |
| 27 | + if (i >= s.length()) { |
| 28 | + return; |
| 29 | + } |
| 30 | + char c = s.charAt(i); |
| 31 | + if (c == '#') { |
| 32 | + if (!stack.isEmpty()) { |
| 33 | + stack.pop(); |
43 | 34 | } |
| 35 | + } else { |
| 36 | + stack.push(c); |
44 | 37 | } |
45 | | - return money; |
46 | 38 | } |
47 | 39 | } |
48 | 40 |
|
|
0 commit comments