Perfect Squares 2

Prereq: Knapsack Intro

This problem is similar to Perfect Squares, except a number can only be used once. Again we ask you to output the minimum number of numbers in order to sum to the desired number. Output -1 if the number can't be reached.

Examples

Example 1:
Input: 12
Output: -1
Explanation:

You cannot make 12 with any combination of 1, 4, 9

Example 2:
Input: 13
Output: 2
Explanation:

13 = 4 + 9

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade