python - 寻找最不完美二乘的 BFS 解决方案
问题描述
我正在研究一个完美的正方形 - LeetCode
- 完美的正方形
给定一个正整数n
1, 4, 9, 16, ...
,找出总和为n的最小完美平方数(例如) 。示例 1:
Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4.
示例 2:
Input: n = 13 Output: 2 Explanation: 13 = 4 + 9.
关键思想将其转换为在图中找到从 n 到 0 的最短路径
标准 DFS 解决方案
class Solution:
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
from collections import deque
#declare
queue = deque()
visited = set()
#intitiate
step = -1
queue.append(n)
visited.add(n)
while queue:
step += 1
size = len(queue)
for _ in range(size):
cur = queue.popleft()
if cur == 0: return step #terminating checking
#strech to collect next nodes
i = 1
next_ = cur - i**2 #
while next_ >= 0:
if next_ not in visited:
queue.append(next_)
visited.add(next_)
i += 1
next_ = cur - i**2
运行时间:2532 毫秒,比 Perfect Squares 的 Python3 在线提交的 40.71% 快。内存使用:14 MB,不到 Perfect Squares 的 Python3 在线提交的 18.25%。
收集下一个节点的部分不是很简洁
#strech to collect next nodes
i = 1
next_ = cur - i**2 #
while next_ >= 0:
if next_ not in visited:
queue.append(next_)
visited.add(next_)
i += 1
next_ = cur - i**2
试图将其修改为
i = 1
while cur - i** 2 >= 0:
next_ = cur - i ** 2:
if next_ not in visited:
queue.append(next_)
visited.add(next_)
i += 1
它有效,但超过了时间限制。
如何重构那部分?
解决方案
我认为原因TLE
是你做cur - i** 2
了两次,square
在这里很昂贵。我更改为cur - i * i
,它通过了。
在大多数情况下,双倍计算不会导致TLE
,但DFS
这里足够慢(2560ms
我的测试中的成本),所以它在乎。
如果不想赋值next_
两次,而python比较不支持语法赋值,像这样:
while (next_ = cur - i**2) >= 0:
所以你可以试试这个(我认为这也很丑):
i = 1
while True:
next_ = cur - i ** 2
if next_ < 0:
break
if next_ not in visited:
queue.append(next_)
visited.add(next_)
i += 1
顺便说一句,我只是注意到它与 无关BFS
,并且BFS
是解决此问题的更快解决方案。
推荐阅读
- node.js - eslint 导入导出错误
- google-apps-script - 来自另一个电子表格的动态下拉列表
- salesforce - 使用 tSalesForceConnection 组件从 Talend-SalesForce 连接时的时间延迟
- javascript - 带有多个等待或承诺的循环
- python - 使用 tkinter 返回文件路径
- vbscript - 可以在 .wsh 文件中指定哪些选项?
- keycloak - 使用 Keyclock:如何为为两家公司工作的单个用户定义不同的角色集?
- python - 在 Python 中定义用于调用 CPLEX 的目录
- keras - 如何在 keras 中保存经过训练的神经网络的偏差?
- apache-kafka - 如何使用 Kafka Streams 统计在特定时间段内生成事件的用户?