- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题陈述:
对于正整数,您可以执行以下 3 个步骤中的任何一个。
从中减去 1。 ( n = n - 1 )
如果它能被 2 整除,则除以 2。(如果 n % 2 == 0 ,则 n = n/2 )
如果它能被 3 整除,则除以 3。(如果 n % 3 == 0,则 n = n/3)
给定一个正整数 n,您的任务是找到使 n 等于 1 的最少步数。
我的递归解决方案(在 C++ 中)比较了 N 可以被 3 整除的所有 3 种情况,而一般解决方案只比较 2,但仍然给出了正确的解决方案。
int min_steps(int N){
if(N==1) return 0;
else{
if(N%3==0){
if(N%2==0)
return (1+min(min_steps(N/3),min_steps(N/2),min_steps(N-1)));
else
return(1+min(min_steps(N/3),min_steps(N-1)));
}
else if(N%2==0){
return(1+min(min_steps(N/2),min_steps(N-1)));
}
else
return(1+min_steps(N-1));
}
}
但一般的解决办法是,
int min_steps(int N){
if(N==1) return 0;
else{
if(N%3==0){
return(1+min(min_steps(N/3),min_steps(N-1)));
}
else if(N%2==0){
return(1+min(min_steps(N/2),min_steps(N-1)));
}
else
return(1+min_steps(N-1));
}
}
我的问题是,为什么我们不比较所有 3 种情况,但仍然得出正确的解决方案。我无法遵循通用解决方案的算法。任何让我理解的帮助将不胜感激。
最佳答案
“通用解决方案”不正确。有时最好先除以 2 再减去 1,但通用解决方案代码不允许这样做。
“通用解决方案”为 642 产生了错误的结果。
642 -> 214 -> 107 -> 106 -> 53 -> 52 -> 26 -> 13 -> 12 -> 4 -> 2 -> 1
但是,这是最佳的,因为它更短:
642 -> 321 -> 320 -> 160 -> 80 -> 40 -> 20 -> 10 -> 9 -> 3 -> 1
您可以看到一般解从除以 3 开始,最优解从除以 2 然后减去 1...这正是被删除的情况。
虽然它与您的问题没有直接关系,但这是我用来查找反例的代码(尽管自从我编写它以来已经整理得很好)。它使用您提供的两种算法,但会记住它们以实现指数级速度增长。它还使用了从 min_steps 返回两个结果的技巧:不仅是最短路径的长度,还有该路径的第一步。这使得在不编写太多额外代码的情况下重建路径非常方便。
def memoize(f):
"""Simple memoization decorator"""
def mf(n, div2, cache={}):
if (n, div2) not in cache:
cache[n, div2] = f(n, div2)
return cache[(n, div2)]
return mf
@memoize
def min_steps(n, div2):
"""Returns the number of steps and the next number in the solution.
If div2 is false, the function doesn't consider solutions
which involve dividing n by 2 if n is divisible by 3.
"""
if n == 1:
return 0, None
best = min_steps(n - 1, div2)[0] + 1, n-1
if n % 3 == 0:
best = min(best, (min_steps(n // 3, div2)[0] + 1, n//3))
if n % 2 == 0 and (div2 or n%3):
best = min(best, (min_steps(n // 2, div2)[0] + 1, n//2))
return best
def path(n, div2):
"""Generates an optimal path starting from n.
The argument div2 has the same meaning as in min_steps.
"""
while n:
yield n
_, n = min_steps(n, div2)
# Search for values of n for which the two methods of finding
# an optimal path give different results.
for i in xrange(1, 1000):
ms1, _ = min_steps(i, True)
ms2, _ = min_steps(i, False)
if ms1 != ms2:
print i, ms1, ms2
print ' -> '.join(map(str, path(i, True)))
print ' -> '.join(map(str, path(i, False)))
这是输出,包括运行时间:
$ time python minsteps.py
642 10 11
642 -> 321 -> 320 -> 160 -> 80 -> 40 -> 20 -> 10 -> 9 -> 3 -> 1
642 -> 214 -> 107 -> 106 -> 53 -> 52 -> 26 -> 13 -> 12 -> 4 -> 2 -> 1
643 11 12
643 -> 642 -> 321 -> 320 -> 160 -> 80 -> 40 -> 20 -> 10 -> 9 -> 3 -> 1
643 -> 642 -> 214 -> 107 -> 106 -> 53 -> 52 -> 26 -> 13 -> 12 -> 4 -> 2 -> 1
real 0m0.009s
user 0m0.009s
sys 0m0.000s
关于c++ - 算法的正确性和逻辑 : minimum steps to one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30029581/
我是 Haskell 术语的初学者。我必须做一个显示所有最低位置的练习。 例如: [1,2,3,1,1] => 0,3,4 这些是最小位置。 我尝试用两种方法来做到这一点,但这些都不起作用。 请有人帮
我需要找到整个矩阵的最小值,它是“坐标”。在像 这样的矩阵中 matrix = 8 7 6 5 4 3 2 1 最小值为 (2, 4) 处的 1。 最佳答案 这可以很简单地通过使用
给定一个正整数“l”和“r”。找到最小的数字“n”,使得 l r: cnt = 32 for i in range(l, r+1): s = bi
numpy.minimum 似乎不适用于复数: np.minimum(5+3*1j,4+30*1j) (4+30j) 我想保持最大幅度的值。它只比较实部。元素最小比较的任何其他功能? MATLAB m
鉴于数据库中的以下事实: foo(a, 3). foo(b, 2). foo(c, 4). foo(d, 3). foo(e, 2). foo(f, 6). foo(g, 3). foo(h, 2).
假设我们给出了给定图 G 的最小生成树 T(有 n 个顶点和 m 个边)和一条权重为 w 的新边 e = (u, v),我们将添加到 G 中。 I) 检查 T 是否仍然是 MST。II) 如果不是,请
我有一个名为“posts”的elasticsearch索引。 http://127.0.0.1:9200/posts/doc/_count返回{"count":240000,"_shards":{"t
我在 MySQL 中有一个表,名称如下 我有两件事要处理 1- 停用所有未使用的书籍 isActive = 1 - Active isActive = 0 - Inactive is_inuse =
我的站点位于 www.ethoma.com/wd/ . 如您所见,我已经使用自己的代码实现了主题和所有菜单。我想在我的网站上安装 WordPress,这样我就可以简单地在 WordPress 上输入我
当我在 bool 数组上使用 numpy 函数 minimum() 和 maximum() 时,结果类型打印为 numpy.int32。但是,与 numpy.int32 类型的比较失败(即使在转换之后
我在分布式系统中遇到分片移动问题。 【问题】 最初每个分区负责任意数量的分片。 (这个数字可以是任意的,因为系统支持将分片从一个分区移动到另一个分区) 然后一个新的分区来了,系统需要重新分片。目标是使
我有 3 个这样的观点: 我需要定义一个约束,以便在蓝色或芥末色垂直调整大小时,红色 View 将保持在任一上 View 的最小距离处,例如 或者 那么我怎样才能达到那个结果呢??? 最佳答案 建立从
题目地址:https://leetcode.com/problems/minimum-area-rectangle/description/ 题目描述 Given a set of points
题目地址:https://leetcode-cn.com/problems/path-with-minimum-effort/ 题目描述 你准备参加一场远足活动。给你一个二维 rows x col
题目地址:https://leetcode.com/problems/minimum-absolute-difference/ 题目描述 Given an array of distinct in
题目地址:https://leetcode.com/problems/minimum-height-trees/description/ 题目描述 Fora undirected graph wi
题目地址:https://leetcode.com/problems/minimum-time-difference/description/ 题目描述: Given a list of 24-h
题目地址: https://leetcode.com/problems/minimum-genetic-mutation/description/ 题目描述 Agene string can be
你们能帮我解决一些我被困的家庭作业问题吗? 完整二叉树中的局部最小值被定义为小于其所有邻居(邻居 = 父、左子、右子)的节点。 我需要在给定的完整二叉树中找到一个局部最小值,它的每个节点都有不同的数字
(defun *smaller* (x y) ( if (> x y) y x)) (defun *minimum* (lst) (do ((numbers lst (cdr
我是一名优秀的程序员,十分优秀!