We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My math skill sucks pants, java brute force approach for fun
public static int pageCount(int n, int p) {
// Write your code here
List<List<Integer>> book = new ArrayList();
System.out.println(String.format("n=%d, p=%d ", n, p));
int numOfPages = n + 1; // +1 is page zero which will always present
int l = (int) Math.ceil (numOfPages/2.0);
System.out.println("l=" + l);
//create the books
for (int i = 0; i < l; i++) {
List<Integer> pages = new ArrayList<>();
int number = i * 2;
pages.add(number);
pages.add(number+1);
book.add(new ArrayList<>(pages));
}
//7 pages
//should be 4 paper
//7 / 2
System.out.println("book: " + book);
int ftbFlip = 0;
for (int i = 0; i < book.size(); i++) {
List<Integer> pages = book.get(i);
System.out.println(String.format("iftb=%d pages=%s", i, pages.toString()));
if (pages.contains(p)) {
ftbFlip = i;
break;
}
}
System.out.println("ftbFlip: " + ftbFlip);
int btfFlip = 0;
for (int i = book.size() - 1; i > 0; i--) {
List<Integer> pages = book.get(i);
System.out.println("pages: " + pages);
if (pages.contains(p)) {
break;
} else {
btfFlip++;
}
}
System.out.println("btfFlip: " + btfFlip);
int result = Math.min(ftbFlip, btfFlip);
return result;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Drawing Book
You are viewing a single comment's thread. Return to all comments →
My math skill sucks pants, java brute force approach for fun