Python 找到到达阵列末端的最小成本
给出了一系列成本。你可以向前跳两次,也可以向后跳一次。如果你在一个特定的指数上着陆,你必须将成本加到总成本中。找到穿过阵列或到达阵列末端所需的最小成本 输入:Python 找到到达阵列末端的最小成本,python,c++,algorithm,data-structures,dynamic-programming,Python,C++,Algorithm,Data Structures,Dynamic Programming,给出了一系列成本。你可以向前跳两次,也可以向后跳一次。如果你在一个特定的指数上着陆,你必须将成本加到总成本中。找到穿过阵列或到达阵列末端所需的最小成本 输入: 5 (Number of elements in the array) [9,4,6,8,5] (Array) 1 (Index to start) 12 输出: 5 (Number of elements in the array) [9,4,6,8,5] (Array) 1 (Index to start) 12 说明
5 (Number of elements in the array)
[9,4,6,8,5] (Array)
1 (Index to start)
12
输出:
5 (Number of elements in the array)
[9,4,6,8,5] (Array)
1 (Index to start)
12
说明:我们从索引1开始,跳转到3,然后跳转,总成本为8+4=12
我们如何为这个问题构建DP解决方案?您可以使用Dijkstra算法(图)来解决这个问题 遵循以下步骤: 1.通过将第个索引的节点与第(i-1)个和第(i+2)个索引的节点及其成本(如果可能)连接,生成加权有向图
2.应用Dijkstra算法找到初始节点(索引)和目标节点(索引)之间的最短路径您可以使用带Dp的递归程序
//cur will the final destination that is last element at first execution
//N is no of elements
int Dp[N]={0};
Dp[0]=element[0]; //initial condition
least_path(cur,*elements,N)
{
if(cur>N-1 || cur<0)
return INT_MAX;
if(Dp[cur])
return Dp[cur];
int temp1=least_path(cur-2,*elements,N)+element[cur];
int temp2=least_path(cur+1,*elements,N)+element[cur];
Dp[cur]=min(temp1,temp2);
return Dp[cur];
}
//cur将是第一次执行时的最后一个元素的最终目标
//N是元素的数目
int-Dp[N]={0};
Dp[0]=元素[0]//初始条件
最小路径(cur,*元素,N)
{
if(cur>N-1 | | curC++解决方案(dijikstras)
int mindsist(向量与访问、向量与距离、int n){
int m=int_MAX,res=0;
对于(int i=0;idist[i])
m=dist[i],res=i;
返回res;
}
int-minJump(int*arr,int-n){
向量访问(n,假);
向量距离(n,INT_MAX);
距离[0]=0;
对于(int c=0;c=0&&!访问了[u-1]&&dist[u]+arr[u-1]
您可以使用以下代码:
#包括
使用名称空间std;
int main()
{
int n=3;
布尔标志=真;
while(n)
{
//取最后两位,然后右移
int i=n%2;
n=n/2;
int j=n%2;
n=n/2;
//仅允许使用给定的模式01和10进行检查
if((i==0&&j==1)| |(i==1&&j==0))
{
//正确的模式
}
否则{
//不准
flag=false;
打破
}
}
国际单项体育联合会(旗)
cout我使用了path
来表示迄今为止访问的所有节点
res
表示到目前为止的结果
import sys
infi = sys.maxsize
def optimum_jump_recurse(arr, curr_pos, cost, path, res):
if curr_pos in path or cost > res or curr_pos < 0:
return infi
elif curr_pos > len(arr) - 1:
if cost < res:
res = cost
return cost
res = optimum_jump_recurse(arr, curr_pos + 2, cost + arr[curr_pos],
path | {curr_pos}, res)
backward_cost = optimum_jump_recurse(arr, curr_pos - 1,
cost + arr[curr_pos],
path | {curr_pos}, res)
if res == backward_cost == infi:
return res
res = min(res, backward_cost)
actual_cost = res - cost
return res
nums = [1, 2, 3, 4, 100]
# nums = [1, 2, 3]
# nums = [1]
# nums = [1, 2]
# nums = [1, 2, 3, 100, 200]
# nums = [1, 1000, 3, 100, 200]
# nums = [1, 1, 3, 100, 200]
# nums = [1, 1, 3, 5, 100, 200]
# nums = [1, 2, 3, 100, 4]
def optimum_jump(nums):
return optimum_jump_recurse(nums, 0, 0, set(), infi)
print(optimum_jump(nums))
导入系统
infi=sys.maxsize
定义最佳跳转递归(arr、curr\u pos、cost、path、res):
如果路径或成本中的curr\u pos>res或curr\u pos<0:
返回infi
elif curr_pos>len(arr)-1:
如果成本
基于DP的解决方案-以自下而上的方式填充一维成本阵列。包括两个步骤:
在达到指数i时,检查并更新成本,如果我们可以通过从指数i+1达到它来改进成本
在i+2更新到达要素的成本
时间复杂度:O(n),其中n是输入数组的长度
# Python3 snippet
# Inputs: startIndex, array A of non negative integers
# Below snippet assumes A has min length 3 beginning from the startIndex. We can handle smaller arrays with base cases - thus, not listing them out explicitly
import math
costs = [math.inf] * len(A)
costs[startIndex] = A[startIndex]
costs[startIndex+1] = A[startIndex] + A[startIndex+1] + min(A[startIndex+1], A[startIndex-1] if startIndex >=0 else A[startIndex+1])
N = len(A)
for i in range(startIndex, N):
if i+1 < N:
costs[i] = min(costs[i], costs[i+1] + A[i])
if i + 2 < N:
costs[i+2] = costs[i] + A[i+2]
print(min(costs[-1], costs[-2]))
#Python3片段
#输入:startIndex,非负整数数组
#下面的代码段假设从startIndex开始的最小长度为3。我们可以使用基本情况处理较小的数组-因此,不显式地列出它们
输入数学
成本=[math.inf]*len(A)
成本[startIndex]=A[startIndex]
成本[startIndex+1]=A[startIndex]+A[startIndex+1]+min(A[startIndex+1],如果startIndex>=0,则A[startIndex-1],否则A[startIndex+1])
N=len(A)
对于范围内的i(起始索引,N):
如果i+1
以下代码将用于获取索引0到N-1的最小成本
int[] dp;
boolean[] visited;
int func(int pos, int N, int[] cost){
if(pos == 0) return 0;
if(dp[pos] != -1) return dp[pos];
visited[pos] = true;
int dist = Integer.MAX_VALUE;
if(pos-2 > -1 && !visited[pos-2]) dist = Math.min(dist, cost[pos-2] + func(pos-2,N,cost));
if(pos+1 < N && !visited[pos+1]) dist = Math.min(dist, cost[pos+1] + func(pos+1,N,cost));
return dp[pos] = dist;
}
int min_cost_to_reach_the_end(int[] cost){
int N = cost.length;
this.dp = new int[N];
this.visited = new boolean[N];
Arrays.fill(dp,-1);
return func(N-1, N, cost);
}
int[]dp;
曾到访,;
int func(int pos,int N,int[]成本){
如果(pos==0)返回0;
如果(dp[pos]!=-1)返回dp[pos];
访问[pos]=真;
int dist=整数最大值;
如果(pos-2>-1&&!已访问[pos-2])距离=数学最小值(距离,成本[pos-2]+函数(pos-2,N,成本));
如果(pos+1
我知道很晚了,但请检查一下
public class MinimumCost {
public static void main(String ar[]){
int arr[]={9,4,6,8,5};
int startIndex=1;
int DP[]=new int[arr.length];
for(int i=0;i<arr.length;i++){
DP[i]=100000;//I just take this as maximum value .you can change it.
}
DP[startIndex] = arr[startIndex] ;
DP[startIndex+2] = arr[startIndex] + arr[startIndex+2] + Math.min(arr[startIndex+2], arr[startIndex]);
int n=arr.length;
for(int i=0;i<n-1;i++){
if ((i+1)<n)
DP[i] = Math.min(DP[i], (DP[i+1]) + arr[i]);
if ((i + 2 )< n)
DP[i+2] = DP[i] + arr[i+2];
}
System.out.println(Math.min(DP[arr.length-1],DP[arr.length-2]));
}
公共类最低成本{
公共静态void main(字符串ar[]{
int arr[]={9,4,6,8,5};
int startIndex=1;
int DP[]=新int[arr.length];
对于(inti=0;iJAVA解决方案:解决方案需要O(n2)
int minCost[]=新的int[N];
填充(最小成本、整数、最大值);
对于(int i=0;i函数解算(
function solve(A_i) {
A_i.sort((a,b) => a-b);
let totalcost = A_i[0];
for(let i=2; i<A_i.length;i++) {
if(i + 1 == A_i.length -1) {
totalcost = totalcost + A_i[i];
break;
}
const preSum = A_i[i] + A_i[i+2];
const postSum = A_i[i] + A_i[i-1] + A_i[i+1];
if(preSum <= postSum) {
totalcost = totalcost + A_i[i];
i++;
} else {
totalcost = totalcost + A_i[i];
i = i-2;
console.log(i)
}
}
return totalcost;
}
long getJumpCost(int arr[]) {
int length = arr.length;
long dp[] = new long[length];
for(int i=0; i<length; i++) {
dp[i] = Long.MAX_VALUE;
}
dp[0] = arr[0];
dp[2] = dp[0] + (long)arr[2];
dp[1] = dp[2] + (long)arr[1];
for(int i=3; i<length; i++) {
dp[i] = Math.min(dp[i], dp[i-2] + (long)arr[i]);
dp[i-1] = Math.min(dp[i-1], dp[i] + (long)arr[i-1]);
}
long result = Math.min( dp[length-1], dp[length-2]);
return result;
}