Posts

LeetCode Blind 75 in Swift: Solving the "3Sum" Problem

Problem: 3Sum The 3Sum problem asks you to find all unique triplets in the array that give the sum of zero. Each triplet should be listed only once, even if multiple combinations can produce the same triplet. Example: Input: nums = [-1, 0, 1, 2, -1, -4] Output: [[-1, -1, 2], [-1, 0, 1]] Explanation: The two triplets with sum zero are [-1, -1, 2] and [-1, 0, 1]. Swift Solution class Solution { func threeSum(_ nums: [Int]) -> [[Int]] { let nums = nums.sorted() var result = [[Int]]() for i in 0.. 0 && nums[i] == nums[i - 1] { continue } var left = i + 1 var right = nums.count - 1 while left Explanation In this solution, the array is first sorted, which helps in efficiently finding triplets that sum to zero. For each element, we use the two-pointer technique to find pairs that together with the ...

LeetCode Blind 75 in Swift: Solving the "Search in Rotated Sorted Array" Problem

Problem: Search in Rotated Sorted Array The Search in Rotated Sorted Array problem asks you to search for a target value in a rotated sorted array of unique integers. If the target exists, return its index. If not, return -1. Example: Input: nums = [4, 5, 6, 7, 0, 1, 2], target = 0 Output: 4 Swift Solution class Solution { func search(_ nums: [Int], _ target: Int) -> Int { var left = 0 var right = nums.count - 1 while left <= right { let mid = (left + right) / 2 if nums[mid] == target { return mid } // Determine which part is sorted if nums[left] <= nums[mid] { // Left side is sorted if target >= nums[left] && target < nums[mid] { right = mid - 1 } else { left = mid + 1 } ...

LeetCode Blind 75 in Swift: Solving the "Find Minimum in Rotated Sorted Array" Problem

Problem: Find Minimum in Rotated Sorted Array The Find Minimum in Rotated Sorted Array problem asks you to find the minimum element in a rotated sorted array of unique elements. Example: Input: nums = [3, 4, 5, 1, 2] Output: 1 Explanation: The minimum value is 1. Swift Solution class Solution { func findMin(_ nums: [Int]) -> Int { var left = 0 var right = nums.count - 1 while left nums[right] { left = mid + 1 } else { right = mid } } return nums[left] } } let solution = Solution() let nums = [3, 4, 5, 1, 2] let result = solution.findMin(nums) print(result) // Output: 1 Explanation This problem can be solved efficiently using binary search. The rotated sorted array has two sorted halves. By comparing the middle element with the rightmost element, we can determine which ha...

LeetCode Blind 75 in Swift: Solving the "Maximum Product Subarray" Problem

Problem: Maximum Product Subarray The Maximum Product Subarray problem asks you to find the contiguous subarray within an array (containing at least one number) that has the largest product. Example: Input: nums = [2, 3, -2, 4] Output: 6 Explanation: [2, 3] has the largest product = 6. Swift Solution class Solution { func maxProduct(_ nums: [Int]) -> Int { var currentMax = nums[0] var currentMin = nums[0] var result = nums[0] for num in nums.dropFirst() { let tempMax = currentMax currentMax = max(num, currentMax * num, currentMin * num) currentMin = min(num, tempMax * num, currentMin * num) result = max(result, currentMax) } return result } } let solution = Solution() let nums = [2, 3, -2, 4] let result = solution.maxProduct(nums) print(result) // Output: 6 Explanation In...

LeetCode Blind 75 in Swift: Solving the "Product of Array Except Self" Problem

Problem: Product of Array Except Self The Product of Array Except Self problem asks you to return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i] . Example: Input: nums = [1, 2, 3, 4] Output: [24, 12, 8, 6] Swift Solution class Solution { func productExceptSelf(_ nums: [Int]) -> [Int] { let n = nums.count var answer = Array(repeating: 1, count: n) var leftProduct = 1 for i in 0.. Explanation In this solution, we first calculate the product of all elements to the left of each element and store it in the answer array. Then, we calculate the product of all elements to the right of each element and multiply it with the corresponding value in the answer array. This approach avoids division and runs in O(n) time.

LeetCode Blind 75 in Swift: Solving the "Contains Duplicate" Problem

Problem: Contains Duplicate The Contains Duplicate problem asks you to determine if a given array contains any duplicates. Your task is to return true if any value appears at least twice in the array, and false if every element is distinct. Example: Input: nums = [1, 2, 3, 1] Output: true Explanation: 1 appears twice in the array, so we return true. Swift Solution class Solution { func containsDuplicate(_ nums: [Int]) -> Bool { var numSet = Set () for num in nums { if numSet.contains(num) { return true } numSet.insert(num) } return false } } let solution = Solution() let nums = [1, 2, 3, 1] let result = solution.containsDuplicate(nums) print(result) // Output: true Explanation This solution leverages a Set in Swift, which stores unique elements. By iterating through the array, we check if th...

LeetCode Blind 75 in Swift: Solving the "Best Time to Buy and Sell Stock" Problem

LeetCode Blind 75 in Swift: Solving the "Best Time to Buy and Sell Stock" Problem Problem: Best Time to Buy and Sell Stock The Best Time to Buy and Sell Stock problem asks for the maximum profit you can achieve from buying and selling a stock. Given an array where prices[i] represents the price of a given stock on day i , find the maximum profit you can achieve. You may not engage in multiple transactions (you must sell the stock before you buy again). Example: 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. Swift Solution class Solution { func maxProfit(_ prices: [Int]) -> Int { var minPrice = Int.max var maxProfit = 0 for price in prices { if price maxProfit { maxProfit = price - minPrice } } return maxProfit ...