我被要求建立一种算法,包括置换和我有点难倒,并寻找一个首发位置。详细是这里...
正在攀登的 N 的楼梯楼梯。每次你可以爬1或2个步骤的时间。你有多少不同的方式可以爬上顶端?
任何建议,我怎么能解决这个难题?
解决方案你错排列。置换涉及到一组排序。这个问题是另一回事。
涉及该产生同样的问题的另一个实例的简单决定的问题是经常可解通过动态规划。这是这样的问题。
当您有n个台阶要爬,你可以选择1或2级跳,进而解决小问题,对于n-1和n-2步分别。在这种情况下,你要添加的可能性号码。 (许多移民都找到最低价或最大值来代替,所以这是一个有点不寻常。)
基本情况是当你有0或1的一步。有确切1的方法来遍历所有这些。
对于所有考虑到这一点,我们就可以各种方法爬上n步作为一个递归EX pression数写这个充满活力的方案:
W(N)= W(N - 1)+ W(N - 2)如果n> 1
1N的== 0,1
现在你不想要实现这是一个简单的递归函数。这需要时间,指数在n来计算,因为每次调用为W自称两次。然而,大多数这些电话都是不必要的重复。怎么办?
要完成工作的方法之一是找到一种方法来计算W(我)的序列值。对于1值DP它通常很简单,所以它是在这里:
W(0)= 1
W(1)= 1(从基部壳体)
W(2)= W(1)+ W(0)= 1 + 1 = 2
W(3)= W(2)+ W(1)= 2 + 1 = 3
您的想法。这是一个非常简单的DP确实如此。为了计算W(i)中,我们只需要两个previous值,W(I-1)和W(I-2)。一个简单的O(n)的循环会做的伎俩。
作为一个全面的检查,看看W(3)= 3。事实上,去了3个步骤,我们可以采取1,然后2跳,2然后1,或1这3种方式三跳!
无法抗拒多一个? W(4)= 2 + 3 = 5。的跳的序列是(2,2),(2,1,1),(1,2,1),(1,1,2),和(1,1,1,1):5种方法
其实,上面的图表将看起来很熟悉了许多。方式爬n步数为第(n + 1)个斐波纳契数。你应该$ C C自己的循环$。如果卡住了,你可以查找任何动辄上贴出例子。
I've been asked to build an algorithm that involves a permutation and I'm a little stumped and looking for a starting place. The details are this...
You are climbing a staircase of n stairs. Each time you can either climb 1 or 2 steps at a time. How many distinct ways can you climb to the top?
Any suggestions how I can tackle this challenge?
解决方案You're wrong about permutations. Permutations involve orderings of a set. This problem is something else.
Problems involving simple decisions that produce another instance of the same problem are often solvable by dynamic programming. This is such a problem.
When you have n steps to climb, you can choose a hop of either 1 or 2 steps, then solve the smaller problems for n-1 and n-2 steps respectively. In this case you want to add the numbers of possibilities. (Many DPs are to find minimums or maximums instead, so this is a bit unusual.)
The "base cases" are when you have either 0 or 1 step. There's exactly 1 way to traverse each of these.
With all that in mind, we can write this dynamic program for the number of ways to climb n steps as a recursive expression:
W(n) = W(n - 1) + W(n - 2) if n > 1
1 n == 0, 1
Now you don't want to implement this as a simple recursive function. It will take time exponential in n to compute because each call to W calls itself twice. Yet most of those calls are unnecessary repeats. What to do?
One way to get the job done is find a way to compute the W(i) values in sequence. For a 1-valued DP it's usually quite simple, and so it is here:
W(0) = 1
W(1) = 1 (from the base case)
W(2) = W(1) + W(0) = 1 + 1 = 2
W(3) = W(2) + W(1) = 2 + 1 = 3
You get the idea. This is a very simple DP indeed. To compute W(i), we need only two previous values, W(i-1) and W(i-2). A simple O(n) loop will do the trick.
As a sanity check, look at W(3)=3. Indeed, to go up 3 steps, we can take hops of 1 then 2, 2 then 1, or three hops of 1. That's 3 ways!
Can't resist one more? W(4)=2+3=5. The hop sequences are (2,2), (2,1,1), (1,2,1), (1,1,2), and (1,1,1,1): 5 ways.
In fact, the chart above will look familiar to many. The number of ways to climb n steps is the (n+1)th Fibonacci number. You should code the loop yourself. If stuck, you can look up any of the hundreds of posted examples.