Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Jun 15, 2020
2 parents 874cff7 + 64299dc commit 283c3bd
Show file tree
Hide file tree
Showing 1,841 changed files with 301 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.PriorityQueue;
import java.util.Queue;

//TODO:SACHIN::WRONG Answer
public class MinSumOfLengths {

public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 2, 6, 7, 2, 1, 4, 8};
int target = 5;
System.out.println(minSumOfLengths(arr, target));
}

public static int minSumOfLengths(int[] arr, int target) {
Queue<Integer> pq = new PriorityQueue<>();
//Find all subarrays
int sum = 0, counter = 0;
for (Integer i : arr) {
sum += i;
counter++;
if (sum > target) {
counter = 1;
sum = i;
}
if (sum == target) {
pq.add(counter);
sum = 0;
counter = 0;
}
}
if (pq.size() < 2) {
return -1;
} else {
return pq.poll() + pq.poll();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class SubrectangleQueries {

int[][] matrix;

public SubrectangleQueries(int[][] rectangle) {
matrix = rectangle;
}

public static void main(String[] args) {
int[][] matrix = new int[][]{
{5,2,5,9,4},
{10,7,1,4,1},
{7,3,1,3,8},
{9,7,9,4,9}
};
SubrectangleQueries subrectangleQueries = new SubrectangleQueries(matrix);
Util.print(matrix);

subrectangleQueries.updateSubrectangle(1, 0, 3, 3, 10);
Util.print(matrix);

subrectangleQueries.updateSubrectangle(3, 2, 3, 2, 4);
Util.print(matrix);

System.out.println(subrectangleQueries.getValue(2, 0)); // return 10
System.out.println(subrectangleQueries.getValue(2, 2)); // return 5
System.out.println(subrectangleQueries.getValue(3, 4)); // return 5

subrectangleQueries.updateSubrectangle(1, 4, 1, 4, 10);
Util.print(matrix);

}

public void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
int r = row1, c = col1;
while (r < matrix.length && r <= row2) {
c = col1;
while (c < matrix[r].length && c <= col2) {
matrix[r][c] = newValue;
c++;
}
r++;
}
}

public int getValue(int row, int col) {
return matrix[row][col];
}
}


/**
* Your SubrectangleQueries object will be instantiated and called as such:
* SubrectangleQueries obj = new SubrectangleQueries(rectangle);
* obj.updateSubrectangle(row1,col1,row2,col2,newValue);
* int param_2 = obj.getValue(row,col);
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.HashMap;
import java.util.Map;

//TODO:SACHIN::WRONG Answer
public class KthAncestorOfATreeNode {

int size;
int[] arr;
Map<String, Integer> cache;
int height;

public KthAncestorOfATreeNode(int n, int[] parent) {
size = n;
arr = parent;
cache = new HashMap<>();
height = (n - 1) / 2;
}

public static void main(String[] args) {
int n= 10;
int[] arr = new int[]{-1,0,1,2,0,1,0,4,7,1};
KthAncestorOfATreeNode kthAncestorOfATreeNode = new KthAncestorOfATreeNode(n, arr);
System.out.println(kthAncestorOfATreeNode.getKthAncestor(3, 3));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(2, 9));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(2, 7));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(3, 2));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(2, 10));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(4, 9));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(0, 2));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(6, 4));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(4, 2));
System.out.println(kthAncestorOfATreeNode.getKthAncestor(4, 7));
}

public int getKthAncestor(int node, int k) {
int counter = 1;
if (k > height) return -1;
String key = "" + node + "+" + k;
if (cache.containsKey(key)) return cache.get(key);
int sol = node;
while (k > 0 && sol >= 0) {
sol = arr[sol];
k--;
cache.put("" + node + "+" + counter, sol);
counter++;
}
if (k > 0) {
cache.put(key, -1);
return -1;
} else {
cache.put(key, sol);
return sol;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 283c3bd

Please sign in to comment.