leetcode笔记:Perfect Squares

一. 题目描述

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.

For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.

二. 题目分析

该题目的大意是,给出一个目标整数,算出由完全平方数累加得到目标整数的最小个数。

比较好的解决方法是使用动态规划。我们都知道一个数x可分解为一个任意数a加上一个平方数b * b,即x = a + b * b。因此,能组成目标整数x最少的平方数个数,就是能组成a最少的平方数个数加上1

三. 示例代码

class Solution {
public:
    int numSquares(int n) {
        static vector<int> sqrtNum(1, 0);
        if(sqrtNum.size() >= n + 1)  return sqrtNum[n];

        while(sqrtNum.size() <= n + 1){
            int temp = INT_MAX;
            for(int j = 1; j * j <= sqrtNum.size(); j++)
                temp = min(temp, sqrtNum[sqrtNum.size() - j * j] + 1);
            sqrtNum.push_back(temp);
        }
        return sqrtNum[n];
    }
};

四. 小结

动态规划的另一处用法。值得深入学习!

时间: 2024-10-12 17:46:31

leetcode笔记:Perfect Squares的相关文章

【Leetcode】Perfect Squares

题目链接:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 

LeetCode 279. Perfect Squares

Question: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. Code: public

【leetcode】Perfect Squares (#279)

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16 ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. 解析: 利用动态规划解决此问题:对于要求的当前节

Leetcode: Valid Perfect Squares

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 我的binary Sear

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

leetcode笔记:Pascal&amp;#39;s Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

hdu 3524 Perfect Squares 推公式求逆元

Perfect Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 501    Accepted Submission(s): 272 Problem Description A number x is called a perfect square if there exists an integer b satisfy

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是完全平方数 求有多少种方案 思路:每个数分解因子 每隔数可以选也可以不选 0 1表示 然后设有m种素数因子 选出的数组成的各个因子的数量必须是偶数 组成一个m行和n列的矩阵 每一行代表每一种因子的系数 解出自由元的数量 #include <cstdio> #include <cstring> #include <algorithm&g