From 39d8fd2e82558579d6a586aad5a591fc7672e220 Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Sun, 20 Oct 2024 14:53:37 +0530 Subject: [PATCH 1/9] HARD --- .idea/workspace.xml | 30 +++---- ...LongestChunkedPalindromeDecomposition.java | 80 +++++++++++++++++++ 2 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 data-structures/src/main/java/com/thealgorithm/strings/LongestChunkedPalindromeDecomposition.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f548824..e26dd78 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,15 +5,7 @@ - - - - - - - - - + - + - - @@ -523,7 +515,15 @@ diff --git a/data-structures/src/main/java/com/thealgorithm/strings/LongestChunkedPalindromeDecomposition.java b/data-structures/src/main/java/com/thealgorithm/strings/LongestChunkedPalindromeDecomposition.java new file mode 100644 index 0000000..5a122dc --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/strings/LongestChunkedPalindromeDecomposition.java @@ -0,0 +1,80 @@ +package com.thealgorithm.strings; + +/** + * @author: Subham Santra + */ +public class LongestChunkedPalindromeDecomposition { + static class Solution { + private final long p = 31; + private final long mod = (int) 1e9 + 7; + private final long[] p_pow = new long[1001]; + + public Solution() { + p_pow[0] = 1; + for (int i = 1; i <= 1000; ++i) { + p_pow[i] = p_pow[i - 1] * p; + p_pow[i] = (p_pow[i] + mod) % mod; + } + } + + void rollingHash(long[] rh, String txt) { + for (int i = 0; i < txt.length(); ++i) { + int c = txt.charAt(i) - 'a' + 1; + rh[i + 1] = ((rh[i] * 31) % mod + c + mod) % mod; + } + } + + long hash(long[] RH, int i, int j) { + return ((RH[j + 1] - RH[i] * p_pow[j - i + 1]) % mod + mod) % mod; + } + + public int longestDecomposition(String text) { + int[][] DP = new int[text.length()][text.length()]; + long[] RH = new long[text.length() + 1]; + for (int i = 0; i < text.length(); ++i) { + for (int j = 0; j < text.length(); ++j) { + DP[i][j] = -1; + } + } + rollingHash(RH, text); + // System.out.println(Arrays.toString(RH)); + return decompose(text, 0, text.length() - 1, DP, RH); + } + + int decompose(String text, int lo, int hi, int[][] DP, long[] RH) { + if (lo == hi + 1) return 0; + if (lo > hi) return -1; + + if (DP[lo][hi] != -1) return DP[lo][hi]; + + int ans = 1; + + for (int len = 1; len <= (hi - lo + 1) / 2; ++len) { + long h1 = hash(RH, lo, lo + len - 1); + long h2 = hash(RH, hi - len + 1, hi); + + // System.out.printf( + // "%s %d %s %d\n", + // text.substring(lo, lo + len), h1, text.substring(hi - len + 1, hi + 1), h2); + + if (h1 == h2) { + int a = decompose(text, lo + len, hi - len, DP, RH); + if (a > -1) ans = Math.max(a + 2, ans); + } + } + + return DP[lo][hi] = ans; + } + } + + public static void main(String[] args) { + System.out.println(new Solution().longestDecomposition("ghiabcdefhelloadamhelloabcdefghi")); + System.out.println( + new Solution().longestDecomposition("wuamlwxskgjzegshjhcwchkgjzegshjwuamlwxs")); + System.out.println(new Solution().longestDecomposition("antaprezatepzapreanta")); + System.out.println(new Solution().longestDecomposition("aabaab")); + System.out.println(new Solution().longestDecomposition("aabaabaab")); + System.out.println(new Solution().longestDecomposition("aabaabaabaab")); + System.out.println(new Solution().longestDecomposition("aabaabaabaabc")); + } +} From c5e53fbd34606af0d157ae222f5d4b322684c40d Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Mon, 28 Oct 2024 13:42:14 +0530 Subject: [PATCH 2/9] HARD --- .idea/workspace.xml | 38 ++++++---- .../thealgorithm/graph/DijkstraAlgorithm.java | 76 +++++++++++++++++++ .../com/thealgorithm/graph/GraphCreator.java | 30 ++++++++ ...SingleSourceShortestPathUsingDijkstra.java | 52 +++++++++++++ .../thealgorithm/heap/TopKFrequentWords.java | 37 +++++++++ 5 files changed, 219 insertions(+), 14 deletions(-) create mode 100644 data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java create mode 100644 data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java create mode 100644 data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java create mode 100644 data-structures/src/main/java/com/thealgorithm/heap/TopKFrequentWords.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e26dd78..9fe844b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,8 +4,9 @@ - - + + + - + - - @@ -523,7 +524,15 @@ @@ -562,7 +571,8 @@ - diff --git a/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java new file mode 100644 index 0000000..e1e4f9d --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java @@ -0,0 +1,76 @@ +package com.thealgorithm.graph; + +import static java.util.AbstractMap.*; + +import java.util.*; + +public class DijkstraAlgorithm { + public static class InvalidWeightException extends Exception { + private String msg; + + public InvalidWeightException(String msg) { + super(msg); + this.msg = msg; + } + } + + void dijkstra(int[][] graph, int n, int source) throws InvalidWeightException { + int[] dist = new int[n]; + Arrays.fill(dist, Integer.MAX_VALUE); + dist[source] = 0; + + Set shortestPathFound = new HashSet<>(); + PriorityQueue> priorityQueue = + new PriorityQueue<>(Comparator.comparingInt(Entry::getValue)); + + priorityQueue.offer(new SimpleEntry<>(source, 0)); + shortestPathFound.add(source); + + while (!priorityQueue.isEmpty()) { + Entry shortedstEntry = priorityQueue.poll(); + int node = shortedstEntry.getKey(); + int cost = shortedstEntry.getValue(); + + for (int i = 0; i < n; ++i) { + if (graph[node][i] < 0) { + throw new InvalidWeightException("Invalid data"); + } + if (node != i && graph[node][i] != 0) { + if (dist[i] > cost + graph[node][i]) { + dist[i] = cost + graph[node][i]; + int finalI = i; + priorityQueue.removeIf(e -> e.getKey() == finalI); + priorityQueue.offer(new SimpleEntry<>(i, dist[i])); + } + } + } + shortestPathFound.add(node); + if (shortestPathFound.size() == n) break; + } + + System.out.println(Arrays.toString(dist)); + } + + public static void main(String[] args) throws InvalidWeightException { + + GraphCreator graphCreator = + GraphCreator.getUndirected(9) + .addEdge(0, 1, 4) + .addEdge(0, 7, 8) + .addEdge(1, 7, 11) + .addEdge(1, 2, 8) + .addEdge(2, 8, 2) + .addEdge(8, 6, 6) + .addEdge(8, 6, 6) + .addEdge(7, 6, 1) + .addEdge(7, 8, 7) + .addEdge(6, 5, 2) + .addEdge(2, 5, 4) + .addEdge(2, 3, 7) + .addEdge(5, 3, 14) + .addEdge(5, 4, 10) + .addEdge(3, 4, -9); + + new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 0); + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java b/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java new file mode 100644 index 0000000..fc0f279 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java @@ -0,0 +1,30 @@ +package com.thealgorithm.graph; + +import lombok.Value; + +@Value +public class GraphCreator { + final int size; + final int[][] graph; + final boolean undirected; + + private GraphCreator(int size, boolean undirected) { + this.size = size; + this.graph = new int[size][size]; + this.undirected = undirected; + } + + public static GraphCreator getUndirected(int n) { + return new GraphCreator(n, true); + } + + public static GraphCreator getDirected(int n) { + return new GraphCreator(n, false); + } + + public GraphCreator addEdge(int u, int v, int w) { + graph[u][v] = w; + if (undirected) graph[v][u] = w; + return this; + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java b/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java new file mode 100644 index 0000000..14d7dd2 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java @@ -0,0 +1,52 @@ +package com.thealgorithm.graph; + +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; +import java.util.Set; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +@Getter +@EqualsAndHashCode(onlyExplicitlyIncluded = true) +@AllArgsConstructor +class HeapEntry { + @EqualsAndHashCode.Include int node; + int distanceFromSource; +} + +class MaxHeap { + + public HeapEntry extractMin() { + return null; + } + + public void update(int node, int newVal) {} +} + +/** + * @author: Subham Santra + */ +public class SingleSourceShortestPathUsingDijkstra { + + private List findShortestPath(int[][] graph, int source) { + PriorityQueue priorityQueue = + new PriorityQueue<>(Comparator.comparingInt(HeapEntry::getDistanceFromSource)); + + // Set<> + + for (int i = 0; i < graph.length; ++i) { + if (i == source) { + priorityQueue.offer(new HeapEntry(i, 0)); + } else { + priorityQueue.offer(new HeapEntry(i, Integer.MAX_VALUE)); + } + } + } + + public static void main(String[] args) { + int[][] graph = new int[9][9]; + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/heap/TopKFrequentWords.java b/data-structures/src/main/java/com/thealgorithm/heap/TopKFrequentWords.java new file mode 100644 index 0000000..b49042a --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/heap/TopKFrequentWords.java @@ -0,0 +1,37 @@ +package com.thealgorithm.heap; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @author: Subham Santra + */ +public class TopKFrequentWords { + public List topKFrequent(String[] words, int k) { + return Arrays.stream(words) + .collect(Collectors.toMap(Function.identity(), s -> 1, Integer::sum)) + .entrySet() + .stream() + .sorted( + (kv1, kv2) -> { + int diff = kv1.getValue() - kv2.getValue(); + if (diff == 0) { + return kv1.getKey().compareTo(kv2.getKey()); + } + return -diff; + }) + .limit(k) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + public static void main(String[] args) throws IOException { + // System.out.println( + // new TopKFrequentWords() + // .topKFrequent(new String[] {"i", "love", "leetcode", "i", "love", "coding"}, 2)); + } +} From dc4f1888915887a5111323c4239511e924f90927 Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Mon, 28 Oct 2024 13:42:51 +0530 Subject: [PATCH 3/9] HARD --- ...SingleSourceShortestPathUsingDijkstra.java | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java diff --git a/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java b/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java deleted file mode 100644 index 14d7dd2..0000000 --- a/data-structures/src/main/java/com/thealgorithm/graph/SingleSourceShortestPathUsingDijkstra.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.thealgorithm.graph; - -import java.util.Comparator; -import java.util.List; -import java.util.PriorityQueue; -import java.util.Set; - -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; - -@Getter -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -@AllArgsConstructor -class HeapEntry { - @EqualsAndHashCode.Include int node; - int distanceFromSource; -} - -class MaxHeap { - - public HeapEntry extractMin() { - return null; - } - - public void update(int node, int newVal) {} -} - -/** - * @author: Subham Santra - */ -public class SingleSourceShortestPathUsingDijkstra { - - private List findShortestPath(int[][] graph, int source) { - PriorityQueue priorityQueue = - new PriorityQueue<>(Comparator.comparingInt(HeapEntry::getDistanceFromSource)); - - // Set<> - - for (int i = 0; i < graph.length; ++i) { - if (i == source) { - priorityQueue.offer(new HeapEntry(i, 0)); - } else { - priorityQueue.offer(new HeapEntry(i, Integer.MAX_VALUE)); - } - } - } - - public static void main(String[] args) { - int[][] graph = new int[9][9]; - } -} From b7fc15cb4eb6602fc09c93e8e07ce5ff695ffd4a Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Mon, 28 Oct 2024 13:46:08 +0530 Subject: [PATCH 4/9] HARD --- .idea/workspace.xml | 62 ++++++++++++------- .../thealgorithm/graph/DijkstraAlgorithm.java | 4 +- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9fe844b..d86df9c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,11 +4,7 @@ - - - - - + - { - "keyToString": { - "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true", - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "SHARE_PROJECT_CONFIGURATION_FILES": "true", - "git-widget-placeholder": "back__tracking", - "jdk.selected.JAVA_MODULE": "openjdk-21 (3)", - "last_opened_file_path": "/Users/subhamsantra/Projects/Interview/TheAlgorithm/data-structures/src/main/resources", - "onboarding.tips.debug.path": "/Users/subhamsantra/Projects/Interview/TheAlgorithm/low-level-design/src/main/java/com/subham/tbpp/Main.java", - "settings.editor.selected.configurable": "preferences.lookFeel" + +}]]> @@ -96,20 +92,24 @@ + + + + - - - diff --git a/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java index e1e4f9d..686dfea 100644 --- a/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java +++ b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java @@ -69,8 +69,10 @@ public static void main(String[] args) throws InvalidWeightException { .addEdge(2, 3, 7) .addEdge(5, 3, 14) .addEdge(5, 4, 10) - .addEdge(3, 4, -9); + .addEdge(3, 4, 9); new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 0); + new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 8); + new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 6); } } From 274731a25c0af7f64815df99b06655e1f4f914d8 Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Wed, 30 Oct 2024 13:10:48 +0530 Subject: [PATCH 5/9] HARD --- .idea/workspace.xml | 77 +++++++++++-------- .../ConnectedComponentOnUndirectedGraph.java | 66 ++++++++++++++++ .../thealgorithm/graph/DijkstraAlgorithm.java | 6 +- .../com/thealgorithm/graph/GraphCreator.java | 44 +++++++++-- .../linkedlist/DoublyLinkedList.java | 21 +++++ .../linkedlist/DoublyLinkedListNode.java | 13 ++++ .../linkedlist/LinkedListNode.java | 12 +++ 7 files changed, 200 insertions(+), 39 deletions(-) create mode 100644 data-structures/src/main/java/com/thealgorithm/graph/ConnectedComponentOnUndirectedGraph.java create mode 100644 data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedList.java create mode 100644 data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedListNode.java create mode 100644 data-structures/src/main/java/com/thealgorithm/linkedlist/LinkedListNode.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d86df9c..863392a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,7 +4,15 @@ - + + + + + + + + + + - { + "keyToString": { + "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "SHARE_PROJECT_CONFIGURATION_FILES": "true", + "git-widget-placeholder": "back__tracking", + "jdk.selected.JAVA_MODULE": "openjdk-21 (3)", + "last_opened_file_path": "/Users/subhamsantra/Projects/Interview/TheAlgorithm/data-structures/src/main/java/com/thealgorithm", + "onboarding.tips.debug.path": "/Users/subhamsantra/Projects/Interview/TheAlgorithm/low-level-design/src/main/java/com/subham/tbpp/Main.java", + "settings.editor.selected.configurable": "preferences.lookFeel" } -}]]> +} @@ -103,9 +117,9 @@ - - - @@ -757,11 +779,6 @@ - - - - - diff --git a/data-structures/src/main/java/com/thealgorithm/graph/ConnectedComponentOnUndirectedGraph.java b/data-structures/src/main/java/com/thealgorithm/graph/ConnectedComponentOnUndirectedGraph.java new file mode 100644 index 0000000..ee3a568 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/graph/ConnectedComponentOnUndirectedGraph.java @@ -0,0 +1,66 @@ +package com.thealgorithm.graph; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * @author: Subham Santra + */ +public class ConnectedComponentOnUndirectedGraph { + public static void main(String[] args) { + // TEST 1 + { + GraphCreator graphCreator = + GraphCreator.getUndirected(7) + .addEdge(1, 2, 1) + .addEdge(2, 3, 1) + .addEdge(3, 4, 1) + .addEdge(5, 6, 1); + + System.out.println( + countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices())); + } + + // TEST 2 + { + GraphCreator graphCreator = + GraphCreator.getUndirected(7); + System.out.println( + countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices())); + } + + // TEST 3 + { + GraphCreator graphCreator = + GraphCreator.getUndirected(3) + .addEdge(1, 2) + .addEdge(2, 0) + .addEdge(1, 0); + System.out.println( + countOfConnectedComponents(graphCreator.getAdjacencyList(), graphCreator.getVertices())); + } + } + + public static int countOfConnectedComponents( + Map> adjacencyList, Set> vertexSet) { + int count = 0; + Set visited = new HashSet<>(); + for (Vertex vertex : vertexSet) { + if (!visited.contains(vertex.getKey())) { + count++; + DFS(adjacencyList, vertex.getKey(), visited); + } + } + return count; + } + + private static void DFS( + Map> adjacencyList, int vertex, Set visited) { + if (visited.contains(vertex)) return; + visited.add(vertex); + for (int child : adjacencyList.get(vertex)) { + DFS(adjacencyList, child, visited); + } + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java index 686dfea..9b7bcdd 100644 --- a/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java +++ b/data-structures/src/main/java/com/thealgorithm/graph/DijkstraAlgorithm.java @@ -71,8 +71,8 @@ public static void main(String[] args) throws InvalidWeightException { .addEdge(5, 4, 10) .addEdge(3, 4, 9); - new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 0); - new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 8); - new DijkstraAlgorithm().dijkstra(graphCreator.getGraph(), graphCreator.getSize(), 6); + new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 0); + new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 8); + new DijkstraAlgorithm().dijkstra(graphCreator.getAdjacencyMatrix(), graphCreator.getSize(), 6); } } diff --git a/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java b/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java index fc0f279..240531a 100644 --- a/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java +++ b/data-structures/src/main/java/com/thealgorithm/graph/GraphCreator.java @@ -1,17 +1,33 @@ package com.thealgorithm.graph; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; import lombok.Value; @Value public class GraphCreator { - final int size; - final int[][] graph; - final boolean undirected; + int size; + int[][] adjacencyMatrix; + Map> adjacencyList; + Set> vertices; + Set> edgeSet; + boolean undirected; private GraphCreator(int size, boolean undirected) { this.size = size; - this.graph = new int[size][size]; + this.adjacencyMatrix = new int[size][size]; this.undirected = undirected; + this.adjacencyList = new HashMap<>(); + this.vertices = new HashSet<>(); + this.edgeSet = new HashSet<>(); + for (int i = 0; i < size; ++i) { + vertices.add(Vertex.create(i)); + } + for (int i = 0; i < size; ++i) { + adjacencyList.put(i, new HashSet<>()); + } } public static GraphCreator getUndirected(int n) { @@ -22,9 +38,25 @@ public static GraphCreator getDirected(int n) { return new GraphCreator(n, false); } + public GraphCreator addEdge(int u, int v) { + return addEdge(u, v, 1); + } + public GraphCreator addEdge(int u, int v, int w) { - graph[u][v] = w; - if (undirected) graph[v][u] = w; + addEdge0(u, v, w); + if (undirected) { + addEdge0(v, u, w); + } return this; } + + private void addEdge0(int u, int v, int w) { + edgeSet.add(Edge.createEdge(u, v, w)); + addToAdjacencyList(u, v); + adjacencyMatrix[u][v] = w; + } + + private void addToAdjacencyList(int u, int v) { + adjacencyList.get(u).add(v); + } } diff --git a/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedList.java b/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedList.java new file mode 100644 index 0000000..4df8251 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedList.java @@ -0,0 +1,21 @@ +package com.thealgorithm.linkedlist; + +/** + * @author: Subham Santra + */ +public class DoublyLinkedList { + DoublyLinkedListNode head; + DoublyLinkedListNode tail; + + public void addLast(int data) { + if (isEmpty()) { + head = tail = new DoublyLinkedListNode(data, null, null); + } else { + tail.next = new DoublyLinkedListNode(data, null, null); + } + } + + private boolean isEmpty() { + return head == null && tail == null; + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedListNode.java b/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedListNode.java new file mode 100644 index 0000000..5fe0ca2 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/linkedlist/DoublyLinkedListNode.java @@ -0,0 +1,13 @@ +package com.thealgorithm.linkedlist; + +/** + * @author: Subham Santra + */ +public class DoublyLinkedListNode extends LinkedListNode { + DoublyLinkedListNode previous; + + public DoublyLinkedListNode(int data, LinkedListNode next, DoublyLinkedListNode previous) { + super(data, next); + this.previous = previous; + } +} diff --git a/data-structures/src/main/java/com/thealgorithm/linkedlist/LinkedListNode.java b/data-structures/src/main/java/com/thealgorithm/linkedlist/LinkedListNode.java new file mode 100644 index 0000000..c85f972 --- /dev/null +++ b/data-structures/src/main/java/com/thealgorithm/linkedlist/LinkedListNode.java @@ -0,0 +1,12 @@ +package com.thealgorithm.linkedlist; + +import lombok.AllArgsConstructor; + +/** + * @author: Subham Santra + */ +@AllArgsConstructor +public class LinkedListNode { + int data; + LinkedListNode next; +} From c0e76f9a6a7608355800158c688c0f22464348c0 Mon Sep 17 00:00:00 2001 From: Subham1999 Date: Sat, 2 Nov 2024 14:26:19 +0530 Subject: [PATCH 6/9] HARD --- .githooks/pre-commit | 10 +++ .idea/workspace.xml | 52 ++++++++++--- .../filesystem/CreateDirectory.java | 16 ++++ .../thealgorithm/filesystem/CreateFile.java | 16 ++++ .../thealgorithm/filesystem/Directory.java | 6 ++ .../thealgorithm/filesystem/FileCommand.java | 10 +++ .../com/thealgorithm/filesystem/FileNode.java | 29 +++++++ .../thealgorithm/filesystem/FileSystem.java | 9 +++ .../com/thealgorithm/filesystem/MyUnix.java | 22 ++++++ .../filesystem/NotSupportedException.java | 10 +++ .../filesystem/SimpleAbstractFileNode.java | 78 +++++++++++++++++++ .../filesystem/SimpleDirectory.java | 41 ++++++++++ .../thealgorithm/filesystem/SimpleFile.java | 33 ++++++++ .../thealgorithm/parkinglot/FourWheeler.java | 29 +++++++ .../com/thealgorithm/parkinglot/Gate.java | 20 +++++ .../thealgorithm/parkinglot/ParkingFloor.java | 42 ++++++++++ .../parkinglot/ParkingLotSystem.java | 60 ++++++++++++++ .../parkinglot/ParkingResponse.java | 18 +++++ .../thealgorithm/parkinglot/ParkingSpot.java | 22 ++++++ .../parkinglot/PaymentRequest.java | 12 +++ .../parkinglot/PaymentResponse.java | 13 ++++ .../parkinglot/PaymentService.java | 29 +++++++ .../parkinglot/PaymentStatus.java | 5 ++ .../com/thealgorithm/parkinglot/Test.java | 30 +++++++ .../thealgorithm/parkinglot/TwoWheeler.java | 24 ++++++ .../com/thealgorithm/parkinglot/Vehicle.java | 15 ++++ .../parkinglot/model/Attendant.java | 54 +++++++++++++ .../parkinglot/model/EntryGate.java | 21 +++++ .../parkinglot/model/ExitGate.java | 21 +++++ .../thealgorithm/parkinglot/requirement.md | 23 ++++++ scripts/build_test.sh | 6 ++ 31 files changed, 767 insertions(+), 9 deletions(-) create mode 100644 .githooks/pre-commit create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/CreateDirectory.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/CreateFile.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/Directory.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/FileCommand.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/FileNode.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/FileSystem.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/MyUnix.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/NotSupportedException.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/SimpleAbstractFileNode.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/SimpleDirectory.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/filesystem/SimpleFile.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/FourWheeler.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/Gate.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/ParkingFloor.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/ParkingLotSystem.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/ParkingResponse.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/ParkingSpot.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/PaymentRequest.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/PaymentResponse.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/PaymentService.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/PaymentStatus.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/Test.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/TwoWheeler.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/Vehicle.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/model/Attendant.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/model/EntryGate.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/model/ExitGate.java create mode 100644 low-level-design/src/main/java/com/thealgorithm/parkinglot/requirement.md create mode 100755 scripts/build_test.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 0000000..93d8007 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,10 @@ +#!/bin/sh + +# Run 'mvn clean install' and check if it succeeds +echo "Running 'mvn clean install'..." +if mvn clean install; then + echo "Build successful, proceeding with commit." +else + echo "Build failed. Aborting commit." + exit 1 +fi \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 863392a..0c5f7f0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,13 +5,37 @@ - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - -