Shopping
The problem is the Shopping Spree 1 on HackerRank. (Remember you need to be signed in and enter the contest to see it.)
There are at least two different ways to approach storing of coordinates for this problem. Think for a minute about what you will have to do and see if you prefer one approach or the other.
- Store all of the information in a 2D array and use (row, column) coordinates.
In that case you might want to use the
Posn
class.
import java.util.*;
class Posn implements Comparable<Posn> {
public int x, y;
public Posn(int xx, int yy) { x=xx; y=yy; }
public int compareTo(Posn other) {
int ans = this.x - other.x;
if (ans == 0) {
ans = this.y - other.y;
}
return ans;
}
public String toString() {
return "(" + x + "," + y + ")";
}
}
- Store all of the information in a long 1D array with
nrows*ncols
elements. In that case the index you use for(row, col)
will be(row * ncols + col)