Skip to content

Commit 22d63e2

Browse files
committed
Added 5 solutions
1 parent 4c9a3a9 commit 22d63e2

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+
class Solution {
2+
public double largestTriangleArea(int[][] points) {
3+
int n = points.length;
4+
double maxArea = 0;
5+
for (int i = 0; i < n; i++) {
6+
for (int j = i + 1; j < n; j++) {
7+
for (int k = j + 1; k < n; k++) {
8+
maxArea = Math.max(maxArea, getArea(points[i], points[j], points[k]));
9+
}
10+
}
11+
}
12+
13+
return maxArea;
14+
}
15+
16+
private double getArea(int[] P, int[] Q, int[] R) {
17+
return 0.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+
class Solution {
2+
public int calculateTime(String keyboard, String word) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
for (int i = 0; i < keyboard.length(); i++) {
5+
map.put(keyboard.charAt(i), i);
6+
}
7+
8+
int currPos = 0;
9+
int totalTime = 0;
10+
for (char c : word.toCharArray()) {
11+
totalTime += Math.abs(currPos - map.get(c));
12+
currPos = map.get(c);
13+
}
14+
15+
return totalTime;
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+
class FileSystem {
2+
3+
Map<String, Integer> map;
4+
public FileSystem() {
5+
map = new HashMap<>();
6+
}
7+
8+
public boolean create(String path, int value) {
9+
if (map.containsKey(path)) {
10+
return false;
11+
}
12+
int lastIdx = path.lastIndexOf('/');
13+
if (lastIdx != 0 && !map.containsKey(path.substring(0, lastIdx))) {
14+
return false;
15+
}
16+
map.put(path, value);
17+
return true;
18+
}
19+
20+
public int get(String path) {
21+
return map.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+
class Solution {
2+
int MOD = 1000000007;
3+
public int numRollsToTarget(int d, int f, int target) {
4+
Integer[][] dp = new Integer[d + 1][target + 1];
5+
return helper(d, f, target, dp);
6+
}
7+
8+
private int helper(int d, int f, int target, Integer[][] dp) {
9+
if (d == 0 || target <= 0) {
10+
return d == target ? 1 : 0;
11+
}
12+
if (dp[d][target] != null) {
13+
return dp[d][target];
14+
}
15+
16+
dp[d][target] = 0;
17+
for (int i = 1; i <= f; i++) {
18+
dp[d][target] = (dp[d][target] + helper(d - 1, f, target - i, dp)) % MOD;
19+
}
20+
21+
return dp[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+
class TopVotedCandidate {
2+
3+
Map<Integer, Integer> voteCount;
4+
Map<Integer, Integer> timeCount;
5+
int currLeaderCount;
6+
List<Winner> winners;
7+
8+
public TopVotedCandidate(int[] persons, int[] times) {
9+
voteCount = new HashMap<>();
10+
timeCount = new HashMap<>();
11+
currLeaderCount = 0;
12+
winners = new ArrayList<>();
13+
14+
for (int i = 0; i < persons.length; i++) {
15+
boolean isPresent = 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(new Winner(persons[i], times[i]));
20+
currLeaderCount = Math.max(currLeaderCount, voteCount.get(persons[i]));
21+
}
22+
}
23+
}
24+
25+
public int q(int t) {
26+
return winners.get(binarySearchHelper(winners, 0, winners.size() - 1, t)).val;
27+
}
28+
29+
private int binarySearchHelper(List<Winner> winners, int left, int right, int target) {
30+
int idx = winners.size();
31+
while (left <= right) {
32+
int mid = (left + right) / 2;
33+
if (winners.get(mid).time == target) {
34+
idx = mid;
35+
break;
36+
}
37+
else if (winners.get(mid).time < target) {
38+
idx = mid;
39+
left = mid + 1;
40+
}
41+
else {
42+
right = mid - 1;
43+
}
44+
}
45+
46+
return idx == winners.size() ? 0 : idx;
47+
}
48+
}
49+
50+
class Winner {
51+
int val;
52+
int time;
53+
54+
public Winner(int val, int time) {
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)