forked from Ahmed-Mujahid/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # Sort/MeetingRooms.swift # Sort/MergeIntervals.swift
- Loading branch information
Showing
75 changed files
with
2,566 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ | ||
* Primary idea: Use a dictionary to track the sum so far since the first until the current | ||
* Time Complexity: O(n), Space Complexity: O(n) | ||
*/ | ||
|
||
class MaximumSizeSubarraySumEqualsK { | ||
func maxSubArrayLen(nums: [Int], _ k: Int) -> Int { | ||
var longestLen = 0 | ||
var dict = [Int: Int]() | ||
dict[0] = -1 | ||
var sum = 0 | ||
|
||
for i in 0 ..< nums.count { | ||
sum += nums[i] | ||
|
||
if let lastIndex = dict[sum - k] { | ||
longestLen = max(longestLen, i - lastIndex) | ||
} | ||
|
||
guard let index = dict[sum] else { | ||
dict[sum] = i | ||
continue | ||
} | ||
} | ||
|
||
return longestLen | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/product-of-array-except-self/ | ||
* Primary idea: Use two arrays to hold multiplication result from left and right sides | ||
* while iterating the original array | ||
* Time Complexity: O(n), Space Complexity: O(n) | ||
*/ | ||
|
||
class ProductExceptSelf { | ||
func productExceptSelf(nums: [Int]) -> [Int] { | ||
var res = [Int]() | ||
|
||
guard nums.count > 0 else { | ||
return res | ||
} | ||
|
||
let left = _initLeft(nums) | ||
let right = _initRight(nums) | ||
|
||
for i in 0 ..< nums.count { | ||
res.append(left[i] * right[i]) | ||
} | ||
|
||
return res | ||
} | ||
|
||
private func _initLeft(nums: [Int]) -> [Int] { | ||
var left = [Int]() | ||
left.append(1) | ||
|
||
for i in 1 ..< nums.count { | ||
left.append(left[i - 1] * nums[i - 1]) | ||
} | ||
|
||
return left | ||
} | ||
|
||
private func _initRight(nums: [Int]) -> [Int] { | ||
var right = Array(count: nums.count, repeatedValue: 1) | ||
|
||
for i in (nums.count - 2).stride(through: 0, by: -1) { | ||
right[i] = right[i + 1] * nums[i + 1] | ||
} | ||
|
||
return right | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/spiral-matrix/ | ||
* Primary idea: Use four index to get the right element during iteration | ||
* | ||
* Time Complexity: O(n^2), Space Complexity: O(1) | ||
*/ | ||
|
||
class SpiralMatrix { | ||
func spiralOrder(matrix: [[Int]]) -> [Int] { | ||
var res = [Int]() | ||
|
||
guard matrix.count != 0 else { | ||
return res | ||
} | ||
|
||
var startX = 0 | ||
var endX = matrix.count - 1 | ||
var startY = 0 | ||
var endY = matrix[0].count - 1 | ||
|
||
while true { | ||
// top | ||
for i in startY ... endY { | ||
res.append(matrix[startX][i]) | ||
} | ||
startX += 1 | ||
if startX > endX { | ||
break | ||
} | ||
|
||
// right | ||
for i in startX ... endX { | ||
res.append(matrix[i][endY]) | ||
} | ||
endY -= 1 | ||
if startY > endY { | ||
break | ||
} | ||
|
||
// bottom | ||
for i in endY.stride(through: startY, by: -1) { | ||
res.append(matrix[endX][i]) | ||
} | ||
endX -= 1 | ||
if startX > endX { | ||
break | ||
} | ||
|
||
// left | ||
for i in endX.stride(through: startX, by: -1) { | ||
res.append(matrix[i][startY]) | ||
} | ||
startY += 1 | ||
if startY > endY { | ||
break | ||
} | ||
} | ||
|
||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/3sum/ | ||
* Primary idea: Sort the array, and traverse it, increment left or decrease right | ||
* predicated on their sum is greater or not than the target | ||
* Time Complexity: O(n^2), Space Complexity: O(n) | ||
*/ | ||
|
||
class ThreeSum { | ||
func threeSum(nums: [Int]) -> [[Int]] { | ||
var nums = nums.sort({$0 < $1}) | ||
var res = [[Int]]() | ||
|
||
if nums.count <= 2 { | ||
return res | ||
} | ||
|
||
for i in 0 ... nums.count - 3 { | ||
if i == 0 || nums[i] != nums[i - 1] { | ||
var remain = -nums[i] | ||
var left = i + 1 | ||
var right = nums.count - 1 | ||
while left < right { | ||
if nums[left] + nums[right] == remain { | ||
var temp = [Int]() | ||
temp.append(nums[i]) | ||
temp.append(nums[left]) | ||
temp.append(nums[right]) | ||
|
||
res.append(temp) | ||
repeat { | ||
left += 1 | ||
} while (left < right && nums[left] == nums[left - 1]) | ||
repeat { | ||
right -= 1 | ||
} while (left < right && nums[right] == nums[right + 1]) | ||
} else if nums[left] + nums[right] < remain { | ||
repeat { | ||
left += 1 | ||
} while (left < right && nums[left] == nums[left - 1]) | ||
} else { | ||
repeat { | ||
right -= 1 | ||
} while (left < right && nums[right] == nums[right + 1]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/combination-sum/ | ||
* Primary idea: Classic Depth-first Search | ||
* | ||
* Time Complexity: O(n^n) | ||
* | ||
*/ | ||
|
||
class CombinationSum { | ||
func combinationSum(candidates: [Int], _ target: Int) -> [[Int]] { | ||
var res = [[Int]]() | ||
|
||
guard candidates.count > 0 else { | ||
return res | ||
} | ||
|
||
var path = [Int]() | ||
let candidates = candidates.sort({$0 < $1}) | ||
|
||
_dfs(candidates, target, &res, &path, 0) | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(candidates: [Int], _ target: Int, inout _ res: [[Int]], inout _ path: [Int], _ index: Int) { | ||
if target == 0 { | ||
res.append([Int](path)) | ||
return | ||
} | ||
|
||
for i in index ..< candidates.count { | ||
guard candidates[i] <= target else { | ||
break | ||
} | ||
|
||
path.append(candidates[i]) | ||
_dfs(candidates, target - candidates[i], &res, &path, i) | ||
path.removeLast() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/combinations/ | ||
* Primary idea: Classic Depth-first Search, another version of Subsets | ||
* | ||
* Time Complexity: O(n!), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class Combinations { | ||
func combine(n: Int, _ k: Int) -> [[Int]] { | ||
var res = [[Int]]() | ||
var path = [Int]() | ||
let nums = _init(n) | ||
|
||
_dfs(nums, &res, &path, 0, k) | ||
|
||
return res | ||
} | ||
|
||
private func _init(n: Int) -> [Int] { | ||
var res = [Int]() | ||
|
||
for i in 1 ... n { | ||
res.append(i) | ||
} | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(nums: [Int], inout _ res: [[Int]], inout _ path: [Int], _ index: Int, _ k: Int) { | ||
if path.count == k { | ||
res.append([Int](path)) | ||
return | ||
} | ||
|
||
for i in index ..< nums.count { | ||
path.append(nums[i]) | ||
_dfs(nums, &res, &path, i + 1, k) | ||
path.removeLast() | ||
} | ||
} | ||
} |
Oops, something went wrong.