|
| 1 | +package ArraysAndStrings; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +// Byte-by-byte and cracking the coding interview |
| 6 | +public class FiftyQuestions { |
| 7 | + |
| 8 | + /** 1. Find median of two sorted arrays |
| 9 | + * arr1 = [1, 3, 5] |
| 10 | + * arr2 = [2, 4, 6] |
| 11 | + * median(arr1, arr2) = 3.5 |
| 12 | + * */ |
| 13 | + static double median(int[] nums1, int[] nums2) { |
| 14 | + int totalLength = nums1.length + nums2.length; |
| 15 | + int medianIndex; |
| 16 | + boolean even = totalLength % 2 == 0; |
| 17 | + if (even) { |
| 18 | + medianIndex = totalLength / 2; |
| 19 | + } |
| 20 | + else { |
| 21 | + medianIndex = (totalLength - 1) / 2; |
| 22 | + } |
| 23 | + |
| 24 | + int i = 0, j = 0; |
| 25 | + int[] nums3 = new int[medianIndex + 1]; |
| 26 | + for (int k = 0; k <= medianIndex; k++) { |
| 27 | + if (i >= nums1.length) |
| 28 | + nums3[k] = nums2[j++]; |
| 29 | + else if (j >= nums2.length) |
| 30 | + nums3[k] = nums1[i++]; |
| 31 | + else if (nums1[i] <= nums2[j]) |
| 32 | + nums3[k] = nums1[i++]; |
| 33 | + else { |
| 34 | + nums3[k] = nums2[j++]; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (even) |
| 39 | + return (nums3[medianIndex] + nums3[medianIndex-1]) / 2.0; |
| 40 | + else |
| 41 | + return nums3[medianIndex]; |
| 42 | + } |
| 43 | + |
| 44 | + /** 2. Given a list of items with values and weights, as well as a max weight, find the |
| 45 | + maximum value you can generate from items where the sum of the weights is less than |
| 46 | + the max. |
| 47 | + */ |
| 48 | + // Item class |
| 49 | + static class Item { |
| 50 | + int weight; |
| 51 | + int value; |
| 52 | + |
| 53 | + public Item(int weight, int value) { |
| 54 | + this.weight = weight; |
| 55 | + this.value = value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static int knapsack(Item[] items, int W) { |
| 60 | + // cache[i][j] = max value for the first i items with a max weight of j |
| 61 | + int[][] cache = new int[items.length + 1][W + 1]; |
| 62 | + for (int i = 1; i <= items.length; i++) { |
| 63 | + for (int j = 0; j <= W; j++) { |
| 64 | + // If including item[i-1] would exceed max weight j, don't |
| 65 | + // include the item. Otherwise take the max value of including |
| 66 | + // or excluding the item |
| 67 | + if (items[i-1].weight > j) cache[i][j] = cache[i-1][j]; |
| 68 | + else cache[i][j] = Math.max(cache[i-1][j], cache[i-1][j-items[i-1].weight] + items[i-1].value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return cache[items.length][W]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * 4. Find Duplicates |
| 77 | + * |
| 78 | + * Given an array of integers where each value 1 <= x <= len(array), write a |
| 79 | + * function that finds all the duplicates in the array. |
| 80 | + * */ |
| 81 | + static int[] findDuplicates(int[] arr) { |
| 82 | + Set<Integer> uniqueNums = new HashSet<>(); |
| 83 | + int[] dups = new int[arr.length]; |
| 84 | + int j = 0; |
| 85 | + for (int num : arr) { |
| 86 | + if (!uniqueNums.add(num)) { |
| 87 | + dups[j++] = num; |
| 88 | + } |
| 89 | + } |
| 90 | + return dups; |
| 91 | + } |
| 92 | + |
| 93 | + static int consecutiveArray(int[] arr) { |
| 94 | + Set<Integer> values = new HashSet<>(); |
| 95 | + for (int i : arr) { |
| 96 | + values.add(i); |
| 97 | + } |
| 98 | + |
| 99 | + // For each value, check if its the first in a sequence of consecutive |
| 100 | + // numbers and iterate through to find the length of the consecutive |
| 101 | + // sequence |
| 102 | + int maxLength = 0; |
| 103 | + for (int i : values) { |
| 104 | + // If it is not the leftmost value in the sequence, don't bother |
| 105 | + if (values.contains(i - 1)) |
| 106 | + continue; |
| 107 | + int length = 0; |
| 108 | + |
| 109 | + // Iterate through sequence |
| 110 | + while (values.contains(i++)) { |
| 111 | + length++; |
| 112 | + } |
| 113 | + maxLength = Math.max(maxLength, length); |
| 114 | + } |
| 115 | + |
| 116 | + return maxLength; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * ctci 17.6 |
| 121 | + * write a method to find indices m and n s.t. if you sorted elements m through n, |
| 122 | + * the entire array would be sorted. Minimize n-m |
| 123 | + * Input : 1,2,4,7,10,7,12,6,7,16,18,19 |
| 124 | + * Output: 3,9 |
| 125 | + * */ |
| 126 | + static void findIndices(int[] arr) { //not correct |
| 127 | + int firstIndex = 0; |
| 128 | + int lastIndex = 0; |
| 129 | + int j = 0; |
| 130 | + for (int i = 0; i < arr.length-2; i++) { |
| 131 | + if (arr[i] > arr[i+1]) { |
| 132 | + firstIndex = i + 1; |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + for (int i = arr.length-1; i > 2; i-- ) { |
| 137 | + if (arr[i] > arr[i-1]) { |
| 138 | + lastIndex = i; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + for (int i = 0; i <= firstIndex; i++) { |
| 143 | + if (arr[i] == arr[firstIndex]) { |
| 144 | + System.out.println("first index is " + i); |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + for (int i = firstIndex; i <= lastIndex; i++) { |
| 149 | + if (arr[i] == arr[firstIndex]) { |
| 150 | + j = i; |
| 151 | + } |
| 152 | + } |
| 153 | + System.out.println("last index is = " + j ); |
| 154 | + } |
| 155 | + |
| 156 | + /** ctci 17.7 given 1284 print "One Thousand Two Hundred Eighty Four*/ |
| 157 | + static void printIntegerInWords(int num) { |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + /** ctci 17.8 contiguous sequence with largest sum*/ |
| 162 | + static int cSum(int[] arr) { |
| 163 | + int maxSum = 0; |
| 164 | + int currSum = 0; |
| 165 | + for (int j : arr) { |
| 166 | + currSum += j; |
| 167 | + maxSum = Math.max(maxSum, currSum); |
| 168 | + if (currSum < 0) { |
| 169 | + currSum = 0; |
| 170 | + } |
| 171 | + } |
| 172 | + return maxSum; |
| 173 | + } |
| 174 | + |
| 175 | + static void allPairs(int[] arr, int k) { |
| 176 | + // 1,2,2,3 and k = 4 => (1,3) and (2,2) |
| 177 | + Map<Integer, Integer> nums = new HashMap<>(); |
| 178 | + for (int num : arr) |
| 179 | + nums.merge(num, 1 , Integer::sum); |
| 180 | + for (int num : arr) { |
| 181 | + if (num == k - num && nums.get(num) > 1) |
| 182 | + System.out.printf("Pair - (%d:%d)%n", num, num); |
| 183 | + if (nums.containsKey(k-num)) |
| 184 | + System.out.printf("Pair - (%d:%d)%n", num, k-num); |
| 185 | + |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + static int search(int[] nums, int target, int left, int right) { |
| 190 | + // 50, 60, 70, 80, 0, 10, 20, 30, 40 |
| 191 | + // 0 1 2 3 4 5 6 7 8 |
| 192 | + int mid = (left + right) / 2; |
| 193 | + if (nums[mid] == target) |
| 194 | + return mid; |
| 195 | + else if (right < left) |
| 196 | + return -1; |
| 197 | + |
| 198 | + if (nums[left] < nums[mid]) { // left of mid is sorted |
| 199 | + if (target <= nums[mid] && target >= nums[left]) { // element is in left array |
| 200 | + return search(nums, target, left, mid-1); |
| 201 | + } |
| 202 | + else { |
| 203 | + return search(nums, target, mid+1, right); |
| 204 | + } |
| 205 | + } |
| 206 | + else if (nums[mid] < nums[right]) { // right of mid is sorted |
| 207 | + if (target >= nums[mid] && target <= nums[right]) { // |
| 208 | + return search(nums, target, mid+1, right); |
| 209 | + } |
| 210 | + else { |
| 211 | + return search(nums, target, left, mid-1); |
| 212 | + } |
| 213 | + } |
| 214 | + else if (target <= nums[right]) { |
| 215 | + return search(nums, target, mid+1, right); |
| 216 | + } |
| 217 | + else { |
| 218 | + return search(nums, target, left, mid-1); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + public static void main(String[] args) { |
| 223 | + int index = search(new int[]{2,2,2,3,4,2}, 3, 0, 5); |
| 224 | + System.out.println("index of 3 is " + index); |
| 225 | + } |
| 226 | +} |
0 commit comments