Skip to content

Commit 1156d03

Browse files
committed
Added 2 solutions & modified 1 solution
1 parent af84561 commit 1156d03

3 files changed

+78
-14
lines changed

Easy/Student Attendance Record I.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
2-
public boolean checkRecord(String s) {
3-
int absCount = 0;
4-
if (s.contains("LLL")) {
5-
return false;
2+
public boolean checkRecord(String s) {
3+
int absentCount = 0;
4+
for (int i = 0; i< s.length(); i++) {
5+
if (s.charAt(i) == 'A') {
6+
absentCount++;
7+
}
8+
else if (s.charAt(i) == 'L') {
9+
if (i + 1 < s.length() && i + 2 < s.length() && s.charAt(i + 1) == 'L' && s.charAt(i + 2) == 'L') {
10+
return false;
611
}
7-
8-
for (int i=0;i<s.length();i++) {
9-
if (s.charAt(i) == 'A') {
10-
absCount++;
11-
}
12-
if (absCount>1) {
13-
return false;
14-
}
15-
}
16-
return true;
12+
}
1713
}
14+
return absentCount <= 1;
15+
}
1816
}

Medium/Network Delay Time.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public int networkDelayTime(int[][] times, int N, int K) {
3+
Map<Integer, List<Connection>> map = new HashMap<>();
4+
for (int[] time : times) {
5+
map.computeIfAbsent(time[0], k -> new ArrayList<>()).add(new Connection(time[1], time[2]));
6+
}
7+
Map<Integer, Integer> dist = new HashMap<>();
8+
for (int node = 1; node <= N; node++) {
9+
dist.put(node, Integer.MAX_VALUE);
10+
}
11+
dist.put(K, 0);
12+
boolean[] seen = new boolean[N + 1];
13+
while (true) {
14+
int candNode = -1;
15+
int candDist = Integer.MAX_VALUE;
16+
for (int i = 1; i <= N; ++i) {
17+
if (!seen[i] && dist.get(i) < candDist) {
18+
candDist = dist.get(i);
19+
candNode = i;
20+
}
21+
}
22+
if (candNode < 0) {
23+
break;
24+
}
25+
seen[candNode] = true;
26+
for (Connection con: map.getOrDefault(candNode, new ArrayList<>())) {
27+
dist.put(con.val, Math.min(dist.get(con.val), dist.get(candNode) + con.time));
28+
}
29+
}
30+
int ans = 0;
31+
for (int cand: dist.values()) {
32+
if (cand == Integer.MAX_VALUE) {
33+
return -1;
34+
}
35+
ans = Math.max(ans, cand);
36+
}
37+
return ans;
38+
}
39+
}
40+
41+
class Connection {
42+
int val;
43+
int time;
44+
45+
public Connection(int val, int time) {
46+
this.val = val;
47+
this.time = time;
48+
}
49+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int numOfSubarrays(int[] arr, int k, int threshold) {
3+
int sum = 0;
4+
int start = 0;
5+
int count = 0;
6+
for (int i = 0; i < arr.length; i++) {
7+
sum += arr[i];
8+
if (i - start + 1 == k) {
9+
if ((double) sum / k >= threshold) {
10+
count++;
11+
}
12+
sum -= arr[start++];
13+
}
14+
}
15+
return count;
16+
}
17+
}

0 commit comments

Comments
 (0)