-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80b62c3
commit 01e6e13
Showing
4 changed files
with
165 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters