finding the closest K points from origin
up vote
0
down vote
favorite
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
|
show 4 more comments
up vote
0
down vote
favorite
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
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 ofList<Integer> smallestKPoints
matches type ofList<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
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
java list 2d
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 ofList<Integer> smallestKPoints
matches type ofList<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
|
show 4 more comments
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 ofList<Integer> smallestKPoints
matches type ofList<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
|
show 4 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53369884%2ffinding-the-closest-k-points-from-origin%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
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 ofList<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