master
ehlxr 2021-10-31 23:22:24 +08:00
parent 947d81a5b5
commit d51106271e
1 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,65 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.ehlxr.algorithm;
/**
* arr 使
*
* https://cdn.jsdelivr.net/gh/0vo/oss/images/dp.jpg
*
* @author ehlxr
* @since 2021-10-31 22:46.
*/
public class DynamicProgramming {
public static int recOpt(int[] arr, int i) {
if (i == 0) {
return arr[0];
} else if (i == 1) {
return Math.max(arr[0], arr[1]);
} else {
int a = recOpt(arr, i - 2) + arr[i];
int b = recOpt(arr, i - 1);
return Math.max(a, b);
}
}
public static int dpOpt(int[] arr) {
int[] opt = new int[arr.length];
opt[0] = arr[0];
opt[1] = Math.max(arr[0], arr[1]);
for (int i = 2; i < opt.length; i++) {
int a = opt[i - 2] + arr[i];
int b = opt[i - 1];
opt[i] = Math.max(a, b);
}
return opt[arr.length - 1];
}
public static void main(String[] args) {
int[] arr = {4, 1, 1, 9, 1};
System.out.println(dpOpt(arr));
System.out.println(recOpt(arr, arr.length - 1));
}
}