Skip to content

Commit

Permalink
fix: 复杂度分析
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 committed Mar 21, 2020
1 parent dfe60bf commit 93973a6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions problems/365.water-and-jug-problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ class Solution:

**复杂度分析**

- 时间复杂度:不太好算,大概是指数级别
- 空间复杂度:不太好算,大概是指数级别
- 时间复杂度:由于状态最多有$O((x + 1) * (y + 1))$ 种,因此总的时间复杂度为$O(x * y)$。
- 空间复杂度:我们使用了队列来存储状态,set 存储已经访问的元素,空间复杂度和状态数目一致,因此空间复杂度是$O(x * y)$。

上面的思路很直观,但是很遗憾这个算法在 LeetCode 的表现是 TLE(Time Limit Exceeded)。不过如果你能在真实面试中写出这样的算法,我相信大多数情况是可以过关的。

我们来看一下有没有别的解法。实际上,上面的算法就是一个标准的 BFS。如果从更深层次去看这道题,会发现这道题其实是一道纯数学问题,类似的纯数学问题在 LeetCode 中也会有一些,不过大多数这种题目,我们仍然可以采取其他方式 AC。那么让我们来看一下如何用数学的方式来解这个题。

## 数学法 - 最大公约数

Expand Down

0 comments on commit 93973a6

Please sign in to comment.