2020.1.16
import java.io.*;
import java.util.*;
import com.google.common.primitives.Ints;
class Solution {
// Warmup Questions
// 1. Sum of array numbers
public static int sum(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
result += nums[i];
}
return result;
}
// 2. Minimum element of the array
public static int smallerNum(int a, int b) {
if (a < b) {
return a;
}
return b;
}
public static int minNum(int[] nums) {
// better Robustness when null nums
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}
// 3. Second minimum element of the array
// case 3.1: no duplicate in array
public static int secondMinNum(int[] nums) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
int secondMin = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == min) {
continue;
}
if (nums[i] < secondMin) {
secondMin = nums[i];
}
}
return secondMin;
}
// case 3.2: duplicate nums in array
public static int secondMinNum2(int[] nums) {
int min = Math.min(nums[0], nums[1]);
int secondMin = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < min) {
secondMin = min;
min = nums[i];
} else if (nums[i] == min) {
secondMin = min;
} else if (nums[i] > min && nums[i] < secondMin) {
secondMin = nums[i];
} else if (nums[i] == secondMin) {
continue;
} else {
continue;
}
}
return secondMin;
}
// case 3.2: duplicate nums in array, simplified version
public static int secondMinNum3(int[] nums) {
int min = Math.min(nums[0], nums[1]);
int secondMin = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < min) {
secondMin = min;
min = nums[i];
} else if (nums[i] < secondMin) {
secondMin = nums[i];
}
}
return secondMin;
}
// 4. Swap two elements in array
public static void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
// Two Sum
// time O(n^2)
public static int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
// corner case
if (nums.length < 2) {
return result;
}
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
if (nums[i] < nums[j]) {
result[0] = nums[i];
result[1] = nums[j];
} else {
result[0] = nums[j];
result[1] = nums[i];
}
}
}
}
return result;
}
/*
Two Pointer
Use TWO pointers, instead of one, to tranverse the array in the array in the same/opposite direction.
* Mostly, SORTED
- in number order
- in sequence order
- linked list order
* To find two numbers, or two set of numbers, which are subject to some conditions.
*/
// time O(n log n) + O(n) = O(n log n)
public static int[] twoSum2(int[] nums, int target) {
int[] result = new int[2];
int[] sortedNums = nums.clone();
Arrays.sort(sortedNums);
int i = 0;
int j = sortedNums.length - 1;
while (i < j) {
if (sortedNums[i] + sortedNums[j] == target) {
result[0] = Ints.indexOf(nums, sortedNums[i]);
result[1] = Ints.indexOf(nums, sortedNums[j]);
return result;
} else if (sortedNums[i] + sortedNums[j] < target) {
i++;
} else {
j--;
}
}
return result;
}
// Three Sum
// time O(n log n) + O(n^2) = O(n^2)
public static int[] threeSum(int[] nums, int target) {
int[] result = new int[3];
if (nums.length < 3) {
return result;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int m = i + 1;
int n = nums.length - 1;
int target2 = target - nums[i];
while (m < n) {
if (nums[m] + nums[n] == target2) {
result[0] = nums[i];
result[1] = nums[m];
result[2] = nums[n];
return result;
} else if (nums[m] + nums[n] < target2) {
m++;
} else {
n--;
}
}
}
return result;
}
// Reverse Array
// Given an array, reverse all the numbers in the array
// Follow up:
// - reverse number (1234 -> 4321)
// - palindrome number / string ('abcd' -> 'dcba'; '343' -> true)
// - odd even sort, pivot sort
// - etc.
public static void reverseArray(int[] nums) {
int i = 0;
int j = nums.length - 1;
while (i < j) {
swapHelper(nums, i, j);
i++;
j--;
}
}
private static void swapHelper(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
// Odd Even Sort
// Given an array of integers, sort them so that alll odd integers come before even integers
// The order of elements can be changed. The order of sorted odd numbers and even numbers doesn't matter.
// input: {4,3,5,2,1,11,0,8,6,9}
// output: {9,3,5,11,1,2,0,8,6,4}
public void oddEvenSort(int[] nums) {
int i = 0;
int j = nums.length - 1;
while (i < j) {
while (i < j && nums[i] % 2 == 1) {
i++;
}
while (i < j && nums[i] % 2 == 0) {
j--;
}
if (i < j) {
swapHelper(nums, i, j);
i++;
j--;
}
}
}
// Pivot Sort
// Given an array of integers and a target number, sort them so that all numbers that are smaller than the target always come before the numbers that are larger than the target.
// The order of elements can be changed.
// input: {4,9,5,2,1,11,0,8,6,3}, 7
// output: {4,3,5,2,1,6,0,8,11,9}
// Follow up:
// QuickSort
public static pivotSort(int[] nums, int pivot) {
int i = 0;
int j = nums.length;
while (i < j) {
while (i < j && nums[i] <= pivot) {
i++;
}
while (i < j && nums[j] > pivot) {
j--;
}
if (i < j) {
swapHelper(nums, i, j);
}
}
}
// Remove Element
// Given an array and a value, remove all instances of that value in place and return the new length
// input: {10,9,5,3,9,9,8,6,7}, 9
// output: {10,5,3,8,6,7,X,X,X}, 6
public static int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int i = 0;
int j = nums.length - 1;
while (i < j) {
while (i < j && nums[i] != val) {
i++;
}
while (i < j && nums[j] == val) {
j--;
}
if (i < j) {
swapHelper(nums, i, j);
i++;
j--;
}
}
return nums[i] != val ? i + 1 : i;
}
public static int removeElement2(int[] nums, int val) {
int i = 0;
int len = nums.length;
// len is the valid length of remaining array
while (i < len) {
if (nums[i] == val) {
// remove one element
len--;
// swap curr number with the curr last number
nums[i] = nums[len];
} else {
i++;
}
}
return len;
}
// Merge Two Sorted Array
// input: {1,3,5}, {2,4,6}
// output: {1,2,3,4,5,6}
// Follow up:
// - merge two sorted linked list
// - merge k sorted array
// - etc.
public static int[] mergeSortedArray(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int i = 0, m = 0, n = 0;
while (m < arr1.length && n < arr2.length) {
if (arr1[m] < arr2[n]) {
result[i++] = arr1[m++];
} else {
result[i++] = arr2[n++];
}
}
for (int k = m; i < arr1.length; k++) {
result[m++] = arr1[k];
}
for (int k = n; i < arr2.length; k++) {
result[n++] = arr2[k];
}
return result;
}
///
// Limited Operation
// Get value by index & Get length
// We need more operations
// - add a number into array
// - remove a number
// - check if a number is in the array
// - etc
// They are all very commonly used and need many lines of code!
///
// ArrayList
// Basic Operations
// Get, Set, Add, Remove, Find
// Fields: store basic data, info about the object
// Functions: get access to or modify the fields
///
public static void main(String[] args) {
// // Warm up Questions
// int[] nums = {1,2,3,4,5};
// System.out.println(sum(nums));
// System.out.println(smallerNum(2,3));
// System.out.println(minNum(nums));
// System.out.println(secondMinNum(nums));
// System.out.println(secondMinNum2(nums));
// System.out.println(secondMinNum3(nums));
// // two sum
// int[] nums = {3,2,4};
// int[] result = twoSum2(nums, 6);
// System.out.println(result[0] + "," + result[1]);
// // three sum
// int[] nums = {-1, 0, 1, 2, -1, -4};
// int[] result = threeSum(nums, 0);
// System.out.println(result[0] + "," + result[1] + ", " + result[2]);
}
}