24 files changed

+988
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ leetcode.ipr
1313
leetcode.iws
1414
out/
1515
problems/src.iml
16-
.idea/*
16+
.idea
1717
.gradle/
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ My accepted leetcode solutions to some of the common interview problems.
266266
- [Toss Strange Coins](problems/src/dynamic_programming/TossStrangeCoins.java) (Medium)
267267
- [Knight Dialer](problems/src/dynamic_programming/KnightDialer.java) (Medium)
268268
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Palindrome Removal](problems/src/dynamic_programming/PalindromeRemoval.java) (Hard)
269+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Restore The Array](problems/src/dynamic_programming/RestoreTheArray.java) (Hard)
270+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Cherry Pickup II](problems/src/dynamic_programming/CherryPickupII.java) (Hard)
271+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Constrained Subsequence Sum](problems/src/dynamic_programming/ConstrainedSubsequenceSum.java) (Hard)
272+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Largest Multiple of Three](problems/src/dynamic_programming/LargestMultipleOfThree.java) (Hard)
273+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Largest Multiple of Three](problems/src/dynamic_programming/MaximumProfitInJobScheduling.java) (Hard)
274+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Number of Music Playlists](problems/src/dynamic_programming/NumberOfMusicPlaylists.java) (Hard)
275+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Paint House III](problems/src/dynamic_programming/PaintHouseIII.java) (Hard)
276+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Shortest Path Visiting All Nodes](problems/src/dynamic_programming/ShortestPathVisitingAllNodes.java) (Hard)
277+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Smallest Sufficient Team](problems/src/dynamic_programming/SmallestSufficientTeam.java) (Hard)
278+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Stone Game IV](problems/src/dynamic_programming/StoneGameIV.java) (Hard)
279+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Tallest Billboard](problems/src/dynamic_programming/TallestBillboard.java) (Hard)
269280

270281

271282
#### [Greedy](problems/src/greedy)
@@ -287,6 +298,7 @@ My accepted leetcode solutions to some of the common interview problems.
287298
- [Broken Calculator](problems/src/greedy/BrokenCalculator.java) (Medium)
288299
- [Two City Scheduling](problems/src/greedy/TwoCityScheduling.java) (Easy)
289300
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Minimum Time to Build Blocks](problems/src/greedy/MinimumTimeToBuildBlocks.java) (Hard)
301+
- ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) [Reducing Dishes](problems/src/greedy/ReducingDishes.java) (Hard)
290302

291303
#### [Hashing](problems/src/hashing)
292304

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package dynamic_programming;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 28/07/2020 Given a rows x cols matrix grid representing a field
7+
* of cherries. Each cell in grid represents the number of cherries that you can collect.
8+
*
9+
* <p>You have two robots that can collect cherries for you, Robot #1 is located at the top-left
10+
* corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid.
11+
*
12+
* <p>Return the maximum number of cherries collection using both robots by following the rules
13+
* below:
14+
*
15+
* <p>From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1). When any robot
16+
* is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).
17+
* When both robots stay on the same cell, only one of them takes the cherries. Both robots cannot
18+
* move outside of the grid at any moment. Both robots should reach the bottom row in the grid.
19+
*
20+
* <p>Example 1:
21+
*
22+
* <p>Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] Output: 24 Explanation: Path of robot #1 and
23+
* #2 are described in color green and blue respectively. Cherries taken by Robot #1, (3 + 2 + 5 +
24+
* 2) = 12. Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. Total of cherries: 12 + 12 = 24.
25+
* Example 2:
26+
*
27+
* <p>Input: grid =
28+
* [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]] Output: 28
29+
* Explanation: Path of robot #1 and #2 are described in color green and blue respectively. Cherries
30+
* taken by Robot #1, (1 + 9 + 5 + 2) = 17. Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. Total
31+
* of cherries: 17 + 11 = 28. Example 3:
32+
*
33+
* <p>Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] Output: 22 Example 4:
34+
*
35+
* <p>Input: grid = [[1,1],[1,1]] Output: 4
36+
*
37+
* <p>Constraints:
38+
*
39+
* <p>rows == grid.length cols == grid[i].length 2 <= rows, cols <= 70 0 <= grid[i][j] <= 100
40+
*/
41+
public class CherryPickupII {
42+
private final int[] R = {1, 1, 1};
43+
private final int[] C = {0, -1, 1};
44+
45+
public static void main(String[] args) {
46+
int[][] A = {
47+
{1, 0, 0, 3},
48+
{2, 0, 0, 0, 0, 3, 0},
49+
{2, 0, 9, 0, 0, 0, 0},
50+
{0, 3, 0, 5, 4, 0, 0},
51+
{1, 0, 2, 3, 0, 0, 6}
52+
};
53+
System.out.println(new CherryPickupII().cherryPickup(A));
54+
}
55+
56+
int[][][] DP;
57+
58+
public int cherryPickup(int[][] grid) {
59+
DP = new int[grid.length][grid[0].length][grid[0].length];
60+
for (int i = 0; i < grid.length; i++) {
61+
for (int j = 0; j < grid[0].length; j++) {
62+
Arrays.fill(DP[i][j], -1);
63+
}
64+
}
65+
return dp(0, 0, grid[0].length - 1, grid);
66+
}
67+
68+
private int dp(int r, int c1, int c2, int[][] grid) {
69+
if (DP[r][c1][c2] != -1) return DP[r][c1][c2];
70+
else {
71+
int count = (c1 == c2) ? grid[r][c1] : (grid[r][c1] + grid[r][c2]);
72+
int max = count;
73+
for (int i = 0; i < 3; i++) {
74+
int newR = r + R[i];
75+
int newC1 = c1 + C[i];
76+
if (newR >= 0 && newR < grid.length && newC1 >= 0 && newC1 < grid[0].length) {
77+
for (int j = 0; j < 3; j++) {
78+
int newC2 = c2 + C[j];
79+
if (newC2 >= 0 && newC2 < grid[0].length) {
80+
max = Math.max(max, count + dp(newR, newC1, newC2, grid));
81+
}
82+
}
83+
}
84+
}
85+
DP[r][c1][c2] = max;
86+
return max;
87+
}
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dynamic_programming;
2+
3+
import java.util.*;
4+
5+
/** Created by gouthamvidyapradhan on 14/05/2020 */
6+
public class ConstrainedSubsequenceSum {
7+
8+
public static void main(String[] args) {
9+
int[] A = {10, -2, -10, -5, 20};
10+
System.out.println(new ConstrainedSubsequenceSum().constrainedSubsetSum(A, 2));
11+
}
12+
13+
class Node {
14+
int v, i;
15+
16+
public int getV() {
17+
return v;
18+
}
19+
20+
public int getI() {
21+
return i;
22+
}
23+
24+
Node(int v, int i) {
25+
this.v = v;
26+
this.i = i;
27+
}
28+
}
29+
30+
public int constrainedSubsetSum(int[] nums, int k) {
31+
Queue<Node> pQ =
32+
new PriorityQueue<>(Comparator.comparing(Node::getV).thenComparing(Node::getI).reversed());
33+
int max = Integer.MIN_VALUE;
34+
for (int i = 0; i < nums.length; i++) {
35+
int value = nums[i];
36+
while (!pQ.isEmpty() && (i - pQ.peek().i > k)) {
37+
pQ.poll();
38+
}
39+
if (pQ.isEmpty()) {
40+
pQ.offer(new Node(value, i));
41+
} else {
42+
if (pQ.peek().v + value > value) {
43+
pQ.offer(new Node(pQ.peek().v + value, i));
44+
} else {
45+
pQ.offer(new Node(value, i));
46+
}
47+
}
48+
max = Math.max(max, pQ.peek().v);
49+
}
50+
return max;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dynamic_programming;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 29/05/2020 Given an integer array of digits, return the largest
7+
* multiple of three that can be formed by concatenating some of the given digits in any order.
8+
*
9+
* <p>Since the answer may not fit in an integer data type, return the answer as a string.
10+
*
11+
* <p>If there is no answer return an empty string.
12+
*
13+
* <p>Example 1:
14+
*
15+
* <p>Input: digits = [8,1,9] Output: "981" Example 2:
16+
*
17+
* <p>Input: digits = [8,6,7,1,0] Output: "8760" Example 3:
18+
*
19+
* <p>Input: digits = [1] Output: "" Example 4:
20+
*
21+
* <p>Input: digits = [0,0,0,0,0,0] Output: "0"
22+
*
23+
* <p>Constraints:
24+
*
25+
* <p>1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain
26+
* unnecessary leading zeros.
27+
*/
28+
public class LargestMultipleOfThree {
29+
public static void main(String[] args) {
30+
int[] A = {8, 4, 1, 7};
31+
System.out.println(new LargestMultipleOfThree().largestMultipleOfThree(A));
32+
}
33+
34+
int[][][] DP;
35+
36+
public String largestMultipleOfThree(int[] digits) {
37+
Arrays.sort(digits);
38+
for (int i = 0, j = digits.length - 1; i < j; i++, j--) {
39+
int t = digits[i];
40+
digits[i] = digits[j];
41+
digits[j] = t;
42+
}
43+
DP = new int[digits.length][3][2];
44+
for (int i = 0; i < digits.length; i++) {
45+
for (int j = 0; j < 3; j++) {
46+
Arrays.fill(DP[i][j], -2);
47+
}
48+
}
49+
dp(0, 0, digits);
50+
StringBuilder sb = new StringBuilder();
51+
int r = 0;
52+
for (int i = 0; i < digits.length; i++) {
53+
if (DP[i][r][1] >= DP[i][r][0]) {
54+
if (sb.length() != 1 || sb.charAt(0) != '0') {
55+
sb.append(digits[i]);
56+
}
57+
r = (Integer.parseInt(String.valueOf(r + "" + digits[i])) % 3);
58+
}
59+
}
60+
return sb.toString();
61+
}
62+
63+
private int dp(int i, int r, int[] A) {
64+
if (i == A.length) {
65+
return r == 0 ? 0 : -1;
66+
} else if (DP[i][r][0] != -2) {
67+
return Math.max(DP[i][r][0], DP[i][r][1]);
68+
} else {
69+
int d = A[i];
70+
int newR = (Integer.parseInt(String.valueOf(r + "" + d)) % 3);
71+
int result = dp(i + 1, newR, A);
72+
if (result >= 0) {
73+
result = result + 1;
74+
}
75+
DP[i][r][1] = result;
76+
DP[i][r][0] = dp(i + 1, r, A);
77+
return Math.max(DP[i][r][0], DP[i][r][1]);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)