From 01e6e13cd4abfbdcef213698943d87d7284c43c0 Mon Sep 17 00:00:00 2001 From: kaustubhdeokar Date: Sat, 19 Oct 2024 19:03:54 +0530 Subject: [PATCH] Oct 19 week --- .../topics/bitwisemath/LinkedListMerge.java | 98 +++++++++++++++++++ .../bitwisemath/MaximumNumberInOneSwap.java | 38 +++++++ topics/topics/graph/Cycle.java | 46 ++++----- topics/topics/graph/Graph.java | 5 + 4 files changed, 165 insertions(+), 22 deletions(-) create mode 100644 topics/topics/bitwisemath/LinkedListMerge.java create mode 100644 topics/topics/bitwisemath/MaximumNumberInOneSwap.java diff --git a/topics/topics/bitwisemath/LinkedListMerge.java b/topics/topics/bitwisemath/LinkedListMerge.java new file mode 100644 index 0000000..e484342 --- /dev/null +++ b/topics/topics/bitwisemath/LinkedListMerge.java @@ -0,0 +1,98 @@ +package bitwisemath; + +public class LinkedListMerge { + + + class LinkedList { + int data; + LinkedList next; + + LinkedList(int data) { + this.data = data; + } + + public LinkedList createLinkedList(Integer... args) { + + LinkedList list = new LinkedList(-1); + LinkedList temp = list; + for (int i : args) { + temp.next = new LinkedList(i); + temp = temp.next; + } + return list.next; + } + + + public LinkedList merge(LinkedList[] lists) { //3 //2 //0 + + //result + LinkedList result = new LinkedList(-1); + LinkedList temp = result; + + int endReached = 0; // number of lists done traversing. + + while (endReached < lists.length) { + + int smallest = Integer.MAX_VALUE; + int index = -1; + + for (int i = 0; i < lists.length; i++) { + + if (lists[i] != null && lists[i].data < smallest) { + smallest = lists[i].data; + index = i; + } + } + + temp.next = new LinkedList(lists[index].data); + temp = temp.next; + lists[index] = lists[index].next; + if (lists[index] == null) { + endReached += 1; + + } + + } + + return result.next; + + } + + public void printLinkedList(LinkedList list){ + while(list!=null){ + System.out.print(list.data+" "); + list = list.next; + } + System.out.println(); + } + + } + + + public static void main(String[] args) { + + LinkedListMerge llm = new LinkedListMerge(); + llm.perform(); + + + + + } + + private void perform() { + LinkedList master = new LinkedList(-1); + LinkedList list1 = master.createLinkedList(1,2,3,4); + LinkedList list2 = master.createLinkedList(2,4,6,8); + LinkedList list3 = master.createLinkedList(0,9,10,11); + master.printLinkedList(list1); + master.printLinkedList(list2); + master.printLinkedList(list3); + + System.out.println("merging"); + + LinkedList[] lists = new LinkedList[]{list1, list2, list3}; + LinkedList result = master.merge(lists); + master.printLinkedList(result); + } + +} diff --git a/topics/topics/bitwisemath/MaximumNumberInOneSwap.java b/topics/topics/bitwisemath/MaximumNumberInOneSwap.java new file mode 100644 index 0000000..3a191bd --- /dev/null +++ b/topics/topics/bitwisemath/MaximumNumberInOneSwap.java @@ -0,0 +1,38 @@ +package bitwisemath; + +import java.util.ArrayDeque; + +public class MaximumNumberInOneSwap { + + public static void main(String[] args) { + + int num = 2736592; + MaximumNumberInOneSwap maximumNumberInOneSwap = new MaximumNumberInOneSwap(); + ArrayDeque numsInIncOrder = maximumNumberInOneSwap.calculate(num); + + int length = maximumNumberInOneSwap.lengthOfNumber(num); + int power = (int) Math.ceil(Math.log10(num + 1)); + StringBuilder result = new StringBuilder(); + + } + + private ArrayDeque calculate(int num) { + StringBuilder str = new StringBuilder(String.valueOf(num)); + ArrayDeque q = new ArrayDeque<>(); + for (int i = 0; i < str.length(); i++) { + Integer curr = Integer.parseInt(String.valueOf(str.charAt(i))); + while (!q.isEmpty() && q.peekLast() < curr) { + q.removeLast(); + } + q.add(curr); + } + return q; + } + + private int lengthOfNumber(int num) { + + return (int) Math.ceil(Math.log10(num + 1)); + + } + +} diff --git a/topics/topics/graph/Cycle.java b/topics/topics/graph/Cycle.java index 6a757b1..c653cab 100644 --- a/topics/topics/graph/Cycle.java +++ b/topics/topics/graph/Cycle.java @@ -1,47 +1,49 @@ package graph; -import java.util.ArrayDeque; import java.util.ArrayList; public class Cycle { - public void traverseBfs(ArrayList> list, int nodes){ + public void dfs(ArrayList> list, int nodes) { boolean[] visited = new boolean[nodes]; - for(int i=0;i> list, int curr, boolean[] visited) { + private boolean dfs(ArrayList> list, int curr, boolean[] visited, boolean[] recstack) { + - ArrayDeque queue = new ArrayDeque<>(); - queue.add(curr); visited[curr] = true; + recstack[curr] = true; + System.out.println(curr); - while(!queue.isEmpty()) - { - curr = queue.remove(); - System.out.println(curr); - for(int neigh: list.get(curr)) - { - if(!visited[neigh]){ - visited[neigh] = true; - queue.add(neigh); - } - else{ - System.out.println("cycle."); + for (int neigh : list.get(curr)) { + System.out.println("neigh: " + neigh + " curr:" + curr); + + if (!visited[neigh]) { + visited[neigh] = true; + if (dfs(list, neigh, visited, recstack)) { + return true; } } + if (recstack[neigh]) { + return true; + } } + + recstack[curr] = false; + return false; } public static void main(String[] args) { Graph g = new Graph(); - ArrayList> graph = g.sampleCycleGraph(); + ArrayList> directedGraph = g.sampleDirectedGraph(); Cycle cycle = new Cycle(); - cycle.traverseBfs(graph, 5); + cycle.dfs(directedGraph, 4); } } diff --git a/topics/topics/graph/Graph.java b/topics/topics/graph/Graph.java index 408588e..e370003 100644 --- a/topics/topics/graph/Graph.java +++ b/topics/topics/graph/Graph.java @@ -21,6 +21,11 @@ public ArrayList> sampleCycleGraph() { return createGraph(graph, 4, false); } + public ArrayList> sampleDirectedGraph() { + int[][] graph = new int[][]{{0, 1}, {0, 2}, {1, 2}, {2, 3}, {2, 0}}; + return createGraph(graph, 4, false); + } + private ArrayList> createGraph(int[][] graph, int nodes, boolean bidirectional) { ArrayList> list = new ArrayList<>();