递归 Fib 风格函数变成迭代函数?

发布于 2024-12-08 13:22:59 字数 1127 浏览 0 评论 0原文

我在java中有一个函数,它是用递归算法编写的,需要以迭代形式编写。问题是我不知道从哪里开始思考新的算法。这是我正在做的一项作业。

考虑以下计算问题:

假设您是一名顾问,并且您有一系列 n 个潜在的咨询工作,其报酬为 A[0],A[1],..,A[n -1] 美元(因此工作 0 支付 A[0] 美元,工作 1 支付 A[ 1] 美元等)。

此外,作业 ii (i = 0 ; : : : ; n 1 ) 天开始。

但是,每个工作需要 2 天,因此您不能连续执行两个工作。目标是确定最大金额,用 F(n) 表示;您可以从 n 个职位 A[0]A[n-1] 中选择的有效工作计划中赚取收入 : 例如,考虑以下输入数组:

  0 1 2 3 4 5
A 5 6 8 6 2 4

最佳计划是执行作业 0 ; 2 ;5 ;金额 赚取,F(6) = A[0] + A [2] + A [5] = 17 ;尽可能大。请注意,这 是一个有效的计划,因为不包含两个连续的作业。

解决这个问题的递归版本的函数如下:

public static int jobScheduleRecursive(int[] A, int n) 
{
    n = A.length;
    if(n == 0){return 0;}
    else if(n == 1){return A[0];}
    else if(n >= 2){return max(jobScheduleRecursive(A, (n-1)), (A[n-1]) 
                           + jobScheduleRecursive(A, n-2));}
    else return -1;
}

总之,我必须想出一个迭代算法来完成这项工作。唯一的问题是我不知道如何继续。如果有任何建议能够引导我走向正确的方向,我将不胜感激。

I have a function in java which is written with a recursive algorithm that needs to be written in Iterative form. The thing is that I dont know where to start in wrapping my mind around a new algorithm for this. This is an assignment I am working on.

Consider the following computational problem:

Suppose you work as a consultant and you have a sequence of n potential consulting jobs that pay A[0],A[1],..,A[n-1] dollars, respectively (so job 0 pays A[0] dollars, job 1 pays A[1] dollars, etc.).

Also, job i starts on day i (i = 0 ; : : : ; n 1 ).

However, each job requires 2 days, so you cannot perform any two consecutive jobs. The goal is to determine the maximum amount of money, denoted by F(n) ; you can earn from a valid job schedule selected from the n jobs A[0] through A[n-1]
:
As an example, consider the following input array:

  0 1 2 3 4 5
A 5 6 8 6 2 4

An optimal schedule is to do jobs 0 ; 2 ; and 5 ; for which the amount of money
earned, F(6) = A[0] + A [2] + A [5] = 17 ; is as large as possible. Notice that this
is a valid schedule, as no two consecutive jobs are included.

My function for the recursive version that solves this is as follows:

public static int jobScheduleRecursive(int[] A, int n) 
{
    n = A.length;
    if(n == 0){return 0;}
    else if(n == 1){return A[0];}
    else if(n >= 2){return max(jobScheduleRecursive(A, (n-1)), (A[n-1]) 
                           + jobScheduleRecursive(A, n-2));}
    else return -1;
}

To sum up, I have to come up with an iterative algorithm that does this job. The only problem is that i have no idea how to proceed. I would appreciate any advice to lead me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你搞怪i 2024-12-15 13:22:59

有时,已知问题的递归解决方案的迭代解决方案并不像递归解决方案那样直接。实现迭代解决方案的最简单方法是使用 - 动态编程 基本上你想要的是创建一个临时数组,保存途中所有子问题的解决方案。为此,请根据输入的大小创建一个动态分配的数组。例如,如果您的递归函数是 int foo(int a)
用 1..n 的解决方案填充数组,其中 n 是原始问题的输入。
更改算法,它不再递归地调用自身,而是检查数组中是否存在子问题的解决方案,如果不存在,则将其填充。这样子问题就不会计算次数,而只会计算一次。

sometimes, iterative solutions for a known recursive solution to problems isn't straight forward as the recursive solution. The easiest way to achieve iterative solution is to use - Dynamic Programming basically what you want is to create a temporary array that holds the solution to all sub-problems in the way. to achieve that, create a dynamically allocated array in the size of your input. and if for instance your recursive function is int foo(int a)
fill the array with the solutions to 1..n, where n is the input for your original problem.
change the algorithm, that instead of calling itself recursivelly, it will check if the solution for the sub-problem allready exists in the array, if not, it fills it up. that way a sub-problem won't compute numerus times, but only once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文