Skip to content

Commit ce8b51d

Browse files
committed
Update
1 parent 3c4b752 commit ce8b51d

24 files changed

+1006
-6
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package array;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/moving-average-from-data-stream
8+
* Difficulty: Easy
9+
* Time complexity: O(1)
10+
* Space complexity: O(m)
11+
*/
12+
public class MovingAverageFromDataStream {
13+
14+
private static class MovingAverageViaDeque {
15+
16+
private final Deque<Integer> window;
17+
private final int maxWindowSize;
18+
private int windowSum;
19+
20+
public MovingAverageViaDeque(int size) {
21+
this.window = new LinkedList<>();
22+
this.maxWindowSize = size;
23+
}
24+
25+
public double next(int val) {
26+
windowSum += val;
27+
window.offerLast(val);
28+
if (window.size() > maxWindowSize) {
29+
windowSum -= window.pollFirst();
30+
}
31+
32+
return (double) windowSum / window.size();
33+
}
34+
}
35+
36+
private static class MovingAverageViaRingBuffer {
37+
38+
private final int[] window;
39+
private int current;
40+
private int windowSum;
41+
private int count;
42+
43+
public MovingAverageViaRingBuffer(int size) {
44+
this.window = new int[size];
45+
}
46+
47+
public double next(int val) {
48+
count++;
49+
windowSum = windowSum - window[current] + val;
50+
window[current] = val;
51+
current = (current + 1) % window.length;
52+
53+
return (double) windowSum / Math.min(count, window.length);
54+
}
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/number-of-equivalent-domino-pairs
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(1)
11+
*/
12+
public class NumberOfEquivalentDominoPairs {
13+
14+
public int numEquivDominoPairs(int[][] dominoes) {
15+
Map<Integer, Integer> freqMap = new HashMap<>();
16+
int pairs = 0;
17+
18+
for (int[] domino : dominoes) {
19+
// hash
20+
int value = Math.min(domino[0], domino[1]) * 10 + Math.max(domino[0], domino[1]);
21+
22+
// form a pair with each previously seen equivalent
23+
pairs += freqMap.getOrDefault(value, 0);
24+
25+
// increment frequency counter
26+
freqMap.merge(value, 1, Integer::sum);
27+
}
28+
29+
return pairs;
30+
}
31+
}

src/binary_search/FirstBadVersion.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public int firstBadVersion(int n) {
2525
return left;
2626
}
2727

28+
// API call
2829
private boolean isBadVersion(int n) {
2930
return true;
3031
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/longest-palindromic-subsequence
5+
* Difficulty: Medium
6+
* Time complexity: O(n^2)
7+
* Space complexity: O(n^2)
8+
*/
9+
public class LongestPalindromicSubsequence {
10+
11+
public int longestPalindromeSubseq(String s) {
12+
int[][] dp = new int[s.length()][s.length()];
13+
14+
for (int right = 0; right < s.length(); right++) {
15+
for (int left = right; left >= 0; left--) {
16+
if (left == right) {
17+
dp[left][right] = 1;
18+
continue;
19+
}
20+
21+
if (s.charAt(left) == s.charAt(right)) {
22+
int length = right - left + 1;
23+
dp[left][right] = length <= 3 ? length : dp[left + 1][right - 1] + 2;
24+
} else {
25+
dp[left][right] = Math.max(dp[left + 1][right], dp[left][right - 1]);
26+
}
27+
}
28+
}
29+
30+
return dp[0][s.length() - 1];
31+
}
32+
}

src/graph/FindTheCelebrity.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graph;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/find-the-celebrity
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(1) or O(n) with cache
11+
*/
12+
public class FindTheCelebrity {
13+
14+
private final Map<Relation, Boolean> cache = new HashMap<>();
15+
16+
public int findCelebrity(int n) {
17+
int candidate = 0;
18+
for (int man = 1; man < n; man++) {
19+
if (cachedKnows(candidate, man)) {
20+
candidate = man;
21+
}
22+
}
23+
24+
if (isCelebrity(candidate, n)) {
25+
return candidate;
26+
}
27+
28+
return -1;
29+
}
30+
31+
private boolean isCelebrity(int candidate, int n) {
32+
for (int man = 0; man < n; man++) {
33+
if (man == candidate) continue;
34+
if (cachedKnows(candidate, man) || !cachedKnows(man, candidate)) {
35+
return false;
36+
}
37+
}
38+
39+
return true;
40+
}
41+
42+
// use if API calls are expensive
43+
private boolean cachedKnows(int a, int b) {
44+
return cache.computeIfAbsent(new Relation(a, b), __ -> knows(a, b));
45+
}
46+
47+
private record Relation(int a, int b) {
48+
}
49+
50+
// API call
51+
private boolean knows(int a, int b) {
52+
return true;
53+
}
54+
}

src/graph/FindTheTownJudge.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package graph;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/find-the-town-judge
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class FindTheTownJudge {
10+
11+
public int findJudge(int n, int[][] trust) {
12+
int[] trusts = new int[n + 1];
13+
int[] trustedBy = new int[n + 1];
14+
15+
for (int[] relation : trust) {
16+
trusts[relation[0]]++;
17+
trustedBy[relation[1]]++;
18+
}
19+
20+
for (int man = 1; man <= n; man++) {
21+
if (trusts[man] == 0 && trustedBy[man] == n - 1) {
22+
return man;
23+
}
24+
}
25+
26+
return -1;
27+
}
28+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package graph;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/flower-planting-with-no-adjacent
7+
* Difficulty: Medium
8+
* Time complexity: O(n)
9+
* Space complexity: O(n)
10+
*/
11+
public class FlowerPlantingWithNoAdjacent {
12+
13+
public int[] gardenNoAdj(int n, int[][] paths) {
14+
Map<Integer, List<Integer>> adjList = buildAdjList(paths);
15+
16+
int[] colors = new int[n];
17+
for (int garden = 0; garden < n; garden++) {
18+
if (colors[garden] == 0) {
19+
bfs(adjList, colors, garden);
20+
}
21+
}
22+
23+
return colors;
24+
}
25+
26+
private void bfs(Map<Integer, List<Integer>> adjList, int[] colors, int start) {
27+
Queue<Integer> planned = new LinkedList<>();
28+
planned.offer(start);
29+
colors[start] = 1;
30+
31+
while (!planned.isEmpty()) {
32+
int current = planned.poll();
33+
34+
for (int neighbor : adjList.getOrDefault(current, List.of())) {
35+
if (colors[neighbor] == 0) {
36+
// colors are 1-indexed: (colors[current] - 1 + 1) % 4 + 1 = colors[current] % 4 + 1
37+
colors[neighbor] = colors[current] % 4 + 1;
38+
planned.offer(neighbor);
39+
} else if (colors[neighbor] == colors[current]) {
40+
colors[neighbor] = colors[current] % 4 + 1;
41+
}
42+
}
43+
}
44+
}
45+
46+
public int[] gardenNoAdjViaGreedyAlgo(int n, int[][] paths) {
47+
Map<Integer, List<Integer>> adjList = buildAdjList(paths);
48+
49+
int[] colors = new int[n];
50+
for (int garden = 0; garden < n; garden++) {
51+
Set<Integer> usedColors = new HashSet<>();
52+
for (int neighbor : adjList.getOrDefault(garden, List.of())) {
53+
usedColors.add(colors[neighbor]);
54+
}
55+
56+
for (int color = 1; color <= 4; color++) {
57+
if (!usedColors.contains(color)) {
58+
colors[garden] = color;
59+
break;
60+
}
61+
}
62+
}
63+
64+
return colors;
65+
}
66+
67+
private Map<Integer, List<Integer>> buildAdjList(int[][] paths) {
68+
Map<Integer, List<Integer>> adjList = new HashMap<>();
69+
for (int[] path : paths) {
70+
adjList.computeIfAbsent(path[0] - 1, __ -> new ArrayList<>()).add(path[1] - 1);
71+
adjList.computeIfAbsent(path[1] - 1, __ -> new ArrayList<>()).add(path[0] - 1);
72+
}
73+
74+
return adjList;
75+
}
76+
}

src/graph/IsGraphBipartite.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package graph;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/is-graph-bipartite
9+
* Difficulty: Medium
10+
* Time complexity: O(n)
11+
* Space complexity: O(n)
12+
*/
13+
public class IsGraphBipartite {
14+
15+
private static final int RED = 0;
16+
private static final int BLUE = 1;
17+
private static final int UNDEFINED = -1;
18+
19+
public boolean isBipartite(int[][] graph) {
20+
int[] colors = new int[graph.length];
21+
Arrays.fill(colors, UNDEFINED);
22+
23+
for (int i = 0; i < graph.length; i++) {
24+
if (colors[i] == UNDEFINED && !isBipartite(i, graph, colors)) {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
}
31+
private boolean isBipartite(int start, int[][] graph, int[] colors) {
32+
Queue<Integer> planned = new LinkedList<>();
33+
planned.offer(start);
34+
colors[start] = RED;
35+
36+
while (!planned.isEmpty()) {
37+
int current = planned.poll();
38+
39+
for (int neighbor : graph[current]) {
40+
if (colors[neighbor] == UNDEFINED) {
41+
// color nodes red-blue-red-blue-...
42+
colors[neighbor] = (colors[current] + 1) % 2;
43+
planned.offer(neighbor);
44+
} else if (colors[neighbor] == colors[current]) {
45+
// if colors of neighbors match – graph is not bipartile
46+
return false;
47+
}
48+
}
49+
}
50+
51+
return true;
52+
}
53+
}

src/graph/MaximalNetworkRank.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package graph;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/maximal-network-rank
5+
* Difficulty: Medium
6+
* Time complexity: O(n^2)
7+
* Space complexity: O(n^2)
8+
*/
9+
public class MaximalNetworkRank {
10+
11+
public int maximalNetworkRank(int n, int[][] roads) {
12+
int[] ranks = new int[n];
13+
int[][] adjMatrix = new int[n][n];
14+
for (int[] road : roads) {
15+
ranks[road[0]]++;
16+
ranks[road[1]]++;
17+
adjMatrix[road[0]][road[1]] = 1;
18+
adjMatrix[road[1]][road[0]] = 1;
19+
}
20+
21+
int maxRank = 0;
22+
for (int i = 0; i < n; i++) {
23+
for (int j = i + 1; j < n; j++) {
24+
// road between 'i' and 'j' is only counted once -> subtract 1 if connection exists
25+
maxRank = Math.max(maxRank, ranks[i] + ranks[j] - adjMatrix[i][j]);
26+
}
27+
}
28+
29+
return maxRank;
30+
}
31+
}

0 commit comments

Comments
 (0)