Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit56cb3fd

Browse files
committed
Added 2 solutions & modified 2 solutions
1 parent098c75a commit56cb3fd

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+
classSolution {
13+
publicList<Interval>employeeFreeTime(List<List<Interval>>schedule) {
14+
List<Interval>ans =newArrayList<>();
15+
PriorityQueue<Interval>pq =newPriorityQueue<>((a,b) ->a.start -b.start);
16+
schedule.forEach(e ->pq.addAll(e));
17+
18+
Intervaltemp =pq.poll();
19+
while (!pq.isEmpty()) {
20+
if (temp.end <pq.peek().start) {
21+
ans.add(newInterval(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+
returnans;
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+
classSolution {
2+
publicdouble[]calcEquation(List<List<String>>equations,double[]values,List<List<String>>queries) {
3+
double[]ans =newdouble[queries.size()];
4+
5+
Map<String,Set<String>>map =newHashMap<>();
6+
Map<String,Double>resMap =newHashMap<>();
7+
8+
for (inti =0;i <equations.size();i++) {
9+
Stringoperand1 =equations.get(i).get(0);
10+
Stringoperand2 =equations.get(i).get(1);
11+
12+
map.computeIfAbsent(operand1,k ->newHashSet<>()).add(operand2);
13+
map.computeIfAbsent(operand2,k ->newHashSet<>()).add(operand1);
14+
15+
resMap.put(operand1 +"|" +operand2,values[i]);
16+
}
17+
18+
for (inti =0;i <queries.size();i++) {
19+
Stringoperand1 =queries.get(i).get(0);
20+
Stringoperand2 =queries.get(i).get(1);
21+
22+
if (!map.containsKey(operand1) || !map.containsKey(operand2)) {
23+
ans[i] = -1.0;
24+
}
25+
elseif (operand1.equals(operand2)) {
26+
ans[i] =1.0;
27+
}
28+
elseif (map.get(operand1).contains(operand2)) {
29+
ans[i] =getSimpleDivisionVal(resMap,operand1,operand2);
30+
}
31+
elseif (operand2.contains(operand1)) {
32+
ans[i] =getSimpleDivisionVal(resMap,operand2,operand1);
33+
}
34+
else {
35+
ans[i] =dfs(map,resMap,operand1,operand2,1.0,newHashSet<>());
36+
}
37+
}
38+
39+
returnans;
40+
}
41+
42+
privatedoubledfs (Map<String,Set<String>>map,Map<String,Double>resMap,Stringoperand1,Stringoperand2,
43+
doublebase,Set<String>visited) {
44+
if (visited.contains(operand1)) {
45+
return -1.0;
46+
}
47+
if (map.get(operand1).contains(operand2)) {
48+
returnbase *getSimpleDivisionVal(resMap,operand1,operand2);
49+
}
50+
51+
visited.add(operand1);
52+
53+
doubleans = -1.0;
54+
Iterator<String>children =map.get(operand1).iterator();
55+
56+
while (children.hasNext()) {
57+
Stringchild =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+
returnans;
64+
}
65+
66+
privatedoublegetSimpleDivisionVal (Map<String,Double>resMap,Stringoperand1,Stringoperand2) {
67+
if (resMap.containsKey(operand1 +"|" +operand2)) {
68+
returnresMap.get(operand1 +"|" +operand2);
69+
}
70+
71+
return1 /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
classSolution {
22
publicint[]exclusiveTime(intn,List<String>logs) {
3-
int[]time =newint[n];
4-
5-
Stack<Task>tasks =newStack<>();
3+
int[]timer =newint[n];
4+
Stack<Integer>stack =newStack<>();
5+
intlastTime =0;;
66

77
for (Stringlog :logs) {
8-
Tasktask =newTask(log);
8+
String[]strs =log.split(":");
9+
intid =Integer.parseInt(strs[0]);
10+
Stringstatus =strs[1];
11+
inttime =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-
Tasktop =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-
returntime;
26+
returntimer;
2427
}
2528
}
26-
27-
classTask {
28-
publicintnum;
29-
publicinttime;
30-
publicStatusstatus;
31-
32-
publicTask(Stringlog) {
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-
enumStatus {
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
classSolution {
11-
12-
List<List<String>>ans =newArrayList<>();
13-
1411
publicList<List<String>>printTree(TreeNoderoot) {
15-
introw =height(root);
16-
intcol = (int)(Math.pow(2,row) -1);
17-
18-
List<String>oneRow =newArrayList<>();
19-
20-
for (inti=0;i<col;i++) {
21-
oneRow.add("");
22-
}
23-
24-
for (inti=0;i<row;i++) {
25-
ans.add(newArrayList<>(oneRow));
12+
intheight =getHeight(root);
13+
intnumOfNodes = (int)Math.pow(2,height) -1;
14+
15+
List<List<String>>ans =newArrayList<>();
16+
17+
for (inti =0;i <height;i++) {
18+
List<String>temp =newArrayList<>();
19+
for (intj =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
returnans;
3129
}
32-
33-
publicvoidhelper(introwNum,intstart,intend,TreeNoderoot) {
34-
35-
if (root ==null)return;
36-
37-
intmid = (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+
privatevoidupdateList (List<List<String>>ans,TreeNoderoot,intidx,intstart,intend) {
32+
if (root ==null) {
33+
return;
34+
}
35+
36+
intmid = (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-
publicintheight(TreeNoderoot) {
49-
if (root ==null)return0;
50-
return1 +Math.max(height(root.left),height(root.right));
42+
43+
privateintgetHeight (TreeNoderoot) {
44+
if (root ==null) {
45+
return0;
46+
}
47+
48+
return1 +Math.max(getHeight(root.left),getHeight(root.right));
5149
}
5250
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp