【发布时间】:2020-12-12 15:24:57
【问题描述】:
以下是使用树递归方法的问题分配:
最大子序列 数字的子序列是数字的一系列(不一定是连续的)数字。例如,12345 的子序列包括 123、234、124、245 等。您的任务是获取低于一定长度的最大子序列。
def max_subseq(n, l):
"""
Return the maximum subsequence of length at most l that can be found in the given number n.
For example, for n = 20125 and l = 3, we have that the subsequences are
2
0
1
2
5
20
21
22
25
01
02
05
12
15
25
201
202
205
212
215
225
012
015
025
125
and of these, the maxumum number is 225, so our answer is 225.
>>> max_subseq(20125, 3)
225
>>> max_subseq(20125, 5)
20125
>>> max_subseq(20125, 6) # note that 20125 == 020125
20125
>>> max_subseq(12345, 3)
345
>>> max_subseq(12345, 0) # 0 is of length 0
0
>>> max_subseq(12345, 1)
5
"""
"*** YOUR CODE HERE ***"
这个问题有两个关键的见解
- 您需要区分使用个位和不使用的情况。在它是的情况下,我们想要减少
l,因为我们使用了其中一个数字,而在它不是的情况下,我们不这样做。 - 在我们使用个位的情况下,您需要将数字放回末尾,将数字
d附加到数字n的末尾的方法是10 * n + d。
看不懂这个问题的见解,下面提到2点:
-
分为使用个位和不使用个位的情况
-
如果我们使用个位数,你需要把数字放回末尾
我对这个问题的理解:
这个问题的解决方案看起来生成所有子序列到l,伪代码如下:
digitSequence := strconv.Itoa(n) // "20125"
printSubSequence = func(digitSequence string, currenSubSequenceSize int) { // digitSequence is "20125" and currenSubSequenceSize is say 3
printNthSubSequence(digitSequence, currenSubSequenceSize) + printSubSequence(digitSequence, currenSubSequenceSize-1)
}
其中printNthSubSequence 打印(20125, 3) 或(20125, 2) 等的子序列...
在所有这些序列中找到max_subseq 变得很容易
你能通过一个例子帮助我理解这个问题中给出的见解吗(比如20125, 1)? here 是完整的问题
【问题讨论】:
标签: go recursion divide-and-conquer