Array
Notes and typical questions for array related problems
Notes
// Two-way partition // it divides the array into 2 parts // elements < pivot and >= pivot (or elements <= pivot and >= > pivot), // making it simple but inefficient for duplicate values. private int[] twoWaysPartition(int[] nums, int left, int right) { int pivot = nums[right]; int i = left; for (int j = left; j < right; j++) { if (nums[j] < pivot) { swap(nums, i, j); i++; } } swap(nums, i, right); return i; } // Three-way partition (AKA Dutch National Flag Problem => LC 75) // it further splits the array into 3 parts // element < pivot, == pivot, and > pivot, // reducing redundant swaps and improving QuickSort efficiency, especially when duplicates are common. private int[] threeWayspartition(int[] nums, int left, int right) { int pivot = nums[right]; int i = left; int j = left; int k = right; while (j <= k) { if (nums[j] < pivot) { swap(nums, i, j); i++; j++; } else if (nums[j] > pivot) { swap(nums, j, k); k--; } else { j++; } } return new int[]{i,k}; }
Typical Questions
Matrix
Boyer-Moore Voting Algorithm
Sorting Algorithm
DNF algorithm
Other Array Questions
Simulation
Last updated
