Question about LeetCode 279. 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.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
import java.util.*;
public class Solution {
public int numSquares(int n) {
int i = 1;
int ret = Integer.MAX_VALUE;
while(i * i < (n / 2)) {
int start = i * i;
int step = BFS(start,n);
if(step < ret)
ret = step;
i++;
}
return ret;
}
public int BFS(int start, int n) {
int step = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(start);
while(!queue.isEmpty()){
int node = queue.poll();
step++;
if(node == n)
break;
for(int k=1;k*k <= n;k++){
if(k*k <= n)
queue.offer(k * k + node);
if(node + k*k == n)
return step + 1;
}
}
return step;
}
}
I have problems updating the step value. I can't come up with a solution how to update the step value. Can anyone help?
java algorithm breadth-first-search
add a comment |
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 = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
import java.util.*;
public class Solution {
public int numSquares(int n) {
int i = 1;
int ret = Integer.MAX_VALUE;
while(i * i < (n / 2)) {
int start = i * i;
int step = BFS(start,n);
if(step < ret)
ret = step;
i++;
}
return ret;
}
public int BFS(int start, int n) {
int step = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(start);
while(!queue.isEmpty()){
int node = queue.poll();
step++;
if(node == n)
break;
for(int k=1;k*k <= n;k++){
if(k*k <= n)
queue.offer(k * k + node);
if(node + k*k == n)
return step + 1;
}
}
return step;
}
}
I have problems updating the step value. I can't come up with a solution how to update the step value. Can anyone help?
java algorithm breadth-first-search
1
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04
add a comment |
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 = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
import java.util.*;
public class Solution {
public int numSquares(int n) {
int i = 1;
int ret = Integer.MAX_VALUE;
while(i * i < (n / 2)) {
int start = i * i;
int step = BFS(start,n);
if(step < ret)
ret = step;
i++;
}
return ret;
}
public int BFS(int start, int n) {
int step = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(start);
while(!queue.isEmpty()){
int node = queue.poll();
step++;
if(node == n)
break;
for(int k=1;k*k <= n;k++){
if(k*k <= n)
queue.offer(k * k + node);
if(node + k*k == n)
return step + 1;
}
}
return step;
}
}
I have problems updating the step value. I can't come up with a solution how to update the step value. Can anyone help?
java algorithm breadth-first-search
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 = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
import java.util.*;
public class Solution {
public int numSquares(int n) {
int i = 1;
int ret = Integer.MAX_VALUE;
while(i * i < (n / 2)) {
int start = i * i;
int step = BFS(start,n);
if(step < ret)
ret = step;
i++;
}
return ret;
}
public int BFS(int start, int n) {
int step = 0;
LinkedList<Integer> queue = new LinkedList<Integer>();
queue.offer(start);
while(!queue.isEmpty()){
int node = queue.poll();
step++;
if(node == n)
break;
for(int k=1;k*k <= n;k++){
if(k*k <= n)
queue.offer(k * k + node);
if(node + k*k == n)
return step + 1;
}
}
return step;
}
}
I have problems updating the step value. I can't come up with a solution how to update the step value. Can anyone help?
java algorithm breadth-first-search
java algorithm breadth-first-search
edited Jan 7 at 15:50
c0der
8,79351745
8,79351745
asked Jan 1 at 1:30
Wentao WangWentao Wang
313
313
1
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04
add a comment |
1
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04
1
1
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04
add a comment |
1 Answer
1
active
oldest
votes
IMHO, I will try to update the step
value, by detect some marker
between different levels
in the BFS
procedure, who can tell us that we are hitting the end of previous level and are going into the next level. I usually employ a null
element as this marker
.
See my code with comments, which has been accepted:
public class Solution {
public int numSquares(int n) {
Queue<Integer> queue=new LinkedList();
queue.add(n);
queue.add(null);//a null element to show the gap between two levels,i.e., the step
int res=1;
while(true){
Integer i=queue.element();
if(queue.size()!=1&&i==null){// we are hitting the marker
queue.remove();
res++;//i.e., step+1
queue.add(null);//think about why we need add an additional marker here.
}else{
Integer sqrt=(int)Math.round(Math.sqrt(i));
if(sqrt*sqrt==i){
break;
}else{
queue.remove();
for(int j=sqrt;1<=j;j--){
if(0<i-j*j)
queue.add(i-j*j);
}
}
}
}
return res;
}
}
PS,as stated:
Most standard library headers are already included automatically for your convenience.
So we don't do import java.util.*
and the code can compile.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992515%2fquestion-about-leetcode-279-perfect-squares%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
IMHO, I will try to update the step
value, by detect some marker
between different levels
in the BFS
procedure, who can tell us that we are hitting the end of previous level and are going into the next level. I usually employ a null
element as this marker
.
See my code with comments, which has been accepted:
public class Solution {
public int numSquares(int n) {
Queue<Integer> queue=new LinkedList();
queue.add(n);
queue.add(null);//a null element to show the gap between two levels,i.e., the step
int res=1;
while(true){
Integer i=queue.element();
if(queue.size()!=1&&i==null){// we are hitting the marker
queue.remove();
res++;//i.e., step+1
queue.add(null);//think about why we need add an additional marker here.
}else{
Integer sqrt=(int)Math.round(Math.sqrt(i));
if(sqrt*sqrt==i){
break;
}else{
queue.remove();
for(int j=sqrt;1<=j;j--){
if(0<i-j*j)
queue.add(i-j*j);
}
}
}
}
return res;
}
}
PS,as stated:
Most standard library headers are already included automatically for your convenience.
So we don't do import java.util.*
and the code can compile.
add a comment |
IMHO, I will try to update the step
value, by detect some marker
between different levels
in the BFS
procedure, who can tell us that we are hitting the end of previous level and are going into the next level. I usually employ a null
element as this marker
.
See my code with comments, which has been accepted:
public class Solution {
public int numSquares(int n) {
Queue<Integer> queue=new LinkedList();
queue.add(n);
queue.add(null);//a null element to show the gap between two levels,i.e., the step
int res=1;
while(true){
Integer i=queue.element();
if(queue.size()!=1&&i==null){// we are hitting the marker
queue.remove();
res++;//i.e., step+1
queue.add(null);//think about why we need add an additional marker here.
}else{
Integer sqrt=(int)Math.round(Math.sqrt(i));
if(sqrt*sqrt==i){
break;
}else{
queue.remove();
for(int j=sqrt;1<=j;j--){
if(0<i-j*j)
queue.add(i-j*j);
}
}
}
}
return res;
}
}
PS,as stated:
Most standard library headers are already included automatically for your convenience.
So we don't do import java.util.*
and the code can compile.
add a comment |
IMHO, I will try to update the step
value, by detect some marker
between different levels
in the BFS
procedure, who can tell us that we are hitting the end of previous level and are going into the next level. I usually employ a null
element as this marker
.
See my code with comments, which has been accepted:
public class Solution {
public int numSquares(int n) {
Queue<Integer> queue=new LinkedList();
queue.add(n);
queue.add(null);//a null element to show the gap between two levels,i.e., the step
int res=1;
while(true){
Integer i=queue.element();
if(queue.size()!=1&&i==null){// we are hitting the marker
queue.remove();
res++;//i.e., step+1
queue.add(null);//think about why we need add an additional marker here.
}else{
Integer sqrt=(int)Math.round(Math.sqrt(i));
if(sqrt*sqrt==i){
break;
}else{
queue.remove();
for(int j=sqrt;1<=j;j--){
if(0<i-j*j)
queue.add(i-j*j);
}
}
}
}
return res;
}
}
PS,as stated:
Most standard library headers are already included automatically for your convenience.
So we don't do import java.util.*
and the code can compile.
IMHO, I will try to update the step
value, by detect some marker
between different levels
in the BFS
procedure, who can tell us that we are hitting the end of previous level and are going into the next level. I usually employ a null
element as this marker
.
See my code with comments, which has been accepted:
public class Solution {
public int numSquares(int n) {
Queue<Integer> queue=new LinkedList();
queue.add(n);
queue.add(null);//a null element to show the gap between two levels,i.e., the step
int res=1;
while(true){
Integer i=queue.element();
if(queue.size()!=1&&i==null){// we are hitting the marker
queue.remove();
res++;//i.e., step+1
queue.add(null);//think about why we need add an additional marker here.
}else{
Integer sqrt=(int)Math.round(Math.sqrt(i));
if(sqrt*sqrt==i){
break;
}else{
queue.remove();
for(int j=sqrt;1<=j;j--){
if(0<i-j*j)
queue.add(i-j*j);
}
}
}
}
return res;
}
}
PS,as stated:
Most standard library headers are already included automatically for your convenience.
So we don't do import java.util.*
and the code can compile.
answered Jan 1 at 14:43
ZhaoGangZhaoGang
1,9691116
1,9691116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992515%2fquestion-about-leetcode-279-perfect-squares%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
IMHO, this question has a dp solution. So why bothering try to find a BFS one?
– ZhaoGang
Jan 1 at 1:55
It 's under the BFS problems collection so that I used BFS. And I hope I can figure out this specific solution.
– Wentao Wang
Jan 1 at 10:21
This question has a surprisingly simple solution. Start with Lagrange four-squares theorem, and follow the links for three-squares, and two-squares theorems.
– user58697
Jan 1 at 22:04
@user58697 consider posting a solution. Looking at the links I couldn't see an easy solution.
– c0der
Jan 7 at 16:04