Skip to content

Commit ee0b6b9

Browse files
Solved Problems
1 parent b0e7b60 commit ee0b6b9

20 files changed

+1523
-88
lines changed

README.md

Lines changed: 108 additions & 88 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 14/08/2019 Given an array of 2n integers, your task is to group
7+
* these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of
8+
* min(ai, bi) for all i from 1 to n as large as possible.
9+
*
10+
* <p>Example 1: Input: [1,4,3,2]
11+
*
12+
* <p>Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
13+
* Note: n is a positive integer, which is in the range of [1, 10000]. All the integers in the array
14+
* will be in the range of [-10000, 10000].
15+
*
16+
* <p>Solution: O(n log n) General idea is to pair the smallest with the next smallest value inorder
17+
* to get the max sum of minimum.
18+
*/
19+
public class ArrayPartitionI {
20+
public static void main(String[] args) {
21+
int[] A = {1, 2, 3, 4};
22+
System.out.println(new ArrayPartitionI().arrayPairSum(A));
23+
}
24+
25+
public int arrayPairSum(int[] nums) {
26+
Arrays.sort(nums);
27+
int sum = 0;
28+
for (int i = 1; i < nums.length; i += 2) {
29+
sum += Math.min(nums[i - 1], nums[i]);
30+
}
31+
return sum;
32+
}
33+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 14/08/2019 Given a 01 matrix M, find the longest line of
5+
* consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal.
6+
* Example: Input: [[0,1,1,0], [0,1,1,0], [0,0,0,1]] Output: 3 Hint: The number of elements in the
7+
* given matrix will not exceed 10,000.
8+
*
9+
* <p>Solution O(N x M) for each cell keep track of maximum value possible horizontally, vertically
10+
* and diagonally. Start iterating from left-right and top-bottom and repeat the same from
11+
* right-left and top to bottom to get max for anti-diagonal and return the max value.
12+
*/
13+
public class LongestLineofConsecutiveOneinMatrix {
14+
final int[] R = {0, 0, -1, 1};
15+
final int[] C = {1, -1, 0, 0};
16+
17+
public static void main(String[] args) {
18+
int[][] M = {
19+
{1, 1, 0, 0, 1, 0, 0, 1, 1, 0},
20+
{1, 0, 0, 1, 0, 1, 1, 1, 1, 1},
21+
{1, 1, 1, 0, 0, 1, 1, 1, 1, 0},
22+
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1},
23+
{0, 0, 1, 1, 1, 1, 1, 1, 1, 0},
24+
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
25+
{0, 1, 1, 1, 1, 1, 1, 0, 0, 1},
26+
{1, 1, 1, 1, 1, 0, 0, 1, 1, 1},
27+
{0, 1, 0, 1, 1, 0, 1, 1, 1, 1},
28+
{1, 1, 1, 0, 1, 0, 1, 1, 1, 1}
29+
};
30+
System.out.println(new LongestLineofConsecutiveOneinMatrix().longestLine(M));
31+
}
32+
33+
class Cell {
34+
int h, v, d;
35+
36+
Cell(int h, int v, int d) {
37+
this.h = h;
38+
this.v = v;
39+
this.d = d;
40+
}
41+
}
42+
43+
public int longestLine(int[][] M) {
44+
if (M.length == 0) return 0;
45+
int max = 0;
46+
Cell[][] cells = new Cell[M.length][M[0].length];
47+
for (int i = 0; i < M.length; i++) {
48+
for (int j = 0; j < M[0].length; j++) {
49+
int h = 0, v = 0, d = 0;
50+
if (M[i][j] == 1) {
51+
h = 1;
52+
v = 1;
53+
d = 1;
54+
max = Math.max(max, 1);
55+
if (j - 1 >= 0) {
56+
Cell left = cells[i][j - 1];
57+
if (left.h > 0) {
58+
h += left.h;
59+
max = Math.max(max, h);
60+
}
61+
}
62+
if (i - 1 >= 0) {
63+
Cell top = cells[i - 1][j];
64+
if (top.v > 0) {
65+
v += top.v;
66+
max = Math.max(max, v);
67+
}
68+
}
69+
if (i - 1 >= 0 && j - 1 >= 0) {
70+
Cell diagonal = cells[i - 1][j - 1];
71+
if (diagonal.d > 0) {
72+
d += diagonal.d;
73+
max = Math.max(max, d);
74+
}
75+
}
76+
}
77+
cells[i][j] = new Cell(h, v, d);
78+
}
79+
}
80+
81+
for (int i = 0; i < M.length; i++) {
82+
for (int j = M[0].length - 1; j >= 0; j--) {
83+
int h = 0, v = 0, d = 0;
84+
if (M[i][j] == 1) {
85+
h = 1;
86+
v = 1;
87+
d = 1;
88+
max = Math.max(max, 1);
89+
if (j + 1 < M[0].length) {
90+
Cell left = cells[i][j + 1];
91+
if (left.h > 0) {
92+
h += left.h;
93+
max = Math.max(max, h);
94+
}
95+
}
96+
if (i - 1 >= 0) {
97+
Cell top = cells[i - 1][j];
98+
if (top.v > 0) {
99+
v += top.v;
100+
max = Math.max(max, v);
101+
}
102+
}
103+
if (i - 1 >= 0 && j + 1 < M[0].length) {
104+
Cell diagonal = cells[i - 1][j + 1];
105+
if (diagonal.d > 0) {
106+
d += diagonal.d;
107+
max = Math.max(max, d);
108+
}
109+
}
110+
}
111+
cells[i][j] = new Cell(h, v, d);
112+
}
113+
}
114+
115+
return max;
116+
}
117+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 15/08/2019 We are given a matrix with R rows and C columns has
7+
* cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
8+
*
9+
* <p>Additionally, we are given a cell in that matrix with coordinates (r0, c0).
10+
*
11+
* <p>Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from
12+
* smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2)
13+
* is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that
14+
* satisfies this condition.)
15+
*
16+
* <p>Example 1:
17+
*
18+
* <p>Input: R = 1, C = 2, r0 = 0, c0 = 0 Output: [[0,0],[0,1]] Explanation: The distances from (r0,
19+
* c0) to other cells are: [0,1] Example 2:
20+
*
21+
* <p>Input: R = 2, C = 2, r0 = 0, c0 = 1 Output: [[0,1],[0,0],[1,1],[1,0]] Explanation: The
22+
* distances from (r0, c0) to other cells are: [0,1,1,2] The answer [[0,1],[1,1],[0,0],[1,0]] would
23+
* also be accepted as correct. Example 3:
24+
*
25+
* <p>Input: R = 2, C = 3, r0 = 1, c0 = 2 Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]] Explanation:
26+
* The distances from (r0, c0) to other cells are: [0,1,1,2,2,3] There are other answers that would
27+
* also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
28+
*
29+
* <p>Note:
30+
*
31+
* <p>1 <= R <= 100 1 <= C <= 100 0 <= r0 < R 0 <= c0 < C
32+
*
33+
* <p>Solution: O (log (R x C)) Straight forward solution, find the Manhattan distance from the
34+
* given cell to all the cells in the grid and sort by min distance and return their coordinates.
35+
*/
36+
public class MatrixCellsinDistanceOrder {
37+
public static void main(String[] args) {
38+
//
39+
}
40+
41+
class Cell {
42+
int max, i, j;
43+
44+
Cell(int max, int i, int j) {
45+
this.max = max;
46+
this.i = i;
47+
this.j = j;
48+
}
49+
}
50+
51+
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
52+
List<Cell> list = new ArrayList<>();
53+
for (int i = 0; i < R; i++) {
54+
for (int j = 0; j < C; j++) {
55+
int sum = Math.abs(r0 - i) + Math.abs(c0 - j);
56+
list.add(new Cell(sum, i, j));
57+
}
58+
}
59+
list.sort(Comparator.comparingInt(o -> o.max));
60+
int[][] A = new int[list.size()][2];
61+
for (int i = 0; i < A.length; i++) {
62+
A[i][0] = list.get(i).i;
63+
A[i][1] = list.get(i).j;
64+
}
65+
return A;
66+
}
67+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 15/08/2019 Given an array A of non-negative integers, return
7+
* the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L
8+
* and M. (For clarification, the L-length subarray could occur before or after the M-length
9+
* subarray.)
10+
*
11+
* <p>Formally, return the largest V for which V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1]
12+
* + ... + A[j+M-1]) and either:
13+
*
14+
* <p>0 <= i < i + L - 1 < j < j + M - 1 < A.length, or 0 <= j < j + M - 1 < i < i + L - 1 <
15+
* A.length.
16+
*
17+
* <p>Example 1:
18+
*
19+
* <p>Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2 Output: 20 Explanation: One choice of subarrays
20+
* is [9] with length 1, and [6,5] with length 2. Example 2:
21+
*
22+
* <p>Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2 Output: 29 Explanation: One choice of subarrays
23+
* is [3,8,1] with length 3, and [8,9] with length 2. Example 3:
24+
*
25+
* <p>Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3 Output: 31 Explanation: One choice of subarrays
26+
* is [5,6,0,9] with length 4, and [3,8] with length 3.
27+
*
28+
* <p>Note:
29+
*
30+
* <p>L >= 1 M >= 1 L + M <= A.length <= 1000 0 <= A[i] <= 1000 Solution O(N ^ 2) Find prefix sum of
31+
* array of length L and array of length M and keep track of their begin and end indices. Now,
32+
* brute-force compare pairs of prefix array sum where their indices don't overlap and return the
33+
* max possible answer.
34+
*/
35+
public class MaximumSumofTwoNonOverlappingSubarrays {
36+
public static void main(String[] args) {
37+
int[] A = {2, 1, 5, 6, 0, 9, 5, 0, 3, 8};
38+
System.out.println(new MaximumSumofTwoNonOverlappingSubarrays().maxSumTwoNoOverlap(A, 4, 3));
39+
}
40+
41+
class MaxWithIndex {
42+
int max, i, j;
43+
44+
MaxWithIndex(int max, int i, int j) {
45+
this.max = max;
46+
this.i = i;
47+
this.j = j;
48+
}
49+
}
50+
51+
public int maxSumTwoNoOverlap(int[] A, int L, int M) {
52+
List<MaxWithIndex> first = getMax(A, L);
53+
List<MaxWithIndex> second = getMax(A, M);
54+
int max = 0;
55+
for (MaxWithIndex f : first) {
56+
for (MaxWithIndex s : second) {
57+
if (f.j < s.i || s.j < f.i) {
58+
max = Math.max(max, f.max + s.max);
59+
}
60+
}
61+
}
62+
return max;
63+
}
64+
65+
private List<MaxWithIndex> getMax(int[] A, int L) {
66+
List<MaxWithIndex> list = new ArrayList<>();
67+
int i = 0, j = L;
68+
int sum = 0;
69+
for (; i < L; i++) {
70+
sum += A[i];
71+
}
72+
list.add(new MaxWithIndex(sum, 0, j - 1));
73+
for (i = 1; j < A.length; i++, j++) {
74+
sum -= A[i - 1];
75+
sum += A[j];
76+
list.add(new MaxWithIndex(sum, i, j));
77+
}
78+
return list;
79+
}
80+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 20/08/2019 Given an array A of non-negative integers, half of
7+
* the integers in A are odd, and half of the integers are even.
8+
*
9+
* <p>Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.
10+
*
11+
* <p>You may return any answer array that satisfies this condition.
12+
*
13+
* <p>Example 1:
14+
*
15+
* <p>Input: [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also
16+
* have been accepted.
17+
*
18+
* <p>Note:
19+
*
20+
* <p>2 <= A.length <= 20000 A.length % 2 == 0 0 <= A[i] <= 1000 Solution: O(N) straight forward
21+
* linear solution, keep track of odd and even indices and increment by 2 every time a value is
22+
* added at the index.
23+
*/
24+
public class SortArrayByParityII {
25+
public static void main(String[] args) {
26+
//
27+
}
28+
29+
public int[] sortArrayByParityII(int[] A) {
30+
int[] R = new int[A.length];
31+
int i = 0, j = 1;
32+
for (int a : A) {
33+
if (a % 2 == 0) {
34+
R[i] = a;
35+
i += 2;
36+
} else {
37+
R[j] = a;
38+
j += 2;
39+
}
40+
}
41+
return R;
42+
}
43+
}

0 commit comments

Comments
 (0)