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
class Solution { | |
public int numSquares(int n) { | |
int[] dp = new int[n + 1]; //dp[i] - the least number of perfect square number of i | |
for (int i = 1; i <= n; i++) { | |
dp[i] = Integer.MAX_VALUE; | |
} | |
for (int i = 0; i <= n; i++) { | |
for (int j = 1; i + j * j <= n; j++) { | |
dp[i + j * j] = Math.min(dp[i] + 1, dp[i + j * j]); | |
} | |
} | |
return dp[n]; | |
} | |
} |
报名参加比赛,然后不做题,排名竟然会倒退!!!
没有评论:
发表评论