-
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
cf29a57
commit 13ff91b
Showing
7 changed files
with
224 additions
and
23 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 |
---|---|---|
|
@@ -3,3 +3,6 @@ | |
*.class | ||
/out/ | ||
*.iml | ||
./out/** | ||
./.idea/** | ||
/.vscode/** |
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,62 @@ | ||
package array.problems; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SpiralMatrix { | ||
|
||
public List<Integer> spiralOrder(int[][] matrix) { | ||
|
||
|
||
// row rowstart : c(colstart) to c(colend) -> rowstart+=1 | ||
// col (colend): row (rowstart:rowend) -> colend-=1 | ||
// row(rowend): col (colend:colstart): rowend-=1 | ||
// col colstart: row(rowend:rowstart): colstart+=1 | ||
|
||
int rowstart = 0; | ||
int rowend = matrix.length - 1; | ||
int colstart = 0; | ||
int colend = matrix[0].length - 1; | ||
List<Integer> answer = new ArrayList<>(); | ||
while (rowstart <= rowend && colstart <= colend) { | ||
|
||
for (int col = colstart; col <= colend; col++) { | ||
answer.add(matrix[rowstart][col]); | ||
} | ||
rowstart += 1; | ||
|
||
for (int row = rowstart; row <= rowend; row++) { | ||
answer.add(matrix[row][colend]); | ||
} | ||
colend -= 1; | ||
|
||
if (rowstart > rowend || colstart > colend) { | ||
break; | ||
} | ||
|
||
for (int col = colend; col >= colstart; col--) { | ||
answer.add(matrix[rowend][col]); | ||
} | ||
rowend -= 1; | ||
|
||
for (int row = rowend; row >= rowstart; row--) { | ||
answer.add(matrix[row][colstart]); | ||
} | ||
colstart += 1; | ||
|
||
} | ||
|
||
return answer; | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | ||
SpiralMatrix spiralMatrix = new SpiralMatrix(); | ||
List<Integer> spiralOrderList = spiralMatrix.spiralOrder(matrix); | ||
System.out.println(spiralOrderList); | ||
|
||
|
||
} | ||
|
||
} |
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,41 +1,37 @@ | ||
package dp.problems; | ||
|
||
public class MCMRecursive{ | ||
public int mcm(int[] arr, int i, int j){ | ||
if(i>=j){ | ||
public class MCMRecursive { | ||
|
||
public int mcm(int[] arr, int i, int j) { | ||
|
||
if (i >= j) { | ||
return 0; | ||
} | ||
|
||
|
||
|
||
int min = Integer.MAX_VALUE; | ||
|
||
for(int k=i;k<j;k++){ | ||
int cost = mcm(arr, i, k) + mcm(arr, k+1, j) + arr[i-1]*arr[k]*arr[j]; | ||
if(cost < min){ | ||
for (int k = i; k < j; k++) { | ||
int cost = mcm(arr, i, k) + mcm(arr, k + 1, j) + arr[i - 1] * arr[k] * arr[j]; | ||
if (cost < min) { | ||
min = Integer.min(min, cost); | ||
} | ||
} | ||
|
||
return min; | ||
|
||
} | ||
|
||
|
||
public static void main(String[] args){ | ||
public static void main(String[] args) { | ||
|
||
int[] arr = {1, 2, 3, 4, 3}; | ||
int[] arr = { 1, 2, 3, 4, 3 }; | ||
|
||
int i = arr.length; | ||
int j = arr.length; | ||
|
||
int[][] dp = new int[i+1][j+1]; | ||
|
||
int[][] dp = new int[i + 1][j + 1]; | ||
|
||
MCMRecursive mcm = new MCMRecursive(); | ||
System.out.println(mcm.mcm(arr, 1, arr.length-1)); | ||
System.out.println(mcm.mcm(arr, 1, arr.length - 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,10 +1,34 @@ | ||
package linkedlist; | ||
|
||
public class LLNode { | ||
public int data; | ||
public int val; | ||
public LLNode next; | ||
|
||
public LLNode(int data) { | ||
this.data = data; | ||
public LLNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
public LLNode() { | ||
|
||
} | ||
|
||
public LLNode createLLNodeList(int[] arr) { | ||
if (arr.length == 0) return null; | ||
LLNode head = new LLNode(-1); | ||
LLNode temp = head; | ||
for (int i : arr) { | ||
temp.next = new LLNode(i); | ||
temp = temp.next; | ||
} | ||
return head.next; | ||
} | ||
|
||
public void print() { | ||
LLNode llNode = this; | ||
while (llNode != null) { | ||
System.out.print(llNode.val + " "); | ||
llNode = llNode.next; | ||
} | ||
} | ||
|
||
} |
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,79 @@ | ||
package linkedlist.problems; | ||
|
||
import linkedlist.LLNode; | ||
//725. Split Linked List in Parts | ||
//daily problem sep 8. | ||
public class GroupNodes { | ||
|
||
public LLNode[] splitListToParts(LLNode head, int k) { | ||
|
||
LLNode temp = head; | ||
int count = 0; | ||
while(temp!=null){ | ||
count+=1; | ||
temp = temp.next; | ||
} | ||
|
||
int nodesPerGroup = count/k; | ||
int remainder = count%k; | ||
LLNode[] array = new LLNode[k]; | ||
|
||
for(int group = 0; group < k; group ++) | ||
{ | ||
LLNode groupHead = new LLNode(0); | ||
temp = groupHead; | ||
|
||
for(int node = 0; node< nodesPerGroup; node++) | ||
{ | ||
if(head==null){ | ||
continue; | ||
} | ||
temp.next = new LLNode(head.val); | ||
temp = temp.next; | ||
head = head.next; | ||
} | ||
|
||
if(remainder>0){ | ||
if(head==null){ | ||
continue; | ||
} | ||
temp.next = new LLNode(head.val); | ||
temp = temp.next; | ||
head = head.next; | ||
remainder-=1; | ||
} | ||
|
||
array[group] = groupHead.next; | ||
} | ||
|
||
return array; | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
LLNode llNode = new LLNode(); | ||
LLNode list = llNode.createLLNodeList(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); | ||
|
||
GroupNodes groupNodes = new GroupNodes(); | ||
LLNode[] llNodes = groupNodes.splitListToParts(list, 3); | ||
|
||
groupNodes.print(llNodes); | ||
|
||
list = llNode.createLLNodeList(new int[]{1, 2, 3}); | ||
llNodes = groupNodes.splitListToParts(list, 5); | ||
groupNodes.print(llNodes); | ||
} | ||
|
||
private void print(LLNode[] llNodes) { | ||
for(LLNode llNode: llNodes){ | ||
LLNode temp = llNode; | ||
while(temp!=null){ | ||
System.out.print(temp.val +" "); | ||
temp = temp.next; | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
} |
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 linkedlist.problems; | ||
|
||
import linkedlist.LLNode; | ||
//leetcode pod sep 10. | ||
public class InsertGcd { | ||
|
||
|
||
public LLNode insertGreatestCommonDivisors(LLNode head) { | ||
|
||
LLNode temp = head; | ||
while(temp.next!=null){ | ||
int first = temp.val; | ||
int second = temp.next.val; | ||
int gcd = gcd(first, second); | ||
LLNode newNode = new LLNode(gcd); | ||
newNode.next = temp.next; | ||
temp.next = newNode; | ||
temp = newNode.next; | ||
} | ||
return head; | ||
|
||
} | ||
|
||
public int gcd(int a, int b) { | ||
if (b == 0) return a; | ||
return gcd(b, a % b); | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
InsertGcd insertGcd = new InsertGcd(); | ||
LLNode llNodeList = new LLNode().createLLNodeList(new int[]{18, 6, 10, 3}); | ||
LLNode llNode = insertGcd.insertGreatestCommonDivisors(llNodeList); | ||
llNode.print(); | ||
|
||
} | ||
|
||
} |
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