Last active
September 9, 2015 18:11
-
-
Save junjiah/bea675c1cf3ae78c296a to your computer and use it in GitHub Desktop.
solved 'Perfect Squares' on LeetCode https://leetcode.com/problems/perfect-squares/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Runtime: 200ms. Typical DP. | |
class Solution { | |
public: | |
int numSquares(int n) { | |
vector<int> perfect_records(n + 1, 0); | |
for (int i = 1; i <= n; ++i) { | |
int min_to_perfect = INT_MAX; | |
for (int j = 1; i - j * j >= 0; ++j) { | |
if (perfect_records[i - j * j] < min_to_perfect) { | |
min_to_perfect = perfect_records[i - j * j]; | |
} | |
} | |
perfect_records[i] = min_to_perfect + 1; | |
} | |
return perfect_records.back(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment