pull/3/head
Leo 5 years ago
parent 99a7cdfd30
commit 3548d2a957

@ -45,6 +45,35 @@ public class CardsInLine {
}
}
static class Recursion1 {
public static int win(int[] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
int f = f(arr, 0, arr.length - 1);
int g = g(arr, 0, arr.length - 1);
return Math.max(g, f);
}
private static int g(int[] arr, int l, int r) {
if (l == r) {
return 0;
}
int p1 = f(arr, l + 1, r);
int p2 = f(arr, l, r - 1);
return Math.min(p1, p2);
}
private static int f(int[] arr, int l, int r) {
if (l == r) {
return arr[l];
}
int p1 = arr[l] + g(arr, l + 1, r);
int p2 = arr[r] + g(arr, l, r - 1);
return Math.max(p1, p2);
}
}
static class RecursionDp {
public static int win(int[] arr) {
if (arr.length == 0 || arr == null) {
@ -94,6 +123,57 @@ public class CardsInLine {
}
}
static class RecursionDp1 {
public static int win(int[] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
int n = arr.length;
int[][] fDp = new int[n][n];
int[][] gDp = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
fDp[i][j] = -1;
gDp[i][j] = -1;
}
}
int f = f(arr, 0, n - 1, fDp, gDp);
int g = g(arr, 0, n - 1, fDp, gDp);
return Math.max(f, g);
}
private static int g(int[] arr, int l, int r, int[][] fDp, int[][] gDp) {
if (gDp[l][r] != -1) {
return gDp[l][r];
}
int ans = 0;
if (l != r) {
int p1 = f(arr, l + 1, r, fDp, gDp);
int p2 = f(arr, l, r - 1, fDp, gDp);
ans = Math.min(p1, p2);
}
gDp[l][r] = ans;
return ans;
}
private static int f(int[] arr, int l, int r, int[][] fDp, int[][] gDp) {
if (fDp[l][r] != -1) {
return fDp[l][r];
}
int ans = 0;
if (l == r) {
ans = arr[l];
}else{
int p1 = arr[l] + g(arr, l + 1, r, fDp, gDp);
int p2 = arr[r] + g(arr, l, r - 1, fDp, gDp);
ans = Math.max(p1, p2);
}
fDp[l][r] = ans;
return ans;
}
}
static class DP {
public static int win(int[] arr) {
if (arr.length == 0 || arr == null) {
@ -116,12 +196,56 @@ public class CardsInLine {
gDp[l][r] = Math.min(fDp[l + 1][r], fDp[l][r - 1]);
l++;
r++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(fDp[i][j]);
System.out.print(",");
}
System.out.println();
}
System.out.println("--------");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(gDp[i][j]);
System.out.print(",");
}
System.out.println();
}
return Math.max(fDp[0][n - 1], gDp[0][n - 1]);
}
}
static class DP1 {
public static int win(int[] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
int n = arr.length;
int[][] fDp = new int[n][n];
int[][] gDp = new int[n][n];
for (int i = 0; i < n; i++) {
fDp[i][i] = arr[i];
}
for (int i = 1; i < n; i++) {
int row = 0;
int col = i;
while (col < n) {
fDp[row][col] = Math.max(arr[row] + gDp[row + 1][col], arr[col] + gDp[row][col - 1]);
gDp[row][col] = Math.min(fDp[row + 1][col], fDp[row][col - 1]);
row++;
col++;
}
}
return Math.max(fDp[0][n - 1], gDp[0][n - 1]);
}
}
public static void main(String[] args){
@ -129,10 +253,10 @@ public class CardsInLine {
int range = 60;
int[] arr = randomArray(maxSize, range);
System.out.println(Recursion.win(arr));
System.out.println(RecursionDp.win(arr));
System.out.println(DP.win(arr));
// arr = new int[]{9, 8, 3, 5, 2};
System.out.println(Recursion1.win(arr));
System.out.println(RecursionDp1.win(arr));
System.out.println(DP1.win(arr));
}
public static int[] randomArray(int maxSize, int range) {

@ -7,6 +7,18 @@ import java.awt.event.KeyEvent;
* @ClassName RobotWalk
* @DATE 2021/1/5 4:27
* @Description
* , start,
* aim,k,
* ?
*
*
* N1~NN 2
* M(M 1~N )
* 12
* N N-1
*
* K P(P1~N)
* NMKP
*/
public class RobotWalk {
@ -58,6 +70,29 @@ public class RobotWalk {
}
static class Recursion1 {
public static int walk(int N, int start, int aim, int k) {
if (N < 2 || start > N || start < 1 || aim > N || aim < 1 || k < 1) {
return -1;
}
return process(start, k, aim, N);
}
private static int process(int cur, int rest, int aim, int n) {
if (rest == 0) {
return aim == cur ? 1 : 0;
}
if (cur == 1) {
return process(2, rest - 1, aim, n);
}
if (cur == n) {
return process(n - 1, rest - 1, aim, n);
}
return process(cur - 1, rest - 1, aim, n) + process(cur + 1, rest - 1, aim, n);
}
}
/**
* ()
*/
@ -94,6 +129,40 @@ public class RobotWalk {
}
}
static class RecursionDp1 {
public static int walk(int n,int start,int aim,int k) {
if (n < 2 || start > n || start < 2 || aim < 2 || aim > n || k < 1) {
return -1;
}
int[][] dp = new int[n + 1][k + 1];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
dp[i][j] = -1;
}
}
return process(start, k, aim, n, dp);
}
private static int process(int cur, int rest, int aim, int n, int[][] dp) {
if (dp[cur][rest] != -1) {
return dp[cur][rest];
}
int ans = 0;
if (rest == 0) {
ans = cur == aim ? 1 : 0;
} else if (cur == 0) {
ans = process(2, rest - 1, aim, n, dp);
} else if (cur == n) {
ans = process(n - 1, rest - 1, aim, n, dp);
} else {
ans = process(cur + 1, rest - 1, aim, n, dp) + process(cur - 1, rest - 1, aim, n, dp);
}
dp[cur][rest] = ans;
return ans;
}
}
/**
* cur,rest
* ,
@ -117,6 +186,24 @@ public class RobotWalk {
}
}
static class DP1 {
public static int walk(int n, int start, int aim, int k) {
if (n < 2 || start > n || start < 1 || aim > n || aim < 1 || k < 1) {
return -1;
}
int[][] dp = new int[n + 1][k + 1];
dp[aim][0] = 1;
for (int rest = 1; rest <= k; rest++) {
dp[1][rest] = dp[2][rest - 1];
for (int cur = 2; cur < n; cur++) {
dp[cur][rest] = dp[cur - 1][rest - 1] + dp[cur + 1][rest - 1];
}
dp[n][rest] = dp[n - 1][rest - 1];
}
return dp[start][k];
}
}
public static class walkMain {
public static void main(String[] args){
@ -124,9 +211,9 @@ public class RobotWalk {
int start = 2;
int aim = 4;
int k = 6;
System.out.println(Recursion.walk(N, start, aim, k));
System.out.println(RecursionDp.walk(N, start, aim, k));
System.out.println(DP.walk(N, start, aim, k));
System.out.println(Recursion1.walk(N, start, aim, k));
System.out.println(RecursionDp1.walk(N, start, aim, k));
System.out.println(DP1.walk(N, start, aim, k));
}

@ -0,0 +1,10 @@
package leo.class19;
/**
* @author Leo
* @ClassName ConvertToLetterString
* @DATE 2021/1/7 5:19
* @Description
*/
public class ConvertToLetterString {
}

@ -0,0 +1,10 @@
package leo.class19;
/**
* @author Leo
* @ClassName Knapsack
* @DATE 2021/1/6 9:43
* @Description
*/
public class Knapsack {
}

@ -0,0 +1,10 @@
package leo.class19;
/**
* @author Leo
* @ClassName StickersToSpellWord
* @DATE 2021/1/7 5:52
* @Description
*/
public class StickersToSpellWord {
}
Loading…
Cancel
Save