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

Commit22d63e2

Browse files
committed
Added 5 solutions
1 parent4c9a3a9 commit22d63e2

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

‎Easy/Largest Triangle Area.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
classSolution {
2+
publicdoublelargestTriangleArea(int[][]points) {
3+
intn =points.length;
4+
doublemaxArea =0;
5+
for (inti =0;i <n;i++) {
6+
for (intj =i +1;j <n;j++) {
7+
for (intk =j +1;k <n;k++) {
8+
maxArea =Math.max(maxArea,getArea(points[i],points[j],points[k]));
9+
}
10+
}
11+
}
12+
13+
returnmaxArea;
14+
}
15+
16+
privatedoublegetArea(int[]P,int[]Q,int[]R) {
17+
return0.5 *Math.abs(P[0] *Q[1] +Q[0] *R[1] +R[0] *P[1]
18+
-P[1] *Q[0] -Q[1] *R[0] -R[1] *P[0]);
19+
}
20+
}

‎Easy/Single-Row Keyboard.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
classSolution {
2+
publicintcalculateTime(Stringkeyboard,Stringword) {
3+
Map<Character,Integer>map =newHashMap<>();
4+
for (inti =0;i <keyboard.length();i++) {
5+
map.put(keyboard.charAt(i),i);
6+
}
7+
8+
intcurrPos =0;
9+
inttotalTime =0;
10+
for (charc :word.toCharArray()) {
11+
totalTime +=Math.abs(currPos -map.get(c));
12+
currPos =map.get(c);
13+
}
14+
15+
returntotalTime;
16+
}
17+
}

‎Medium/Design File System.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classFileSystem {
2+
3+
Map<String,Integer>map;
4+
publicFileSystem() {
5+
map =newHashMap<>();
6+
}
7+
8+
publicbooleancreate(Stringpath,intvalue) {
9+
if (map.containsKey(path)) {
10+
returnfalse;
11+
}
12+
intlastIdx =path.lastIndexOf('/');
13+
if (lastIdx !=0 && !map.containsKey(path.substring(0,lastIdx))) {
14+
returnfalse;
15+
}
16+
map.put(path,value);
17+
returntrue;
18+
}
19+
20+
publicintget(Stringpath) {
21+
returnmap.getOrDefault(path, -1);
22+
}
23+
}
24+
25+
/**
26+
* Your FileSystem object will be instantiated and called as such:
27+
* FileSystem obj = new FileSystem();
28+
* boolean param_1 = obj.create(path,value);
29+
* int param_2 = obj.get(path);
30+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
classSolution {
2+
intMOD =1000000007;
3+
publicintnumRollsToTarget(intd,intf,inttarget) {
4+
Integer[][]dp =newInteger[d +1][target +1];
5+
returnhelper(d,f,target,dp);
6+
}
7+
8+
privateinthelper(intd,intf,inttarget,Integer[][]dp) {
9+
if (d ==0 ||target <=0) {
10+
returnd ==target ?1 :0;
11+
}
12+
if (dp[d][target] !=null) {
13+
returndp[d][target];
14+
}
15+
16+
dp[d][target] =0;
17+
for (inti =1;i <=f;i++) {
18+
dp[d][target] = (dp[d][target] +helper(d -1,f,target -i,dp)) %MOD;
19+
}
20+
21+
returndp[d][target];
22+
}
23+
}

‎Medium/Online Election.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
classTopVotedCandidate {
2+
3+
Map<Integer,Integer>voteCount;
4+
Map<Integer,Integer>timeCount;
5+
intcurrLeaderCount;
6+
List<Winner>winners;
7+
8+
publicTopVotedCandidate(int[]persons,int[]times) {
9+
voteCount =newHashMap<>();
10+
timeCount =newHashMap<>();
11+
currLeaderCount =0;
12+
winners =newArrayList<>();
13+
14+
for (inti =0;i <persons.length;i++) {
15+
booleanisPresent =voteCount.containsKey(persons[i]);
16+
voteCount.put(persons[i],voteCount.getOrDefault(persons[i],0) +1);
17+
timeCount.put(persons[i],times[i]);
18+
if (voteCount.get(persons[i]) >=currLeaderCount) {
19+
winners.add(newWinner(persons[i],times[i]));
20+
currLeaderCount =Math.max(currLeaderCount,voteCount.get(persons[i]));
21+
}
22+
}
23+
}
24+
25+
publicintq(intt) {
26+
returnwinners.get(binarySearchHelper(winners,0,winners.size() -1,t)).val;
27+
}
28+
29+
privateintbinarySearchHelper(List<Winner>winners,intleft,intright,inttarget) {
30+
intidx =winners.size();
31+
while (left <=right) {
32+
intmid = (left +right) /2;
33+
if (winners.get(mid).time ==target) {
34+
idx =mid;
35+
break;
36+
}
37+
elseif (winners.get(mid).time <target) {
38+
idx =mid;
39+
left =mid +1;
40+
}
41+
else {
42+
right =mid -1;
43+
}
44+
}
45+
46+
returnidx ==winners.size() ?0 :idx;
47+
}
48+
}
49+
50+
classWinner {
51+
intval;
52+
inttime;
53+
54+
publicWinner(intval,inttime) {
55+
this.val =val;
56+
this.time =time;
57+
}
58+
}
59+
/**
60+
* Your TopVotedCandidate object will be instantiated and called as such:
61+
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
62+
* int param_1 = obj.q(t);
63+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp