Skip to content

Commit 6ef798e

Browse files
author
Sonia Mathew
committed
more
1 parent 88707c2 commit 6ef798e

11 files changed

+588
-168
lines changed

.idea/workspace.xml

+165-168
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DailyCoding189.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/**
5+
* Given an array of elements, return the length of the longest subarray where all its elements are distinct.
6+
*
7+
* For example, given the array [5, 1, 3, 5, 2, 3, 4, 1], return 5 as the longest subarray of distinct elements is
8+
* [5, 2, 3, 4, 1].
9+
*/
10+
public class DailyCoding189 {
11+
public static void main(String[] args) {
12+
System.out.println(longestSubArray(new int[]{5,1,3,5,2,3,4,1})==5);
13+
}
14+
public static int longestSubArray(int[] nums) {
15+
Set<Integer> numbers = new HashSet<>();
16+
int start = 0;
17+
int curr = 0;
18+
int maxLen = 0;
19+
while(curr < nums.length) {
20+
if (numbers.contains(nums[curr])) {
21+
maxLen = Math.max(maxLen, numbers.size());
22+
while (nums[start]!= nums[curr]) {
23+
numbers.remove(nums[start]);
24+
start++;
25+
}
26+
numbers.remove(nums[start]);
27+
start++;
28+
}
29+
numbers.add(nums[curr]);
30+
curr++;
31+
}
32+
maxLen = Math.max(maxLen, numbers.size());
33+
return maxLen;
34+
}
35+
}

src/DailyCoding190.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Given a circular array, compute its maximum subarray sum in O(n) time. A subarray can be empty, and in this case the
3+
* sum is 0.
4+
*
5+
* For example, given [8, -1, 3, 4], return 15 as we choose the numbers 3, 4, and 8 where the 8 is obtained from
6+
* wrapping around.
7+
*
8+
* Given [-4, 5, 1, 0], return 6 as we choose the numbers 5 and 1.
9+
*/
10+
public class DailyCoding190 {
11+
public static void main(String[] args) {
12+
System.out.println(maxSumSubArray(new int[]{8,-1,3,4})==15);
13+
System.out.println(maxSumSubArray(new int[]{-4,5,1,0})==6);
14+
}
15+
public static int maxSumSubArray(int[] nums) {
16+
//first find maximum sum sub array considering that the array is not circular
17+
//then find the minimum sum sub array considering that the array is not circular
18+
//maximum sum sub array, when the array is circular would be total sum - min sub array sum
19+
int total = 0, maxSum = Integer.MIN_VALUE, curMax = 0, curMin = 0, minSum = Integer.MAX_VALUE;
20+
for (int n : nums) {
21+
curMax = Math.max(curMax+n, n);
22+
maxSum = Math.max(maxSum, curMax);
23+
curMin = Math.min(curMin+n, n);
24+
minSum = Math.min(curMin, minSum);
25+
total += n;
26+
}
27+
//corner case: when all numbers are negative, the maxSum = max(nums) and minSum = sum(nums).
28+
//max(maxSum, total - minSum) = 0. However, we should be returning 0, representing empty sub array
29+
return maxSum > 0 ? Math.max(maxSum, total - minSum) : 0;
30+
}
31+
}
32+

src/DailyCoding191.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
/**
5+
* Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the
6+
* intervals non-overlapping.
7+
*
8+
* Intervals can "touch", such as [0, 1] and [1, 2], but they won't be considered overlapping.
9+
*
10+
* For example, given the intervals (7, 9), (2, 4), (5, 8), return 1 as the last interval can be removed and the first
11+
* two won't overlap.
12+
*/
13+
public class DailyCoding191 {
14+
public static void main(String[] args) {
15+
16+
}
17+
public int eraseOverlapIntervals(Interval[] intervals) {
18+
if(intervals.length==0){
19+
return 0;
20+
}
21+
Arrays.sort(intervals, (i1, i2) -> {
22+
if(i1.end == i2.end){
23+
return 0;
24+
} else if(i1.end < i2.end){
25+
return -1;
26+
} else if(i1.end > i2.end){
27+
return 1;
28+
}
29+
return 0;
30+
});
31+
int count = 1, end = intervals[0].end;
32+
/*Find the maximum number of non-overlapping intervals.
33+
Then substract that from total number to get minimum number of intervals to be removed.*/
34+
for(int i=1; i < intervals.length; i++){
35+
if(end <= intervals[i].start){
36+
count++;
37+
end = intervals[i].end;
38+
}
39+
}
40+
return intervals.length-count;
41+
}
42+
}

