Skip to content

Commit

Permalink
Oct 19 week
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhdeokar committed Oct 19, 2024
1 parent 80b62c3 commit 01e6e13
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 22 deletions.
98 changes: 98 additions & 0 deletions topics/topics/bitwisemath/LinkedListMerge.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
38 changes: 38 additions & 0 deletions topics/topics/bitwisemath/MaximumNumberInOneSwap.java
Original file line number Diff line number Diff line change
@@ -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<Integer> numsInIncOrder = maximumNumberInOneSwap.calculate(num);

int length = maximumNumberInOneSwap.lengthOfNumber(num);
int power = (int) Math.ceil(Math.log10(num + 1));
StringBuilder result = new StringBuilder();

}

private ArrayDeque<Integer> calculate(int num) {
StringBuilder str = new StringBuilder(String.valueOf(num));
ArrayDeque<Integer> 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));

}

}
46 changes: 24 additions & 22 deletions topics/topics/graph/Cycle.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
package graph;

import java.util.ArrayDeque;
import java.util.ArrayList;

public class Cycle {

public void traverseBfs(ArrayList<ArrayList<Integer>> list, int nodes){
public void dfs(ArrayList<ArrayList<Integer>> list, int nodes) {
boolean[] visited = new boolean[nodes];
for(int i=0;i<nodes;i++){
if(!visited[i]){
traverseBfs(list, i, visited);
for (int i = 0; i < nodes; i++) {
boolean[] recStack = new boolean[nodes];
if (!visited[i]) {
System.out.println("cycle exists:" + dfs(list, i, visited, recStack));
}
}
}

private void traverseBfs(ArrayList<ArrayList<Integer>> list, int curr, boolean[] visited) {
private boolean dfs(ArrayList<ArrayList<Integer>> list, int curr, boolean[] visited, boolean[] recstack) {


ArrayDeque<Integer> 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<ArrayList<Integer>> graph = g.sampleCycleGraph();
ArrayList<ArrayList<Integer>> directedGraph = g.sampleDirectedGraph();
Cycle cycle = new Cycle();
cycle.traverseBfs(graph, 5);
cycle.dfs(directedGraph, 4);
}
}
5 changes: 5 additions & 0 deletions topics/topics/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public ArrayList<ArrayList<Integer>> sampleCycleGraph() {
return createGraph(graph, 4, false);
}

public ArrayList<ArrayList<Integer>> sampleDirectedGraph() {
int[][] graph = new int[][]{{0, 1}, {0, 2}, {1, 2}, {2, 3}, {2, 0}};
return createGraph(graph, 4, false);
}


private ArrayList<ArrayList<Integer>> createGraph(int[][] graph, int nodes, boolean bidirectional) {
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
Expand Down

0 comments on commit 01e6e13

Please sign in to comment.