|
| 1 | +public class Arrays { |
| 2 | + public static void sum1(int[] a, int[] b) { |
| 3 | + assert (a.length == b.length); |
| 4 | + for (int i = 0; i < a.length; i++) |
| 5 | + for (int j = 0; j < a.length; j++) |
| 6 | + b[i] += j == i ? 0 : a[i]; |
| 7 | + } |
| 8 | + |
| 9 | + public static void sum2(int[] a, int[] b) { |
| 10 | + assert (a.length == b.length); |
| 11 | + int total = 0; |
| 12 | + for (int i = 0; i < a.length; i++) |
| 13 | + for (int j = 0; j < a.length; j++) |
| 14 | + total += b[i]; |
| 15 | + |
| 16 | + total /= (a.length-1); |
| 17 | + |
| 18 | + for (int i = 0; i < a.length; i++) |
| 19 | + a[i] = total - b[i]; |
| 20 | + } |
| 21 | + |
| 22 | + public static void heapify(int[] a) { |
| 23 | + int heapsize = a.length; |
| 24 | + for (int i = a.length / 2; i >= 0; i--) { |
| 25 | + int j = i; |
| 26 | + while (true) { |
| 27 | + final int left = 2*j + 1; |
| 28 | + final int right = left + 1; |
| 29 | + int max = 0; |
| 30 | + |
| 31 | + if (left <= heapsize && a[left] > a[right]) |
| 32 | + max = left; |
| 33 | + else |
| 34 | + max = j; |
| 35 | + |
| 36 | + if (right <= heapsize && a[right] > a[max]) |
| 37 | + max = right; |
| 38 | + |
| 39 | + if (max == j) |
| 40 | + break; |
| 41 | + // else |
| 42 | + int tmp = a[j]; |
| 43 | + a[j] = a[max]; |
| 44 | + a[max] = tmp; |
| 45 | + j = max; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static int stream(int[] a, long k) { |
| 51 | + assert (k >= 0); |
| 52 | + long len = a.length; |
| 53 | + k %= 2*len; |
| 54 | + if (k < len) |
| 55 | + return a[(int) k]; |
| 56 | + else // k >= len |
| 57 | + return 1 - a[(int) (2*len-1 - k)]; |
| 58 | + } |
| 59 | + |
| 60 | + public static boolean isSubseq(int[] a, int[] b) { |
| 61 | + if (a.length > b.length) |
| 62 | + return false; |
| 63 | + |
| 64 | + int current = 0; |
| 65 | + for (int i = 0; i < b.length ; i++) |
| 66 | + if (b[i] == a[current]) |
| 67 | + current++; |
| 68 | + |
| 69 | + return current >= a.length; |
| 70 | + } |
| 71 | + |
| 72 | + public static boolean isConsecSubseq(int[] a, int[] b) { |
| 73 | + if (a.length > b.length) |
| 74 | + return false; |
| 75 | + |
| 76 | + boolean match = false; |
| 77 | + for (int i = 0; i < b.length - a.length; i++) { |
| 78 | + match = true; |
| 79 | + for (int j = 0; j < a.length; j++) |
| 80 | + if (b[j+i] != a[j]) { |
| 81 | + match = false; |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + if (match) |
| 86 | + return true; |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Princeton Knuth-Morris-Pratt Algorithm: youtu.be/iZ93Unvxwtw |
| 92 | + public static boolean KMP(int[] a, int[] b) { |
| 93 | + if (a.length > b.length) |
| 94 | + return false; |
| 95 | + |
| 96 | + final int RADIX = 10; |
| 97 | + final int[][] dfa = new int[RADIX][a.length]; |
| 98 | + // assume all values are single digits |
| 99 | + dfa[a[0]][0] = 1; |
| 100 | + for (int x = 0, j = 1; j < a.length; j++) { |
| 101 | + for (int c = 0; c < RADIX; c++) |
| 102 | + dfa[c][j] = dfa[c][x]; |
| 103 | + |
| 104 | + dfa[a[j]][j] = j+1; |
| 105 | + x = dfa[a[j]][x]; |
| 106 | + } |
| 107 | + |
| 108 | + int i, j; |
| 109 | + for (j = 0, i = 0; i < b.length && j < a.length; i++) |
| 110 | + j = dfa[b[i]][j]; |
| 111 | + |
| 112 | + return j == a.length; |
| 113 | + } |
| 114 | +} |
0 commit comments