src/DailyCoding192.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* You are given an array of nonnegative integers. Let's say you start at the beginning of the array and are trying to
3+
* advance to the end. You can advance at most, the number of steps that you're currently on. Determine whether you can
4+
* get to the end of the array.
5+
*
6+
* For example, given the array [1, 3, 1, 2, 0, 1], we can go from indices 0 -> 1 -> 3 -> 5, so return true.
7+
*
8+
* Given the array [1, 2, 1, 0, 0], we can't reach the end, so return false.
9+
*/
10+
public class DailyCoding192 {
11+
public static void main(String[] args) {
12+
System.out.println(canJump(new int[]{1,3,1,2,0,1}));
13+
System.out.println(canJump(new int[]{1,2,1,0,0}));
14+
}
15+
public static boolean canJump(int[] nums) {
16+
boolean[] possible = new boolean[nums.length];
17+
if(nums.length==0){
18+
return true;
19+
}
20+
possible[0]=true;
21+
for(int i=0; i<nums.length; i++){
22+
for(int j=i-1; j>=0; j--){
23+
if(possible[j]){
24+
possible[i] = j+nums[j]>=i;
25+
if(possible[i]){
26+
break;
27+
}
28+
}
29+
}
30+
}
31+
return possible[possible.length-1];
32+
}
33+
}

src/DailyCoding194.java

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.Comparator;
4+
import java.util.List;
5+
6+
/**
7+
* Suppose you are given two lists of n points, one list p1, p2, ..., pn on the line y = 0 and the other
8+
* list q1, q2, ..., qn on the line y = 1. Imagine a set of n line segments connecting each point pi to qi.
9+
* Write an algorithm to determine how many pairs of the line segments intersect.
10+
*/
11+
class LPoint {
12+
int x;
13+
int y;
14+
public LPoint(int x, int y) {
15+
this.x = x;
16+
this.y = y;
17+
}
18+
}
19+
public class DailyCoding194 {
20+
public static void main(String[] args) {
21+
System.out.println(numInversions(new int[]{2,4,1,3,5}) == 3);
22+
System.out.println(numInversions(new int[]{1, 20, 6, 4, 5}) == 5);
23+
}
24+
public static int intersections(int[] p, int[] q) {
25+
List<LPoint> points = new ArrayList<>();
26+
for (int i=0; i<p.length; i++) {
27+
points.add(new LPoint(p[i], q[i]));
28+
}
29+
Collections.sort(points, (o1, o2) -> {
30+
if (o1.x == o2.x) {
31+
return 0;
32+
} else if (o1.x > o2.x) {
33+
return 1;
34+
} else {
35+
return -1;
36+
}
37+
});
38+
int[] sorted = new int[q.length];
39+
for (int i=0; i<points.size(); i++) {
40+
sorted[i] = points.get(i).y;
41+
}
42+
return numInversions(sorted);
43+
}
44+
public static int numInversions(int[] arr) {
45+
return mergeSort(arr, 0, arr.length-1);
46+
}
47+
public static int mergeSort(int[] arr, int start, int end) {
48+
int count = 0;
49+
if (start < end) {
50+
int mid = (start + end)/2;
51+
count += mergeSort(arr, start, mid);
52+
count += mergeSort(arr, mid+1, end);
53+
count += merge(arr, start, mid, end);
54+
}
55+
return count;
56+
}
57+
public static int merge(int[] arr, int start, int mid, int end) {
58+
int n = end-start+1;
59+
int[] tmp = new int[n];
60+
int l = start, r = mid+1;
61+
int k = 0, count = 0;
62+
while (l <= mid && r<=end) {
63+
if (arr[l] <= arr[r]) {
64+
tmp[k] = arr[l];
65+
l++;
66+
k++;
67+
} else {
68+
count += (mid - l + 1);
69+
tmp[k] = arr[r];
70+
r++;
71+
k++;
72+
}
73+
}
74+
while (l <= mid) {
75+
tmp[k] = arr[l];
76+
k++;
77+
l++;
78+
}
79+
while (r<=end) {
80+
tmp[k] = arr[r];
81+
r++;
82+
k++;
83+
}
84+
for (int i=start; i<=end; i++) {
85+
arr[i] = tmp[i-start];
86+
}
87+
return count;
88+
}
89+
}

