1D Array Problems
Original12/22/25About 4 min
Q26. Remove Duplicates from Sorted Array
class Solution { public int removeDuplicates(int[] nums) { int k = 1, prev = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] != prev) { nums[k++] = nums[i]; prev = nums[i]; } } return k; } }
Q27. Remove Element
class Solution { public int removeElement(int[] nums, int val) { if (nums.length == 0) return 0; int k = 0, size = nums.length, i = 0; while (i < size - k) { if (nums[i] == val) { nums[i] = nums[size - 1 - k]; k++; } else i++; } return size - k; } }
Q80. Remove Duplicates from Sorted Array II
class Solution { public int removeDuplicates(int[] nums) { int k = 1, prev = nums[0], count = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] == prev) { if (count == 1) { nums[k++] = nums[i]; count++; } } else { nums[k++] = nums[i]; prev = nums[i]; count = 1; } } return k; } }
Q88. Merge Sorted Array
class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { if (n == 0) return; if (m == 0) { for (int i = 0; i < n; i++) nums1[i] = nums2[i]; return; } // move all prefix 0 in nums1 to suffix 0 for (int j = m + n - 1; j >= n; j--) { nums1[j] = nums1[j - n]; } for (int j = 0; j < n; j++) nums1[j] = 0; int a1 = n, a2 = 0, a = 0; while (a1 < m + n && a2 < n) { if (nums1[a1] <= nums2[a2]) nums1[a++] = nums1[a1++]; else nums1[a++] = nums2[a2++]; } while (a2 < n) { nums1[a++] = nums2[a2++]; } } }
Q121. Best Time to Buy and Sell Stock
class Solution { public int maxProfit(int[] prices) { int profit = 0, min = prices[0]; for (int i = 0; i < prices.length; i++) { int p = prices[i] - min; if (p < 0) min = prices[i]; else profit = Math.max(profit, p); } return profit; } }
Q122. Best Time to Buy and Sell Stock II
class Solution { public int maxProfit(int[] prices) { int profit = 0; for (int i = 1; i < prices.length; i++) { int p = prices[i] - prices[i - 1]; if (p > 0) profit += p; } return profit; } }
⭐Q169. Majority Element
class Solution { public int majorityElement(int[] nums) { // brute-force: hashmap time: O(n) space: O(n) // sort and count time: O(nlogn) space: O(1) // aim: O(n), O(1) Boyer–Moore majority vote algorithm // start with thinking of two elements A and B, add and subtract 1 in the loop // then expand to majority A and others, add and subtract 1, same concept int majority = nums[0], count = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] == majority) count++; else { if (count == 0) { majority = nums[i]; count = 1; } else count--; } } return majority; } }
If a second pass is not performed and there is no majority, the algorithm will not detect that no majority exists.
Q189. Rotate Array
class Solution { public void rotate(int[] nums, int k) { int size = nums.length; k = k % size; reverse(nums, 0, size - 1); reverse(nums, 0, k - 1); reverse(nums, k, size - 1); } private void reverse(int[] nums, int start, int end) { while (start < end) { int tmp = nums[start]; nums[start] = nums[end]; nums[end] = tmp; start++; end--; } } }
⭐Q274. H-Index
class Solution { public int hIndex(int[] citations) { int[] buckets = new int[1001]; for(int i: citations) { buckets[i]++; } int count = 0, i = 1000; while(i > 0) { count += buckets[i]; if(count >= i) return i; i--; } return 0; } }
⭐Q380. Insert Delete GetRandom O(1)
class RandomizedSet { public static final int SIZE = 200_001; private Map<Integer, Integer> valToIndexMap; private int[] vals; private int size; public RandomizedSet() { this.valToIndexMap = new HashMap<Integer,Integer>(); this.vals = new int[SIZE]; this.size = 0; } public boolean insert(int val) { if (valToIndexMap.containsKey(val)) return false; else { valToIndexMap.put(val, size); vals[size++] = val; return true; } } public boolean remove(int val) { if (valToIndexMap.containsKey(val)) { int index = valToIndexMap.get(val); int lastElement = vals[size - 1]; vals[index] = lastElement; valToIndexMap.put(lastElement, index); valToIndexMap.remove(val); size--; return true; } else return false; } public int getRandom() { int index = randomNumber(0, size); return vals[index]; } private int randomNumber(int min, int max) { int randomNum = min + (int)(Math.random() * (max - min)); return randomNum; } } /** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet obj = new RandomizedSet(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
Q539. Minimum Time Difference
class Solution { public int findMinDifference(List<String> timePoints) { if (timePoints.size() > 1440) return 0; boolean[] seen = new boolean[1440]; for (String time : timePoints) { int minutes = convertToMinutes(time); if (seen[minutes]) return 0; seen[minutes] = true; } int first = -1, prev = -1; int minDiff = Integer.MAX_VALUE; for (int i = 0; i < 1440; i++) { if (seen[i]) { if (first < 0) { first = i; } else { minDiff = Math.min(minDiff, i - prev); } prev = i; } } minDiff = Math.min(minDiff, 1440 - prev + first); return minDiff; } private int convertToMinutes(String time) { return ((time.charAt(0) - '0') * 10 + (time.charAt(1) - '0')) * 60 + (time.charAt(3) - '0') * 10 + (time.charAt(4) - '0'); } }
Q605. Can Place Flowers
class Solution { public boolean canPlaceFlowers(int[] flowerbed, int n) { int pointer = 0; while(pointer < flowerbed.length && n>=0){ if(flowerbed[pointer] == 1){ pointer += 2; } else{ if(pointer + 1 > flowerbed.length - 1){ n--; pointer += 1; } else if(flowerbed[pointer + 1] == 0){ n--; pointer += 2; } else{ pointer += 3; } } } return n <= 0; } }
Q1431. Kids With the Greatest Number of Candies
class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max = 0; for (int c : candies) { if (max < c) max = c; } List<Boolean> l = new ArrayList<>(); for (int c : candies) { if (c + extraCandies >= max) l.add(true); else l.add(false); } return l; } }
Q1894. Find the Student that Will Replace the Chalk
class Solution { public int chalkReplacer(int[] chalk, int k) { long sum = 0; for (int i = 0; i < chalk.length; i++) sum += chalk[i]; long left = (long) k % sum; for (int i = 0; i < chalk.length; i++) { if (left < chalk[i]) return i; left -= chalk[i]; } return 0; } }
Q2022. Convert 1D Array Into 2D Array
class Solution { public int[][] construct2DArray(int[] original, int m, int n) { if (original.length != m * n) return new int[0][0]; int k = 0; int[][] res = new int[m][n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { res[i][j] = original[k]; k++; } return res; } }
