添加 'datastructure/src/main/java/org/alis/algorithm/leetcode/LeetCode20.java'

master
alis-lc 9 months ago
parent 82c3e991ad
commit 8c57d19940

@ -0,0 +1,35 @@
package org.alis.algorithm.leetcode;
import java.util.Arrays;
/**
*
*
* @author lc
* @date 2024/2/18 16:44
**/
public class LeetCode20 {
/**
*
*/
// private static final int[] array = new int[]{1, 2, 4, 5, 10, 11, 12};
public static void main(String[] args) {
int[] array = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
System.out.println(goEnd(array));
}
public static int goEnd(int[] array) {
// 开始计算孩子需要的时间
if (array.length <= 2) {
return array[array.length - 1];
}
int[] opt = Arrays.copyOf(array, array.length);
for (int i = 2; i < array.length; i++) {
opt[i] = Math.min(opt[i - 1] + array[0] + array[i], opt[i - 2] + array[0] + array[i] + 2 * array[1]);
}
return opt[array.length-1];
}
}
Loading…
Cancel
Save