Skip to content

Commit 2a01dfe

Browse files
committed
fd
1 parent 05cd469 commit 2a01dfe

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@
393393
|703|[Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/)|[Java](leetcode/solution/src/KthLargestElementInAStream.java)|100||
394394
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
395395
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
396+
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Java](leetcode/solution/src/BackspaceStringCompare.java)|100||
396397
|857|[Minimum Cost to Hire K Workers](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Java](leetcode/solution/src/MinimumCostToHireKWorkers.java|70||
397398
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
398399
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Stack;
2+
3+
public class BackspaceStringCompare {
4+
5+
public boolean backspaceCompare(String S, String T) {
6+
Stack<Character> stack1 = new Stack<>();
7+
Stack<Character> stack2 = new Stack<>();
8+
for (int i = 0; i < S.length(); i++) {
9+
helper(stack1, S, i);
10+
}
11+
for (int i = 0; i < T.length(); i++) {
12+
helper(stack2, T, i);
13+
}
14+
while (!stack1.isEmpty() && !stack2.isEmpty()) {
15+
if (!stack1.pop().equals(stack2.pop())) {
16+
return false;
17+
}
18+
}
19+
return stack1.isEmpty() && stack2.isEmpty();
20+
}
21+
22+
private void helper(Stack<Character> stack, String s, int i) {
23+
if (i >= s.length()) {
24+
return;
25+
}
26+
char c = s.charAt(i);
27+
if (c == '#') {
28+
if (!stack.isEmpty()) {
29+
stack.pop();
30+
}
31+
} else {
32+
stack.push(c);
33+
}
34+
}
35+
}

leetcode/src/Main.java

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,37 @@
44

55
public class Main {
66

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-
217
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);
3117
}
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;
4021
}
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();
4334
}
35+
} else {
36+
stack.push(c);
4437
}
45-
return money;
4638
}
4739
}
4840

0 commit comments

Comments
 (0)