LeetCode 75
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Set<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++){ if (set.contains(nums[i])){ return true; } else { set.add(nums[i]); } } return false;
Best Time to Buy and Sell Stock You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
int min = Integer.MAX_VALUE; int maxDiff = 0; for (int i = 0; i < prices.length; i++){ if (prices[i] < min){ min = prices[i]; } if (prices[i]-min > maxDiff){ maxDiff = prices[i]-min; } } return maxDiff;
3Sum Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < nums.length && nums[i] <= 0; ++i) if (i == 0 || nums[i - 1] != nums[i]) { twoSum(nums, i, res); } return res; } void twoSum(int[] nums, int i, List<List<Integer>> res) { var seen = new HashSet<Integer>(); for (int j = i + 1; j < nums.length; ++j) { int complement = -nums[i] - nums[j]; if (seen.contains(complement)) { res.add(Arrays.asList(nums[i], nums[j], complement)); while (j + 1 < nums.length && nums[j] == nums[j + 1]) ++j; } seen.add(nums[j]); } } }
Find Minimum in Rotated Sorted Array Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Given the sorted rotated array nums of unique elements, return the minimum element of this array. Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5] rotated 3 times.
class Solution { public int findMin(int[] nums) { // If the list has just one element then return that element. if (nums.length == 1) { return nums[0]; } // initializing left and right pointers. int left = 0, right = nums.length - 1; // if the last element is greater than the first element then there is no rotation. // e.g. 1 < 2 < 3 < 4 < 5 < 7. Already sorted array. // Hence the smallest element is first element. A[0] if (nums[right] > nums[0]) { return nums[0]; } // Binary search way while (right >= left) { // Find the mid element int mid = left + (right - left) / 2; // if the mid element is greater than its next element then mid+1 element is the smallest // This point would be the point of change. From higher to lower value. if (nums[mid] > nums[mid + 1]) { return nums[mid + 1]; } // if the mid element is lesser than its previous element then mid element is the smallest if (nums[mid - 1] > nums[mid]) { return nums[mid]; } // if the mid elements value is greater than the 0th element this means // the least value is still somewhere to the right as we are still dealing with elements // greater than nums[0] if (nums[mid] > nums[0]) { left = mid + 1; } else { // if nums[0] is greater than the mid value then this means the smallest value is somewhere to // the left right = mid - 1; } } return -1; } }
Maximum Subarray Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.
class Solution { public int maxProduct(int[] nums) { if (nums.length == 0) return 0; int max_so_far = nums[0]; int min_so_far = nums[0]; int result = max_so_far; for (int i = 1; i < nums.length; i++) { int curr = nums[i]; int temp_max = Math.max(curr, Math.max(max_so_far * curr, min_so_far * curr)); min_so_far = Math.min(curr, Math.min(max_so_far * curr, min_so_far * curr)); max_so_far = temp_max; result = Math.max(max_so_far, result); } return result; } }
Search in Rotated Sorted Array There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4
class Solution { public int search(int[] nums, int target) { int start = 0, end = nums.length - 1; while (start <= end) { int mid = start + (end - start) / 2; if (nums[mid] == target) return mid; else if (nums[mid] >= nums[start]) { if (target >= nums[start] && target < nums[mid]) end = mid - 1; else start = mid + 1; } else { if (target <= nums[end] && target > nums[mid]) start = mid + 1; else end = mid - 1; } } return -1; } }
Product of Array Except Self Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. Input: nums = [1,2,3,4] Output: [24,12,8,6]
class Solution { public int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; int tmp = 1; for (int i = 0; i < nums.length; i++) { result[i] = tmp; tmp *= nums[i]; } tmp = 1; for (int i = nums.length - 1; i >= 0; i--) { result[i] *= tmp; tmp *= nums[i]; } return result; } }
Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++){ if (map.containsKey(target-nums[i])){ return new int[] {map.get(target-nums[i]), i}; } map.put(nums[i], i); } return null; } }
Container With Most Water Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
public class Solution { public int maxArea(int[] height) { int maxarea = 0, l = 0, r = height.length - 1; while (l < r) { maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l)); if (height[l] < height[r]) l++; else r--; } return maxarea; } }