Skip to content

Commit

Permalink
ll leetcode pod
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhdeokar committed Sep 10, 2024
1 parent cf29a57 commit 13ff91b
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
*.class
/out/
*.iml
./out/**
./.idea/**
/.vscode/**
62 changes: 62 additions & 0 deletions topics/topics/array/problems/SpiralMatrix.java
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);


}

}
30 changes: 13 additions & 17 deletions topics/topics/dp/problems/MCMRecursive.java
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));

}
}
30 changes: 27 additions & 3 deletions topics/topics/linkedlist/LLNode.java
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;
}
}

}
79 changes: 79 additions & 0 deletions topics/topics/linkedlist/problems/GroupNodes.java
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();
}
}

}
38 changes: 38 additions & 0 deletions topics/topics/linkedlist/problems/InsertGcd.java
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();

}

}
5 changes: 2 additions & 3 deletions topics/topics/trees/tree/LinkedListInBinaryTree.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package trees.tree;

import linkedlist.LLNode;
import trees.Traversal;
import trees.TreeNode;

//leetcode POD 7 sep.
Expand All @@ -12,7 +11,7 @@ public boolean isSubPath(LLNode ll, TreeNode tree) {
if (tree == null) {
return false;
}
if (tree.data == ll.data) {
if (tree.data == ll.val) {
return doesMatch(ll.next, tree.left) || doesMatch(ll.next, tree.right) || isSubPath(ll, tree.left) || isSubPath(ll, tree.right);
} else {
return isSubPath(ll, tree.left) || isSubPath(ll, tree.right);
Expand All @@ -29,7 +28,7 @@ public boolean doesMatch(LLNode ll, TreeNode tree) {
return false;
}

if (ll.data != tree.data) {
if (ll.val != tree.data) {
return false;
}

Expand Down

0 comments on commit 13ff91b

Please sign in to comment.