Skip to content

Instantly share code, notes, and snippets.

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]
}
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) {
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];
@cc2011
cc2011 / gist:7795c28eb4f9c150a6fa77a5821d2510
Created September 5, 2017 05:48
good zookeeper video
https://www.youtube.com/watch?v=2RfBHqDWa60&t=56s
mail.google.com/mail/u/0/#inbox
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) {
https://leetcode.com/problems/solve-the-equation/description/
https://leetcode.com/problems/find-duplicate-subtrees/description/
1
/ \
2 3
/ / \
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
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++) {
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;
@cc2011
cc2011 / gist:c3627429b3a4d0cbad7893259d18629b
Created August 18, 2017 08:03
636. Exclusive Time of Functions
=====================
====== ======
public class Solution {
class Pair {
int time;
int id;
boolean isStart;