daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public int minFactory(int[] array, int range) {
  2.         int i = 0;
  3.         int count = 0;
  4.         int start = 0;
  5.         while (i < array.length && i <= range) {
  6.             if (array[i] == 1) {
  7.                 start = i;
  8.             }
  9.             i++;
  10.         }
  11.         if (start == 0 && array[0] == 0) return -1;
  12.         count++;
  13.         while (start + range + 1 < array.length) {
  14.             start = farMostFactory(array, start, range);
  15.             if (start == -1) return -1;
  16.             count++;
  17.         }
  18.         return count;
  19.     }
  20.     private int farMostFactory(int[] array, int start, int range) {
  21.         int i = start + 1; int pos = start;
  22.        // if (start + 1 + range >= array.length) return array.length; // The start 1 can cover to the end
  23.         while (i < array.length && i - start <= 2 * range + 1) {
  24.             if (array[i] == 1) {
  25.                 pos = i;
  26.             }
  27.             i++;
  28.         }
  29.         if (pos == start) return -1;
  30.         return pos;
  31.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top