finding the closest K points from origin











up vote
0
down vote

favorite
1












I have the following problem: given a list of point coordinate return the k closest point to the origin, the return type should be a list of coordinate.
I have worked on this code but need to fix the return type smallestkpoints to match the return type of closestPoints method.
any thoughts?



import java.lang.Math;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.Double;

public class ClosestKPoints {

//distance to origin
public static Double distance(Integer x, Integer y) {

double powX = Math.pow(x, 2);
double powY = Math.pow(y, 2);

return (Double)(Math.sqrt(powX + powY));
}


public static List<List<Integer>> closestPoints(int k, List<List<Integer>> points, int all) {

Hashtable<Double,List<Integer>> pt = new Hashtable();
Double dt = new Double[all];//where we store distances later to be sorted
List<List<Integer>> smallestKPoints;//to be used as return result


for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(distance( x, y), points.get(i));
dt[i] = distance(x, y);
}

}

Arrays.sort(dt);

for (int n = 0; n < k; n++) {
List<Integer> smallestKPoints = pt.get(dt[n]);
}

return smallestKPoints;

}
}









share|improve this question
























  • just define List<Integer> smallestKPoints out of the loop
    – xyz
    Nov 19 at 7:22










  • I did it before and forgot to put it in the code
    – user1712095
    Nov 19 at 7:23










  • the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
    – user1712095
    Nov 19 at 7:24












  • From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
    – Mershel
    Nov 19 at 7:24










  • the return type should be list<List<Integer>>
    – user1712095
    Nov 19 at 7:25















up vote
0
down vote

favorite
1












I have the following problem: given a list of point coordinate return the k closest point to the origin, the return type should be a list of coordinate.
I have worked on this code but need to fix the return type smallestkpoints to match the return type of closestPoints method.
any thoughts?



import java.lang.Math;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.Double;

public class ClosestKPoints {

//distance to origin
public static Double distance(Integer x, Integer y) {

double powX = Math.pow(x, 2);
double powY = Math.pow(y, 2);

return (Double)(Math.sqrt(powX + powY));
}


public static List<List<Integer>> closestPoints(int k, List<List<Integer>> points, int all) {

Hashtable<Double,List<Integer>> pt = new Hashtable();
Double dt = new Double[all];//where we store distances later to be sorted
List<List<Integer>> smallestKPoints;//to be used as return result


for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(distance( x, y), points.get(i));
dt[i] = distance(x, y);
}

}

Arrays.sort(dt);

for (int n = 0; n < k; n++) {
List<Integer> smallestKPoints = pt.get(dt[n]);
}

return smallestKPoints;

}
}









share|improve this question
























  • just define List<Integer> smallestKPoints out of the loop
    – xyz
    Nov 19 at 7:22










  • I did it before and forgot to put it in the code
    – user1712095
    Nov 19 at 7:23










  • the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
    – user1712095
    Nov 19 at 7:24












  • From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
    – Mershel
    Nov 19 at 7:24










  • the return type should be list<List<Integer>>
    – user1712095
    Nov 19 at 7:25













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have the following problem: given a list of point coordinate return the k closest point to the origin, the return type should be a list of coordinate.
I have worked on this code but need to fix the return type smallestkpoints to match the return type of closestPoints method.
any thoughts?



import java.lang.Math;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.Double;

public class ClosestKPoints {

//distance to origin
public static Double distance(Integer x, Integer y) {

double powX = Math.pow(x, 2);
double powY = Math.pow(y, 2);

return (Double)(Math.sqrt(powX + powY));
}


public static List<List<Integer>> closestPoints(int k, List<List<Integer>> points, int all) {

Hashtable<Double,List<Integer>> pt = new Hashtable();
Double dt = new Double[all];//where we store distances later to be sorted
List<List<Integer>> smallestKPoints;//to be used as return result


for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(distance( x, y), points.get(i));
dt[i] = distance(x, y);
}

}

Arrays.sort(dt);

for (int n = 0; n < k; n++) {
List<Integer> smallestKPoints = pt.get(dt[n]);
}

return smallestKPoints;

}
}









share|improve this question















I have the following problem: given a list of point coordinate return the k closest point to the origin, the return type should be a list of coordinate.
I have worked on this code but need to fix the return type smallestkpoints to match the return type of closestPoints method.
any thoughts?



import java.lang.Math;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import java.lang.Integer;
import java.lang.Double;

public class ClosestKPoints {

//distance to origin
public static Double distance(Integer x, Integer y) {

double powX = Math.pow(x, 2);
double powY = Math.pow(y, 2);

return (Double)(Math.sqrt(powX + powY));
}


public static List<List<Integer>> closestPoints(int k, List<List<Integer>> points, int all) {

Hashtable<Double,List<Integer>> pt = new Hashtable();
Double dt = new Double[all];//where we store distances later to be sorted
List<List<Integer>> smallestKPoints;//to be used as return result


for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(distance( x, y), points.get(i));
dt[i] = distance(x, y);
}

}

Arrays.sort(dt);

for (int n = 0; n < k; n++) {
List<Integer> smallestKPoints = pt.get(dt[n]);
}

return smallestKPoints;

}
}






java list 2d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 7:28

























asked Nov 19 at 7:14









user1712095

971212




971212












  • just define List<Integer> smallestKPoints out of the loop
    – xyz
    Nov 19 at 7:22










  • I did it before and forgot to put it in the code
    – user1712095
    Nov 19 at 7:23










  • the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
    – user1712095
    Nov 19 at 7:24












  • From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
    – Mershel
    Nov 19 at 7:24










  • the return type should be list<List<Integer>>
    – user1712095
    Nov 19 at 7:25


















  • just define List<Integer> smallestKPoints out of the loop
    – xyz
    Nov 19 at 7:22










  • I did it before and forgot to put it in the code
    – user1712095
    Nov 19 at 7:23










  • the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
    – user1712095
    Nov 19 at 7:24












  • From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
    – Mershel
    Nov 19 at 7:24










  • the return type should be list<List<Integer>>
    – user1712095
    Nov 19 at 7:25
















just define List<Integer> smallestKPoints out of the loop
– xyz
Nov 19 at 7:22




just define List<Integer> smallestKPoints out of the loop
– xyz
Nov 19 at 7:22












I did it before and forgot to put it in the code
– user1712095
Nov 19 at 7:23




I did it before and forgot to put it in the code
– user1712095
Nov 19 at 7:23












the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
– user1712095
Nov 19 at 7:24






the error I am getting reside within this block: for (int n=0; n<k;n++) { List<Integer> smallestKPoints = pt.get(dt[n]); } return smallestKPoints;
– user1712095
Nov 19 at 7:24














From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
– Mershel
Nov 19 at 7:24




From what i see, return type of List<Integer> smallestKPoints matches type of List<Integer> closestPoints. I don't understand your problem.
– Mershel
Nov 19 at 7:24












the return type should be list<List<Integer>>
– user1712095
Nov 19 at 7:25




the return type should be list<List<Integer>>
– user1712095
Nov 19 at 7:25

















active

oldest

votes











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369884%2ffinding-the-closest-k-points-from-origin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369884%2ffinding-the-closest-k-points-from-origin%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy