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; + } diff --git a/JavaLeetcode/README.md b/JavaLeetcode/README.md new file mode 100644 index 00000000..e3277d82 --- /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. 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. 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; +} 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); + } + } 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]; + }