Upen
BAN USER
Junior majoring in Computer Science. Email me at dhakal.upenn@gmail.com if you have any questions.
This solution works for any point not just (5,5)
import java.util.Arrays;
/**
* @author upen on 10/24/15.
*/
public class ClosestPoints {
public static void main(String[] args) {
Solution sl = new Solution();
Point[] points = {new Point(-2, -4), new Point(0, 0), new Point(10, 15),
new Point(5, 6), new Point(7, 8), new Point(-10, -30)};
Point[] output = sl.findClosest(points, new Point(5, 5), 2);
for (int i=0; i<2; i++){
System.out.println(output[i].toString());
}
}
}
class Solution {
public Point[] findClosest(Point[] points, Point point, int k) {
int len = points.length;
for (int i = 0; i < len; i++) {
Point currPoint = points[i];
currPoint.setDistance(findDistance(point, currPoint));
}
Arrays.sort(points);
return Arrays.copyOf(points, k);
}
public double findDistance(Point p1, Point p2) {
int squareOfX = (p2.getX() - p1.getX()) * (p2.getX() - p1.getX());
int squareOfY = (p2.getY() - p1.getY()) * (p2.getY() - p1.getY());
return Math.sqrt(squareOfX + squareOfY);
}
}
class Point implements Comparable<Point> {
/**
* The x position of the point
*/
private int mX;
/**
* The y position of the point
*/
private int mY;
/**
* distance form the (5,5)
*/
private double mDistance;
public Point(int x, int y) {
this.mX = x;
this.mY = y;
}
public int getX() {
return mX;
}
public void setX(int mX) {
this.mX = mX;
}
public int getY() {
return mY;
}
public void setY(int mY) {
this.mY = mY;
}
public double getDistance() {
return mDistance;
}
public void setDistance(double mDistance) {
this.mDistance = mDistance;
}
@Override
public int compareTo(Point other) {
return (int) (mDistance - other.mDistance);
}
@Override
public String toString() {
return "(" + mX + ", " + mY +")";
}
}
Java Implementation:
/**
* Created by Upen on 10/20/2015.
*/
public class CountMines {
/**
* Positive number that represents the number of the grids
*/
private int N;
/**
* This is the array that stores the array read form the stdin. Each element in this
* arry is either 0 or a 1
*/
private int[] mInputArray;
/**
* This is the n*n array that represents the output. Each element in this array represents
* the number of '1' around it.
*/
int[][] mOutputArray;
public CountMines(int[] array, int size){
N = size;
mInputArray = array;
mOutputArray = new int[size][size];
}
public int[][] solveAndGetArray(){
solveProblem();
return mOutputArray;
}
private void solveProblem(){
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
mOutputArray[i][j] = findSorroundingOnes(i, j);
}
}
}
private int findSorroundingOnes(int i, int j){
return getOneOrZero(i-1, j-1)
+ getOneOrZero(i, j-1)
+ getOneOrZero(i+1, j-1)
+ getOneOrZero(i-1, j)
+ getOneOrZero(i+1, j)
+ getOneOrZero(i-1, j+1)
+ getOneOrZero(i, j+1)
+ getOneOrZero(i+1, j+1);
}
/**
* This is the function that takes x and y position of the 2 by 2 array and
* returns the equivalant position in 1 dimensional array
* @param i The x position of the array
* @param j The y position of the array
*/
private int intoOneDim(int i, int j){
return i*N + j;
}
/**
* This is the method that returns the value in the grid i.e. weather the board has
* 1 or 0. If the given position is not valid we return 0
* @param i The x position in the board
* @param j The y position if the grid
* @return The value (0 or 1) in the given position
*/
private int getOneOrZero(int i, int j){
if (isValidIndex(i, j)){
return mInputArray[intoOneDim(i, j)];
}
return 0;
}
/**
* This is the method that checks if the x and y position are with in the
* bound of indices in the array
* @param i The x index of the array
* @param j The y index of the array
* @return
*/
private boolean isValidIndex(int i, int j){
return i >= 0 && i < N && j < N && j >= 0;
}
public static void main(String[] args) {
CountMines g = new CountMines(new int[]{0, 1, 0, 0, 0, 0, 1, 0, 0}, 3);
int[][] array = g.solveAndGetArray();
for (int i=0; i<array.length; i++){
for (int j=0; j<array.length; j++){
System.out.print("" + array[i][j] + " ");
}
System.out.println();
}
}
}
Repcinderellagale, Financial Application Engineer at Continental
Working as a Forecasting Analyst at Thorofare for more than three years. There, I am responsible for developing detailed business ...
Reptargienaron, Public relations coordinator at Total Quality
I am a public relations coordinator . Planning publicity strategies and campaigns. writing and producing presentations and press releases. I explore ...
RepNoraLillian, Applications Developer at Coupondesh
Nora, working as a ghostwriter with a history in manuscript development, proofreading, and editing, I apply with enthusiasm for this ...
RepAadhikLee, abc at 8x8
Expedite data entry efforts and bank reconciliation under the direct guidance of the senior accountant, ensuring clean, accurate financial records ...
Repkassacraven, Java Experienced at Brainware
Kassa , an Outgoing a Travel Consultant with over 3 years of experience in delivering professional travel and tourism-related services focusing ...
Repanayadonal67, Animator at ABC TECH SUPPORT
AnayaDonal and I am a City planners. I have completed all my studies from Chicago. It's been a long ...
RepGizzyKale, Graphics Programmer at Arista Networks
Je suis Gizzy, un commis aux archives avec d'excellentes compétences interpersonnelles et de communication. Expert en nourriture , boisson et ...
RepEmilRuiz, Data Engineer at Apple
Emil , an Experienced producer has been producing top-rated shows for five years running. Excels at defining and expanding target audience ...
Repnetteyoder22, Applications Developer at 247quickbookshelp
Nette, transportation inspector inspects goods and equipment associated with transporting people or cargo to ensure safety. I typically work for ...
Repjomajogya, Applications Developer at Autonomy Zantaz
I’m a teacher assistant who supports the teacher in planning and presenting lessons, and helps students learn . I specialize ...
RepJamesSeavey, Android test engineer at ABC TECH SUPPORT
Hey, my name is JamesSeavey and I completed all my study from California. And Nowadays I am working as a ...
RepJaninaGilden, Java Experienced at Boeing
I am Janina , a Registered Nurse with 3 years experience providing healthcare to a variety of patients in different institutions ...
RepTerryMorales, Accountant at ADP
I assist other social and human service providers in providing client services in a wide variety of fields, such as ...
RepWilliamWiltz, Call Girls in Chawri Bazar at Amdocs
Hi , I am William, a Medical technologist with extensive knowledge around sample examination and analyzing results. Selected from a pool ...
RepKayIrish, Animator at ADP
Hey,I am a psychologist.I cure your problems with Vashikaran Specialist In Meerut. I am deeply counseling your problems ...
Repjosebowlin78, Aircraft engineer at CSK Auto
I am an Aircraft engineer . My role as an aircraft engineer involves the application of scientific and technological principles to ...
RepSimonPister, Blockchain Developer at Absolute Softech Ltd
Simon , a food scientist , records the Tracking status of all existing ingredients during the review and updating process and communicates ...
- Upen October 25, 2016