摘要:動(dòng)態(tài)規(guī)劃法建立空數(shù)組從到每個(gè)數(shù)包含最少平方數(shù)情況,先所有值為將到范圍內(nèi)所有平方數(shù)的值賦兩次循環(huán)更新,當(dāng)它本身為平方數(shù)時(shí),簡(jiǎn)化動(dòng)態(tài)規(guī)劃法四平方和定理法
Problem
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
ExampleGiven n = 12, return 3 because 12 = 4 + 4 + 4
Given n = 13, return 2 because 13 = 4 + 9
這道題在OJ有很多解法,公式法,遞歸法,動(dòng)規(guī)法,其中公式法時(shí)間復(fù)雜度最優(yōu)(four square theorem)。
不過(guò)我覺(jué)得考點(diǎn)還是在動(dòng)規(guī)吧,也更好理解。
1. 動(dòng)態(tài)規(guī)劃法
public class Solution { public int numSquares(int n) { //建立空數(shù)組dp:從0到n每個(gè)數(shù)包含最少平方數(shù)情況,先f(wàn)ill所有值為Integer.MAX_VALUE; int[] dp = new int[n+1]; Arrays.fill(dp, Integer.MAX_VALUE); //將0到n范圍內(nèi)所有平方數(shù)的dp值賦1; for (int i = 0; i*i <= n; i++) { dp[i*i] = 1; } //兩次循環(huán)更新dp[i+j*j],當(dāng)它本身為平方數(shù)時(shí),dp[i+j*j] < dp[i]+1 for (int i = 0; i <= n; i++) { for (int j = 0; i+j*j <= n; j++) { dp[i+j*j] = Math.min(dp[i]+1, dp[i+j*j]); } } return dp[n]; } }
2. 簡(jiǎn)化動(dòng)態(tài)規(guī)劃法
public class Solution { public int numSquares(int n) { int[] dp = new int[n+1]; for (int i = 0; i <= n; i++) { dp[i] = i; for (int j = 0; j*j <= i; j++) { dp[i] = Math.min(dp[i], dp[i-j*j]+1); } } return dp[n]; } }
3. 四平方和定理法
public class Solution { public int numSquares (int n) { int m = n; while (m % 4 == 0) m = m >> 2; if (m % 8 == 7) return 4; int sqrtOfn = (int) Math.sqrt(n); if (sqrtOfn * sqrtOfn == n) //Is it a Perfect square? return 1; else { for (int i = 1; i <= sqrtOfn; ++i){ int remainder = n - i*i; int sqrtOfNum = (int) Math.sqrt(remainder); if (sqrtOfNum * sqrtOfNum == remainder) return 2; } } return 3; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66197.html
摘要:類似這種需要遍歷矩陣或數(shù)組來(lái)判斷,或者計(jì)算最優(yōu)解最短步數(shù),最大距離,的題目,都可以使用遞歸。 Problem Given a 2D binary matrix filled with 0s and 1s, find the largest square containing all 1s and return its area. Example For example, given t...
摘要:動(dòng)態(tài)規(guī)劃復(fù)雜度時(shí)間空間思路如果一個(gè)數(shù)可以表示為一個(gè)任意數(shù)加上一個(gè)平方數(shù),也就是,那么能組成這個(gè)數(shù)最少的平方數(shù)個(gè)數(shù),就是能組成最少的平方數(shù)個(gè)數(shù)加上因?yàn)橐呀?jīng)是平方數(shù)了。 Perfect Squares Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4...
Problem Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12Output: 3 Explanation: 12 = 4 + 4 + 4.Exampl...
摘要:題目要求判斷一個(gè)數(shù)字最少由幾個(gè)平方數(shù)的和構(gòu)成。思路一暴力遞歸要想知道什么樣的組合最好,暴力比較所有的結(jié)果就好啦。當(dāng)然,效率奇差。代碼如下思路三數(shù)學(xué)統(tǒng)治一切這里涉及了一個(gè)叫做四平方定理的內(nèi)容。有興趣的可以去了解一下這個(gè)定理。 題目要求 Given a positive integer n, find the least number of perfect square numbers (...
Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...
閱讀 1756·2021-09-23 11:34
閱讀 2477·2021-09-22 15:45
閱讀 12946·2021-09-22 15:07
閱讀 2233·2021-09-02 15:40
閱讀 4122·2021-07-29 14:48
閱讀 1079·2019-08-30 15:55
閱讀 3249·2019-08-30 15:55
閱讀 2194·2019-08-30 15:55