forked from prajwal126/FAANG
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
1,841 changed files
with
301 additions
and
29 deletions.
There are no files selected for viewing
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
Algorithms/sachi/challenges-contests/leetcode/biweekly28/MinSumOfLengths.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
Algorithms/sachi/challenges-contests/leetcode/biweekly28/SubrectangleQueries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |
55 changes: 55 additions & 0 deletions
55
Algorithms/sachi/challenges-contests/leetcode/weekly193/KthAncestorOfATreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.