From 23ab9b46630764f8fc2b567d1433ed2f9ac711d0 Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:22:31 +0530 Subject: [PATCH 1/6] Jump game problems --- java-program/JumpGame.java | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 java-program/JumpGame.java diff --git a/java-program/JumpGame.java b/java-program/JumpGame.java new file mode 100644 index 0000000..b1d4cc4 --- /dev/null +++ b/java-program/JumpGame.java @@ -0,0 +1,52 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +import java.util.*; +public class JumpGame{ + + public static void main(String args[]){ + JumpGame s = new JumpGame(); + int arr[] = {2,4,6,3,4,6}; + + if(s.isPosssible(arr)){ + System.out.println(s.minJumps(arr));} + else{ + System.out.println("Not Possible"); + } + } + + + public boolean isPosssible(int[] nums) { + + int n = nums.length; + + //last index from where we can reach to end cell + //As from last index (n - 1) itself reach to end cell, + int lastIndex = n - 1; + + for(int i = n - 1; i >= 0; i--){ + if(i + nums[i] >= lastIndex){ + lastIndex = i; + } + } + + //check from 0 reach to end cell + return lastIndex == 0; + } + + + +public int minJumps(int[] A) { + int jumps = 0, curEnd = 0, curFarthest = 0; + for (int i = 0; i < A.length - 1; i++) { + curFarthest = Math.max(curFarthest, i + A[i]); + if (i == curEnd) { + jumps++; + curEnd = curFarthest; + } + } + return jumps; +} \ No newline at end of file From 764b2d09bf6cfae72541a296fecd03004bbbac57 Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:29:07 +0530 Subject: [PATCH 2/6] Revere nodes in K groups in LinkedList --- java-program/ReverseNodesinKGroup.java | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 java-program/ReverseNodesinKGroup.java diff --git a/java-program/ReverseNodesinKGroup.java b/java-program/ReverseNodesinKGroup.java new file mode 100644 index 0000000..e372b36 --- /dev/null +++ b/java-program/ReverseNodesinKGroup.java @@ -0,0 +1,51 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + + +// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. +// k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. +// You may not alter the values in the list's nodes, only nodes themselves may be changed. + + +public /*class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + }*/ + +//FUNCTION IMPLEMENTATION + public ListNode reverseKGroup(ListNode head, int k) { + int c=k; + ListNode h=head; + ListNode s=head; + ListNode p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + k--; + if(k==0){ + h.next=reverseKGroup(next,c); + return s; + } + s=next; + } + + if(s==null&&k!=0){ + s=p; + p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + s=next; + + }return p; + }return null; + } +} \ No newline at end of file From d8e7db4ba629722c2aefe02b771bcdb21fca24f6 Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:30:41 +0530 Subject: [PATCH 3/6] Revert "Revere nodes in K groups in LinkedList" This reverts commit 764b2d09bf6cfae72541a296fecd03004bbbac57. --- java-program/ReverseNodesinKGroup.java | 51 -------------------------- 1 file changed, 51 deletions(-) delete mode 100644 java-program/ReverseNodesinKGroup.java diff --git a/java-program/ReverseNodesinKGroup.java b/java-program/ReverseNodesinKGroup.java deleted file mode 100644 index e372b36..0000000 --- a/java-program/ReverseNodesinKGroup.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Author: AKSHAY WAKHARE - * Date: 22 October 2021 - */ - - - -// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. -// k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. -// You may not alter the values in the list's nodes, only nodes themselves may be changed. - - -public /*class ListNode { - int val; - ListNode next; - ListNode() {} - ListNode(int val) { this.val = val; } - ListNode(int val, ListNode next) { this.val = val; this.next = next; } - }*/ - -//FUNCTION IMPLEMENTATION - public ListNode reverseKGroup(ListNode head, int k) { - int c=k; - ListNode h=head; - ListNode s=head; - ListNode p=null; - while(s!=null){ - ListNode next=s.next; - s.next=p; - p=s; - k--; - if(k==0){ - h.next=reverseKGroup(next,c); - return s; - } - s=next; - } - - if(s==null&&k!=0){ - s=p; - p=null; - while(s!=null){ - ListNode next=s.next; - s.next=p; - p=s; - s=next; - - }return p; - }return null; - } -} \ No newline at end of file From 4eb32d598803ab7ae2fbb174012a520f8659f3d3 Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:36:42 +0530 Subject: [PATCH 4/6] Revere nodes in K groups in LinkedList --- java-program/ReverseNodesinKGroup.java | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 java-program/ReverseNodesinKGroup.java diff --git a/java-program/ReverseNodesinKGroup.java b/java-program/ReverseNodesinKGroup.java new file mode 100644 index 0000000..e372b36 --- /dev/null +++ b/java-program/ReverseNodesinKGroup.java @@ -0,0 +1,51 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + + +// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. +// k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. +// You may not alter the values in the list's nodes, only nodes themselves may be changed. + + +public /*class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + }*/ + +//FUNCTION IMPLEMENTATION + public ListNode reverseKGroup(ListNode head, int k) { + int c=k; + ListNode h=head; + ListNode s=head; + ListNode p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + k--; + if(k==0){ + h.next=reverseKGroup(next,c); + return s; + } + s=next; + } + + if(s==null&&k!=0){ + s=p; + p=null; + while(s!=null){ + ListNode next=s.next; + s.next=p; + p=s; + s=next; + + }return p; + }return null; + } +} \ No newline at end of file From 87eded7ff1128914736af87753210bfbd36b2fca Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:43:14 +0530 Subject: [PATCH 5/6] Flatten a multilevel LinkedList --- java-program/FlattenLL.java | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 java-program/FlattenLL.java diff --git a/java-program/FlattenLL.java b/java-program/FlattenLL.java new file mode 100644 index 0000000..c46ad1d --- /dev/null +++ b/java-program/FlattenLL.java @@ -0,0 +1,50 @@ +/** + * Author: AKSHAY WAKHARE + * Date: 22 October 2021 + */ + + +// QUESTION +// You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. +// Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list. + + + + +/* +class Node { + public int val; + public Node prev; + public Node next; + public Node child; +}; +*/ + + +//FUNCTION IMPLEMENTATION + public Node flatten(Node head) { + if( head == null) return head; + + Node p = head; + while( p!= null) { + /* CASE 1: if no child, proceed */ + if( p.child == null ) { + p = p.next; + continue; + } + + /* CASE 2: got child, find the tail of the child and link it to p.next */ + Node temp = p.child; + // Find the tail of the child + while( temp.next != null ) + temp = temp.next; + // Connect tail with p.next, if it is not null + temp.next = p.next; + if( p.next != null ) p.next.prev = temp; + // Connect p with p.child, and remove p.child + p.next = p.child; + p.child.prev = p; + p.child = null; + } + return head; + } \ No newline at end of file From db514595a6d0019592aa42f30acdbbb1d8af68f4 Mon Sep 17 00:00:00 2001 From: Mona Lisa Date: Fri, 22 Oct 2021 03:49:51 +0530 Subject: [PATCH 6/6] No of provinces problem added --- java-program/NoOfProvinces.java | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 java-program/NoOfProvinces.java diff --git a/java-program/NoOfProvinces.java b/java-program/NoOfProvinces.java new file mode 100644 index 0000000..466fb84 --- /dev/null +++ b/java-program/NoOfProvinces.java @@ -0,0 +1,75 @@ + + +// QUESTION +// There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c. +// A province is a group of directly or indirectly connected cities and no other cities outside of the group. +// You are given an n x n matrix isConnected where isConnected[i][j] = 1 if the ith city and the jth city are directly connected, and isConnected[i][j] = 0 otherwise. +// Return the total number of provinces. + + + +//FUNCTION IMPLEMENTATION + +class Solution { + public int findCircleNum(int[][] M) { //<-entry + + int[] unionFindSet = new int[M.length]; + for (int i = 0; i< unionFindSet.length; i++){ + unionFindSet[i] = -1; + } + + for (int i = 0; i < M.length; i++) { + for (int j = i + 1; j < M.length; j++) { + if (M[i][j] == 1) { + union(unionFindSet, i, j); + } + } + } + + int count = 0; + for (int i = 0; i< unionFindSet.length; i++){ + if (unionFindSet[i] < 0) count++; + } + return count; + + } + + + public static int findRoot(int[] unionSet, int v) { + int index = v; + int value = unionSet[v]; + while (value > 0) { + index = value; + value = unionSet[value]; + } + + return index; + } + + public static void union(int[] unionSet, int first, int second) { + int x = findRoot(unionSet, first); + int y = findRoot(unionSet, second); + + if(x == y) { + return; + } + if (Math.abs(unionSet[x]) <= Math.abs(unionSet[y])) { + unionSet[y] -= Math.abs(unionSet[x]); + unionSet[x] = y; + while ( first != y) { + int parent = unionSet[first]; + unionSet[first] = y; + first = parent; + } + } else { + unionSet[x] -= Math.abs(unionSet[y]); + unionSet[y] = x; + while ( second != x) { + int parent = unionSet[second]; + unionSet[second] = x; + second = parent; + } + } + } + +} \ No newline at end of file