From eb65f6a1b5096a0125c258c7eb5c1bd224da958b Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:50:47 +0530 Subject: [PATCH 01/10] Create Sliding Window Maximum.java --- JavaLeetcode/Sliding Window Maximum.java | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 JavaLeetcode/Sliding Window Maximum.java diff --git a/JavaLeetcode/Sliding Window Maximum.java b/JavaLeetcode/Sliding Window Maximum.java new file mode 100644 index 00000000..ba8e30fe --- /dev/null +++ b/JavaLeetcode/Sliding Window Maximum.java @@ -0,0 +1,29 @@ +public static int[] maxSlidingWindow(List nums, int k) +{ + int ans[]=new int[nums.size()-k+1]; + int n=nums.size(); + + Deque dq=new LinkedList<>(); + + int m=0; + + for(int i=0;i=k) + { + if(!dq.isEmpty()&&dq.peekFirst()==i-k) + dq.pollFirst(); + } + + while(!dq.isEmpty()&& nums.get(i) > nums.get(dq.peekLast())) + dq.pollLast(); + + dq.addLast(i); + + if(i>=k-1) + ans[m++]=nums.get(dq.peekFirst()); + } + + + return ans; +} From a0d944845ea085d57b46bad2a973d79cf0de8f01 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:52:40 +0530 Subject: [PATCH 02/10] Create Minimum Time to Collect All Apples in a Tree.java --- ... Time to Collect All Apples in a Tree.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 JavaLeetcode/Minimum Time to Collect All Apples in a Tree.java diff --git a/JavaLeetcode/Minimum Time to Collect All Apples in a Tree.java b/JavaLeetcode/Minimum Time to Collect All Apples in a Tree.java new file mode 100644 index 00000000..a5dc9c38 --- /dev/null +++ b/JavaLeetcode/Minimum Time to Collect All Apples in a Tree.java @@ -0,0 +1,60 @@ +public int minTime(int n, int[][] edges, List hasApple) { + Map> tree = new HashMap<>(); + + for(int[] e: edges) { + if(!tree.containsKey(e[0])) { + tree.put(e[0], new HashSet<>()); + } + tree.get(e[0]).add(e[1]); + } + + int[] ans = dfs(0, hasApple, tree); + + int sum = 0; + + for(int val: ans) { + sum += val; + } + + return sum; + } + + public int[] dfs(int start, ListhasApple, Map> tree) { + if(!tree.containsKey(start)) { + if(hasApple.get(start) == false) { + return new int[]{0}; + } else { + return new int[]{1}; + } + } + + Set set = tree.get(start); + + int len = set.size(), index = 0; + + int[] ans = new int[len]; + + for(int val: set) { + int[] child = dfs(val, hasApple, tree); + int sum = 0; + for(int depth: child) { + sum += depth; + } + if(sum == 0) { + if(hasApple.get(val) == true) { + ans[index++] = 2; + } else { + ans[index++] = 0; + } + } else { + if(tree.containsKey(val)) { + ans[index++] = sum + 2; + } else { + ans[index++] = sum + 1; + } + + } + } + + return ans; + } From 9c8184233f8893fe18bccd2bd661ca25a5c56b40 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:54:28 +0530 Subject: [PATCH 03/10] Create README.md --- JavaLeetcode/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 JavaLeetcode/README.md diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md new file mode 100644 index 00000000..956dbed8 --- /dev/null +++ b/JavaLeetcode/README.md @@ -0,0 +1,3 @@ +#LEETCODE + +The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. From 71563735d741229f33f94d2f3a7d5053675e6d73 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:55:50 +0530 Subject: [PATCH 04/10] Update README.md --- JavaLeetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md index 956dbed8..846fbd49 100644 --- a/JavaLeetcode/README.md +++ b/JavaLeetcode/README.md @@ -1,3 +1,3 @@ -#LEETCODE +### LEETCODE The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. From ba2b8e2defaeb36052530dffea34baf926e739d8 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:56:23 +0530 Subject: [PATCH 05/10] Update README.md --- JavaLeetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md index 846fbd49..ce9a8766 100644 --- a/JavaLeetcode/README.md +++ b/JavaLeetcode/README.md @@ -1,3 +1,3 @@ -### LEETCODE +##### LEETCODE The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. From 6cda1b3f726f07aeece75fa21dbebf3011a730bb Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:56:43 +0530 Subject: [PATCH 06/10] Update README.md --- JavaLeetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md index ce9a8766..935a761c 100644 --- a/JavaLeetcode/README.md +++ b/JavaLeetcode/README.md @@ -1,3 +1,3 @@ -##### LEETCODE +# LEETCODE The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. From aad2def57f2a022ad5d0adce8d107727b4d47609 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:57:38 +0530 Subject: [PATCH 07/10] Update README.md --- JavaLeetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md index 935a761c..05dcc794 100644 --- a/JavaLeetcode/README.md +++ b/JavaLeetcode/README.md @@ -1,3 +1,3 @@ # LEETCODE -The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. +The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. It’s a website where people–mostly software engineers–practice their coding skills. There are 800+ questions (and growing), each with multiple solutions. Questions are ranked by level of difficulty: easy, medium, and hard. From f2f082410e91ca224ca60ac82fad446384c38b08 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 00:58:11 +0530 Subject: [PATCH 08/10] Update README.md --- JavaLeetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md index 05dcc794..e3277d82 100644 --- a/JavaLeetcode/README.md +++ b/JavaLeetcode/README.md @@ -1,3 +1,3 @@ # LEETCODE -The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. It’s a website where people–mostly software engineers–practice their coding skills. There are 800+ questions (and growing), each with multiple solutions. Questions are ranked by level of difficulty: easy, medium, and hard. +The most promising platform in the world to practice coding for any tech companies. It provides standard questions solutions and lots more. It’s a website where people–mostly software engineers–practice their coding skills. There are 1500+ questions (and growing), each with multiple solutions. Questions are ranked by level of difficulty: easy, medium, and hard. From a75b70dbbe99f37fccd94e8a0b2f4f0b9c737ec8 Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 01:00:06 +0530 Subject: [PATCH 09/10] Create Valid Sudoku.java --- JavaLeetcode/Valid Sudoku.java | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 JavaLeetcode/Valid Sudoku.java diff --git a/JavaLeetcode/Valid Sudoku.java b/JavaLeetcode/Valid Sudoku.java new file mode 100644 index 00000000..e3481100 --- /dev/null +++ b/JavaLeetcode/Valid Sudoku.java @@ -0,0 +1,40 @@ +public boolean isValidSudoku(char[][] board) { + return checkValidity(board, 0, 0); + } + + boolean checkValidity(char[][] board, int i, int j){ + if(i == board.length) + return true; + //next row + int ni = 0; + //next column + int nj = 0; + //if end of row move to next row else increment column index in same row. + if(board[0].length - 1 == j){ + ni = i + 1; + nj = 0; + }else{ + ni = i; + nj = j + 1; + } + if(board[i][j] == '.') + return checkValidity(board, ni, nj); + else{ + for(int c = 0; c < board.length; c++){ + if(board[i][j] == board[c][j] && c != i) + return false; + if(board[i][j] == board[i][c] && c != j) + return false; + } + //verify the sub 3*3 matrix for the specific index. + int smi = i/3 * 3; + int smj = j/3 * 3; + for(int mi = 0; mi < 3; mi++){ + for(int mj = 0; mj < 3; mj++){ + if(mi + smi != i && mj + smj != j && board[i][j] == board[mi + smi][mj + smj]) + return false; + } + } + return checkValidity(board, ni, nj); + } + } From 5a515f4a6f05d18ca5b6f9b68726fba0cdd09bdb Mon Sep 17 00:00:00 2001 From: chak_kritip <45520181+Kritip123@users.noreply.github.com> Date: Fri, 1 Oct 2021 01:01:44 +0530 Subject: [PATCH 10/10] Create Wildcard Matching.java --- JavaLeetcode/Wildcard Matching.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 JavaLeetcode/Wildcard Matching.java diff --git a/JavaLeetcode/Wildcard Matching.java b/JavaLeetcode/Wildcard Matching.java new file mode 100644 index 00000000..9a70a30b --- /dev/null +++ b/JavaLeetcode/Wildcard Matching.java @@ -0,0 +1,26 @@ +public boolean isMatch(String s, String p) { + + int rows= s.length(); + int cols = p.length(); + + boolean[][] dp = new boolean[rows+1][cols+1]; + + dp[0][0] = true; + + for(int j = 1; j < cols+1; j++){ + if(p.charAt(j-1) == '*'){ + dp[0][j] = dp[0][j-1]; + } + } + + for(int i = 1; i < rows+1; i++){ + for(int j = 1; j < cols+1; j++){ + if(s.charAt(i-1) == p.charAt(j-1) || p.charAt(j-1) == '?'){ + dp[i][j] = dp[i-1][j-1]; + }else if(p.charAt(j-1) == '*'){ + dp[i][j] = (dp[i-1][j] || dp[i][j-1]); + } + } + } + return dp[rows][cols]; + }