Skip to content

Commit 56cb3fd

Browse files
committed
Added 2 solutions & modified 2 solutions
1 parent 098c75a commit 56cb3fd

File tree

4 files changed

+154
-65
lines changed

4 files changed

+154
-65
lines changed

Hard/Employee Free Time.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
/*
11+
*/
12+
class Solution {
13+
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
14+
List<Interval> ans = new ArrayList<>();
15+
PriorityQueue<Interval> pq = new PriorityQueue<>((a,b) -> a.start - b.start);
16+
schedule.forEach(e -> pq.addAll(e));
17+
18+
Interval temp = pq.poll();
19+
while (!pq.isEmpty()) {
20+
if (temp.end < pq.peek().start) {
21+
ans.add(new Interval(temp.end, pq.peek().start));
22+
temp = pq.poll();
23+
}
24+
else {
25+
temp = temp.end < pq.peek().end ? pq.peek() : temp;
26+
pq.poll();
27+
}
28+
}
29+
30+
return ans;
31+
}
32+
}

Medium/Evaluate Division.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Solution {
2+
public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
3+
double[] ans = new double[queries.size()];
4+
5+
Map<String, Set<String>> map = new HashMap<>();
6+
Map<String, Double> resMap = new HashMap<>();
7+
8+
for (int i = 0; i < equations.size(); i++) {
9+
String operand1 = equations.get(i).get(0);
10+
String operand2 = equations.get(i).get(1);
11+
12+
map.computeIfAbsent(operand1, k -> new HashSet<>()).add(operand2);
13+
map.computeIfAbsent(operand2, k -> new HashSet<>()).add(operand1);
14+
15+
resMap.put(operand1 + "|" + operand2, values[i]);
16+
}
17+
18+
for (int i = 0; i < queries.size(); i++) {
19+
String operand1 = queries.get(i).get(0);
20+
String operand2 = queries.get(i).get(1);
21+
22+
if (!map.containsKey(operand1) || !map.containsKey(operand2)) {
23+
ans[i] = -1.0;
24+
}
25+
else if (operand1.equals(operand2)) {
26+
ans[i] = 1.0;
27+
}
28+
else if (map.get(operand1).contains(operand2)) {
29+
ans[i] = getSimpleDivisionVal(resMap, operand1, operand2);
30+
}
31+
else if (operand2.contains(operand1)) {
32+
ans[i] = getSimpleDivisionVal(resMap, operand2, operand1);
33+
}
34+
else {
35+
ans[i] = dfs(map, resMap, operand1, operand2, 1.0, new HashSet<>());
36+
}
37+
}
38+
39+
return ans;
40+
}
41+
42+
private double dfs (Map<String, Set<String>> map, Map<String, Double> resMap, String operand1, String operand2,
43+
double base, Set<String> visited) {
44+
if (visited.contains(operand1)) {
45+
return -1.0;
46+
}
47+
if (map.get(operand1).contains(operand2)) {
48+
return base * getSimpleDivisionVal(resMap, operand1, operand2);
49+
}
50+
51+
visited.add(operand1);
52+
53+
double ans = -1.0;
54+
Iterator<String> children = map.get(operand1).iterator();
55+
56+
while (children.hasNext()) {
57+
String child = children.next();
58+
if (!visited.contains(child)) {
59+
ans = Math.max(ans, dfs(map, resMap, child, operand2, base * getSimpleDivisionVal(resMap, operand1, child), visited));
60+
}
61+
}
62+
63+
return ans;
64+
}
65+
66+
private double getSimpleDivisionVal (Map<String, Double> resMap, String operand1, String operand2) {
67+
if (resMap.containsKey(operand1 + "|" + operand2)) {
68+
return resMap.get(operand1 + "|" + operand2);
69+
}
70+
71+
return 1 / resMap.get(operand2 + "|" + operand1);
72+
}
73+
}
Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
class Solution {
22
public int[] exclusiveTime(int n, List<String> logs) {
3-
int[] time = new int[n];
4-
5-
Stack<Task> tasks = new Stack<>();
3+
int[] timer = new int[n];
4+
Stack<Integer> stack = new Stack<>();
5+
int lastTime = 0;;
66

77
for (String log : logs) {
8-
Task task = new Task(log);
8+
String[] strs = log.split(":");
9+
int id = Integer.parseInt(strs[0]);
10+
String status = strs[1];
11+
int time = Integer.parseInt(strs[2]);
912

10-
if (task.status == Status.Start) {
11-
tasks.push(task);
13+
if (!stack.isEmpty()) {
14+
timer[stack.peek()] += time - lastTime;
15+
}
16+
lastTime = time;
17+
if (status.equals("start")) {
18+
stack.push(id);
1219
}
1320
else {
14-
Task top = tasks.pop();
15-
time[top.num] += task.time - top.time + 1;
16-
17-
if (!tasks.isEmpty()) {
18-
time[tasks.peek().num] -= task.time - top.time + 1;
19-
}
21+
timer[stack.pop()]++;
22+
lastTime++;
2023
}
2124
}
2225

23-
return time;
26+
return timer;
2427
}
2528
}
26-
27-
class Task {
28-
public int num;
29-
public int time;
30-
public Status status;
31-
32-
public Task(String log) {
33-
String[] strs = log.split(":");
34-
this.num = Integer.parseInt(strs[0]);
35-
this.time = Integer.parseInt(strs[2]);
36-
this.status = strs[1].equals("start") ? Status.Start : Status.End;
37-
}
38-
}
39-
40-
enum Status {
41-
Start, End;
42-
}

Medium/Print Binary Tree.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,43 @@
88
* }
99
*/
1010
class Solution {
11-
12-
List<List<String>> ans = new ArrayList<>();
13-
1411
public List<List<String>> printTree(TreeNode root) {
15-
int row = height(root);
16-
int col = (int)(Math.pow(2, row) - 1);
17-
18-
List<String> oneRow = new ArrayList<>();
19-
20-
for (int i=0;i<col;i++) {
21-
oneRow.add("");
22-
}
23-
24-
for (int i=0;i<row;i++) {
25-
ans.add(new ArrayList<>(oneRow));
12+
int height = getHeight(root);
13+
int numOfNodes = (int) Math.pow(2, height) - 1;
14+
15+
List<List<String>> ans = new ArrayList<>();
16+
17+
for (int i = 0; i < height; i++) {
18+
List<String> temp = new ArrayList<>();
19+
for (int j = 0; j < numOfNodes; j++) {
20+
temp.add("");
21+
}
22+
23+
ans.add(temp);
2624
}
27-
28-
helper(0, 0, col, root);
29-
25+
26+
updateList(ans, root, 0, 0, numOfNodes);
27+
3028
return ans;
3129
}
32-
33-
public void helper(int rowNum, int start, int end, TreeNode root) {
34-
35-
if (root == null) return;
36-
37-
int mid = (start + end)/2;
38-
39-
List<String> temp = ans.get(rowNum);
40-
temp.set(mid, String.valueOf(root.val));
41-
42-
ans.set(rowNum, temp);
43-
44-
helper(rowNum+1, start, mid-1, root.left);
45-
helper(rowNum+1, mid+1, end, root.right);
30+
31+
private void updateList (List<List<String>> ans, TreeNode root, int idx, int start, int end) {
32+
if (root == null) {
33+
return;
34+
}
35+
36+
int mid = (start + end) / 2;
37+
38+
ans.get(idx).set(mid, String.valueOf(root.val));
39+
updateList(ans, root.left, idx + 1, start, mid - 1);
40+
updateList(ans, root.right, idx + 1, mid + 1, end);
4641
}
47-
48-
public int height(TreeNode root) {
49-
if (root == null) return 0;
50-
return 1 + Math.max(height(root.left), height(root.right));
42+
43+
private int getHeight (TreeNode root) {
44+
if (root == null) {
45+
return 0;
46+
}
47+
48+
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
5149
}
5250
}

0 commit comments

Comments
 (0)