Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Leetcode problems #583

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
60 changes: 60 additions & 0 deletions JavaLeetcode/Minimum Time to Collect All Apples in a Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
Map<Integer, Set<Integer>> 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, List<Boolean>hasApple, Map<Integer, Set<Integer>> tree) {
if(!tree.containsKey(start)) {
if(hasApple.get(start) == false) {
return new int[]{0};
} else {
return new int[]{1};
}
}

Set<Integer> 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;
}
3 changes: 3 additions & 0 deletions JavaLeetcode/README.md
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions JavaLeetcode/Sliding Window Maximum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public static int[] maxSlidingWindow(List<Integer> nums, int k)
{
int ans[]=new int[nums.size()-k+1];
int n=nums.size();

Deque<Integer> dq=new LinkedList<>();

int m=0;

for(int i=0;i<n;i++)
{
if(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;
}
40 changes: 40 additions & 0 deletions JavaLeetcode/Valid Sudoku.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions JavaLeetcode/Wildcard Matching.java
Original file line number Diff line number Diff line change
@@ -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];
}