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
https://discuss.leetcode.com/topic/97628/java-4-lines-recursion-with-step-by-step-explanation-to-derive-dp | |
public int maxA(int n) { | |
int[] dp = new int[n + 1]; | |
for (int i = 0; i <= n; i++) { | |
dp[i] = i; | |
for (int j = 1;j <= i - 3; j++) {//dp[j] => with j operations, max num of A we can produce | |
dp[i] = Math.max(dp[i], dp[j] * (i - j - 1)); | |
dp[j] * (i-j-2) + dp[j] | |
} |
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
class Solution { | |
/* | |
public int minSteps(int n) { | |
int[] dp = new int[n+1]; | |
for(int i=0; i <= n; i++) { | |
dp[i] = -1; | |
} | |
return recurse(dp, n); | |
} | |
public int recurse(int[] dp, int currNum) { |
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
public class Solution { | |
/* | |
* @param A: An integer array | |
* @param k: A positive integer (k <= length(A)) | |
* @param target: An integer | |
* @return: An integer | |
*/ | |
public int kSum(int[] A, int k, int target) { | |
int n = A.length; | |
int[][][] dp = new int[n+1][k+1][target+1]; |
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
https://www.youtube.com/watch?v=2RfBHqDWa60&t=56s | |
mail.google.com/mail/u/0/#inbox |
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
public boolean canCross(int[] stones) { | |
// Write your code here | |
HashMap<Integer, HashSet<Integer>> dp = | |
new HashMap<Integer, HashSet<Integer>>(stones.length); | |
for (int i = 0; i < stones.length; i++) { | |
dp.put(stones[i], new HashSet<Integer>() ); | |
} | |
dp.get(0).add(0); | |
for (int i = 0; i < stones.length - 1; ++i) { |
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
https://leetcode.com/problems/solve-the-equation/description/ | |
https://leetcode.com/problems/find-duplicate-subtrees/description/ | |
1 | |
/ \ | |
2 3 | |
/ / \ |
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
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { |
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
class Solution { | |
public int[][] imageSmoother(int[][] M) { | |
int n = M.length; | |
int m = M[0].length; | |
int[][] res = new int[n][m]; | |
int[][] dir = {{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1},{0,0}}; | |
for(int i=0; i < n; i++) { | |
for(int j=0; j < m; j++) { | |
int count = 0; | |
for(int k=0; k < dir.length; k++) { |
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
class Solution { | |
/* | |
public int hIndex(int[] citations) { | |
Arrays.sort(citations); | |
int res = citations.length; | |
for(int i=0; i < citations.length; i++) { | |
if( res > citations[i] ) { | |
res--; | |
} else { | |
return res; |
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
===================== | |
====== ====== | |
public class Solution { | |
class Pair { | |
int time; | |
int id; | |
boolean isStart; |
NewerOlder