From 0fa6af1a861aff72caff6a58a289656ddd839b63 Mon Sep 17 00:00:00 2001 From: Alex Xie Date: Sat, 2 Nov 2019 18:14:45 -0700 Subject: [PATCH 1/2] [BS] Use binary search to check num is 'perfect square' --- DP/PerfectSquares.swift | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/DP/PerfectSquares.swift b/DP/PerfectSquares.swift index 40317b75..f0fa80fe 100644 --- a/DP/PerfectSquares.swift +++ b/DP/PerfectSquares.swift @@ -1,28 +1,29 @@ /** * Question Link: https://leetcode.com/problems/perfect-squares/ - * Primary idea: Dynamic Programming, transition function is - * nums[i] = min(nums[i], nums[i - j * j] + 1) - * Time Complexity: O(n^2), Space Complexity: O(n) + * Primary idea: Binary Search, base on the num itself, from 0 to num, divide the search by half each time + * + * Time Complexity: O(logn), Space Complexity: O(1) */ class PerfectSquares { func numSquares(n: Int) -> Int { - guard n > 0 else { - return 0 + if num == 1 { + return true } - - var leastNums = [Int](count: n + 1, repeatedValue: Int.max) - leastNums[0] = 0 - - for i in 1...n { - for j in 1...i { - if j * j > i { - break - } - leastNums[i] = min(leastNums[i], leastNums[i - j * j] + 1) + + var l = 0, r = num / 2 + while l <= r { + let mid = l + (r - l) / 2 + let tmp = mid * mid + if tmp == num { + return true + } else if tmp < num { + l = mid + 1 + } else { + r = mid - 1 } } - - return leastNums[n] + + return false } -} \ No newline at end of file +} From 7af6269864e6946fb0e765f259e1270d1ed8a45c Mon Sep 17 00:00:00 2001 From: Alex Xie Date: Sat, 2 Nov 2019 18:23:01 -0700 Subject: [PATCH 2/2] [Search] Update PerfectSquares.swift file location and README --- README.md | 2 +- {DP => Search}/PerfectSquares.swift | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {DP => Search}/PerfectSquares.swift (100%) diff --git a/README.md b/README.md index e96ef029..c91b57d9 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,6 @@ [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)| [Swift](./DP/LongestIncreasingSubsequence.swift)| Medium| O(nlogn)| O(n)| [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)| [Swift](./DP/PalindromicSubstrings.swift)| Medium| O(n^2)| O(n^2)| [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)| [Swift](./DP/LongestPalindromicSubstring.swift)| Medium| O(n^2)| O(n^2)| -[Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Swift](./DP/PerfectSquares.swift)| Medium| O(n^2)| O(n)| [House Robber](https://leetcode.com/problems/house-robber/)| [Swift](./DP/HouseRobber.swift)| Easy| O(n)| O(1)| [House Robber II](https://leetcode.com/problems/house-robber-ii/)| [Swift](./DP/HouseRobberII.swift)| Medium| O(n)| O(1)| [Paint Fence](https://leetcode.com/problems/paint-fence/)| [Swift](./DP/PaintFence.swift)| Easy| O(n)| O(n)| @@ -343,6 +342,7 @@ [Sqrt(x)](https://leetcode.com/problems/sqrtx/)| [Swift](./Search/Sqrtx.swift)| Medium| O(logn)| O(1)| [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)| [Swift](./Search/MedianTwoSortedArrays.swift)| Hard| O(log(m + n))| O(1)| [Minimize Max Distance to Gas Station](https://leetcode.com/problems/minimize-max-distance-to-gas-station/)| [Swift](./Search/MinimizeMaxDistanceGasStation.swift)| Hard| O(nlogm)| O(1)| +[Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Swift](./Search/PerfectSquares.swift)| Medium| O(logn)| O(1)| ## Sort | Title | Solution | Difficulty | Time | Space | diff --git a/DP/PerfectSquares.swift b/Search/PerfectSquares.swift similarity index 100% rename from DP/PerfectSquares.swift rename to Search/PerfectSquares.swift