src/DailyCoding195.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Let A be an N by M matrix in which every row and every column is sorted.
3+
*
4+
* Given i1, j1, i2, and j2, compute the number of elements of M smaller than M[i1, j1] and larger than M[i2, j2].
5+
*/
6+
public class DailyCoding195 {
7+
public static void main(String[] args) {
8+
int[][] matrix = {{1,3,7,10,15,20}, {2,6,9,14,22,25}, {3,8,10,15,25,20}, {10,11,12,23,30,35}, {20,25,30,35,40,45}};
9+
System.out.println(count(matrix, 1, 1, 3, 3));
10+
}
11+
public static int count(int[][] matrix, int i1, int j1, int i2, int j2) {
12+
if (i1 == i2) {
13+
if (j1 < j2) {
14+
return countHelper(matrix, i1, j1, i2, j2);
15+
} else {
16+
return countHelper(matrix, i2, j2, i1, j1);
17+
}
18+
} else if(i1 < i2){
19+
return countHelper(matrix, i1, j1, i2, j2);
20+
} else {
21+
return countHelper(matrix, i2, j2, i1, j1);
22+
}
23+
}
24+
public static int countHelper(int[][] matrix, int i1, int j1, int i2, int j2) {
25+
int count = 0;
26+
for (int i=0; i< matrix.length; i++) {
27+
int start = 0;
28+
while (start < matrix[0].length && matrix[i][start] < matrix[i1][j1]) {
29+
start++;
30+
}
31+
if (start == matrix[0].length) {
32+
//there is nothing greater than starting point in this row.
33+
continue;
34+
}
35+
int end = matrix.length-1;
36+
while (end >=0 && matrix[i][end] > matrix[i2][j2]) {
37+
end--;
38+
}
39+
if (end < 0) {
40+
//there is nothing less than ending point
41+
continue;
42+
}
43+
count += end - start + 1;
44+
}
45+
return count;
46+
}
47+
}

src/DailyCoding196.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.*;
2+
3+
/**
4+
* Given the root of a binary tree, find the most frequent subtree sum. The subtree sum of a node is the sum of all
5+
* values under a node, including the node itself.
6+
*
7+
* 5
8+
* / \
9+
* 2 -5
10+
*
11+
* Return 2 as it occurs twice: once as the left leaf, and once as the sum of 2 + 5 - 5.
12+
*/
13+
14+
public class DailyCoding196 {
15+
public static void main(String[] args) {
16+
TreeNode root = new TreeNode(5);
17+
root.left = new TreeNode(2);
18+
root.right = new TreeNode(-5);
19+
Utility.print(findFrequentTreeSum(root));
20+
}
21+
public static int[] findFrequentTreeSum(TreeNode root) {
22+
HashMap<Integer, Integer> counts = new HashMap<>();
23+
int[] maxCount = {0};
24+
findFrequentTreeSumHelper(root, counts, maxCount);
25+
List<Integer> rs = new ArrayList<>();
26+
for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
27+
if (entry.getValue() == maxCount[0]) {
28+
rs.add(entry.getKey());
29+
}
30+
}
31+
int[] res = new int[rs.size()];
32+
for (int i=0; i<rs.size(); i++) {
33+
res[i] = rs.get(i);
34+
}
35+
return res;
36+
}
37+
public static int findFrequentTreeSumHelper(TreeNode root, HashMap<Integer, Integer> counts, int[] maxCount) {
38+
if (root == null) {
39+
return 0;
40+
}
41+
int left = findFrequentTreeSumHelper(root.left, counts, maxCount);
42+
int right = findFrequentTreeSumHelper(root.right, counts, maxCount);
43+
44+
int total = root.val + left + right;
45+
int count = counts.getOrDefault(total, 0);
46+
counts.put(total, count+1);
47+
maxCount[0] = Math.max(maxCount[0], count+1);
48+
return total;
49+
}
50+
}
51+

src/DailyCoding197.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Given an array and a number k that's smaller than the length of the array, rotate the array to the right
3+
* k elements in-place.
4+
*/
5+
public class DailyCoding197 {
6+
public static void main(String[] args) {
7+
Utility.print(rotate(new int[]{1,2,3,4,5,6,7}, 3));
8+
}
9+
public static int[] rotate(int[] nums, int k) {
10+
int n = nums.length;
11+
k = k%n;
12+
int displaced = 0;
13+
for(int i=0; displaced < n; i++) {
14+
int current = i;
15+
int value = nums[i];
16+
do {
17+
int index = (i+k)%n;
18+
int replaced = nums[index];
19+
nums[index] = value;
20+
i = index;
21+
value = replaced;
22+
displaced++;
23+
} while (i != current);
24+
}
25+
return nums;
26+
}
27+
}

0 commit comments

Comments
 (0)