Skip to content

Commit a991725

Browse files
committed
Part of Leetcode top 150 interview questions
1 parent a3dd46b commit a991725

File tree

33 files changed

+591
-1
lines changed

33 files changed

+591
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import UIKit
2+
3+
func maxArea(_ height: [Int]) -> Int {
4+
var left = 0, right = height.count - 1, maxLeft = height[left], maxRight = height[right], waterFilled = 0, maxDifference = 0
5+
while left < right {
6+
maxLeft = height[left]
7+
maxRight = height[right]
8+
if maxLeft < maxRight {
9+
if (maxLeft * maxRight) - (right - left) > waterFilled , right - left > maxDifference {
10+
maxDifference = right - left
11+
waterFilled = (maxLeft * maxRight) - (right - left)
12+
print("left = \(left), right = \(right)")
13+
print("left waterFilled = \(waterFilled)")
14+
}
15+
left += 1
16+
}
17+
else {
18+
if (maxLeft * maxRight) - (right - left) > waterFilled, right - left > maxDifference {
19+
maxDifference = right - left
20+
waterFilled = (maxLeft * maxRight) - (right - left)
21+
print("right waterFilled = \(waterFilled)")
22+
23+
}
24+
right -= 1
25+
}
26+
}
27+
return waterFilled
28+
}
29+
30+
print("\(maxArea([1,8,6,2,5,4,8,3,7]))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

DataStrucutureQuestions/Array/134. Gas Station.playground/Contents.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@ func canCompleteCircuit(_ gas: [Int], _ cost: [Int]) -> Int {
1717
}
1818
return result
1919
}
20-
2120
print("starting gas station's index is :- \(canCompleteCircuit([1,2,3,4,5], [3,4,5,1,2]))")
2221
print("starting gas station's index is :- \(canCompleteCircuit([2,3,4], [3,4,3]))")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/longest-common-prefix/description/?envType=study-plan-v2&envId=top-interview-150
4+
5+
func longestCommonPrefix(_ strs: [String]) -> String {
6+
if strs.isEmpty { return ""}
7+
if strs.count == 1 { return strs.last! }
8+
let sortedArray = strs.sorted()
9+
var output = ""
10+
for (char1, char2) in zip(sortedArray.first!, sortedArray.last!) {
11+
char1
12+
if char1 == char2 {
13+
output += String(char1)
14+
} else {
15+
break
16+
}
17+
}
18+
return output
19+
}
20+
21+
print("Longest common prefix of [flower, flow, flight] is :- \(longestCommonPrefix(["flower","flow","flight"]))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import UIKit
2+
3+
func threeSum(_ nums: [Int]) -> [[Int]] {
4+
if nums.count < 3 {
5+
return []
6+
}
7+
var sortedArray = nums.sorted()
8+
var result = Set<[Int]>()
9+
for index in 0..<sortedArray.count {
10+
var lowIndex = index + 1
11+
var highIndex = nums.count - 1
12+
while lowIndex < highIndex {
13+
let sum = sortedArray[index] + sortedArray[lowIndex] + sortedArray[highIndex]
14+
if sum > 0 {
15+
highIndex -= 1
16+
} else if sum < 0 {
17+
lowIndex += 1
18+
} else {
19+
result.insert([sortedArray[index], sortedArray[lowIndex], sortedArray[highIndex] ])
20+
lowIndex += 1
21+
highIndex -= 1
22+
}
23+
}
24+
}
25+
return Array(result)
26+
}
27+
print("all triplets combination of array [-1,0,1,2,-1,-4] is: \(threeSum([-1,0,1,2,-1,-4]))")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import UIKit
2+
3+
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
4+
var lowerIndex = 0, higherIndex = numbers.count - 1
5+
while lowerIndex < higherIndex {
6+
if numbers[lowerIndex] + numbers[higherIndex] > target {
7+
higherIndex -= 1
8+
} else if numbers[lowerIndex] + numbers[higherIndex] < target {
9+
lowerIndex += 1
10+
} else {
11+
return [lowerIndex + 1, higherIndex + 1]
12+
}
13+
}
14+
return []
15+
}
16+
17+
print("\(twoSum([2,7,11,15], 9))")
18+
print("\(twoSum([2,3,4], 6))")
19+
print("\(twoSum([-1,0], -1))")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/minimum-size-subarray-sum/description/?envType=study-plan-v2&envId=top-interview-150
4+
//Time Complexity O(n)
5+
6+
func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
7+
var left = 0, total = 0
8+
var result = Int.max
9+
for right in 0..<nums.count {
10+
total += nums[right]
11+
while total >= target {
12+
result = min(right - left + 1, result)
13+
total -= nums[left]
14+
left += 1
15+
}
16+
}
17+
return result == Int.max ? 0 : result
18+
}
19+
20+
print("minimum window subarray of target 7 is \(minSubArrayLen(7, [2,3,1,2,4,3]))" )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/sliding-window-maximum/
4+
5+
/*Example :-
6+
What should be the output if the following input is given?
7+
nums = [1, 2, 3, 1, 4, 5, 2, 3, 6]
8+
w = 3
9+
10+
Ans :- The first window of size 3 is [1, 2, 3], and the maximum in this window is 3. The second window of size is [2, 3, 1], and the maximum in this window is also 3. The third window of size 3 is [3, 1, 4], and the maximum in this window is 4. The fourth window of size 3 is [1, 4, 5], and the maximum in this window is 5. The fifth window of size 3 is [4, 5, 2], and the maximum in this window is also 5. The sixth window of size 3 is [5, 2, 3], and the maximum in this window is also 5. The seventh and the last window of size 3 is [2, 3, 6], and the maximum in this window is 6.
11+
= [3, 3, 4, 5, 5, 5, 6]
12+
*/
13+
14+
//func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
15+
// var maxValue = 0
16+
// var windowArray : [Int] = []
17+
// var maxSlidingWindow : [Int] = []
18+
// var start = 0
19+
// while start < nums.count {
20+
// if start == 0 {
21+
// for wIndex in start..<k {
22+
// windowArray.append(nums[wIndex])
23+
// maxValue = max(maxValue, nums[wIndex])
24+
// }
25+
// maxSlidingWindow.append(maxValue)
26+
// start += k
27+
// print(start)
28+
// } else {
29+
// windowArray.removeFirst()
30+
// windowArray.append(nums[start])
31+
// maxValue = max(maxValue, nums[start])
32+
// maxSlidingWindow.append(maxValue)
33+
// start += 1
34+
// }
35+
// }
36+
// return maxSlidingWindow
37+
//}
38+
39+
func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
40+
guard nums.isEmpty == false else { return [] }
41+
42+
var windowArray = [Int]()
43+
var outputArray = [Int]()
44+
45+
for i in 0..<nums.count {
46+
while (windowArray.count > 0) && (nums[windowArray.last!] < nums[i]) {
47+
windowArray.removeLast()
48+
}
49+
windowArray.append(i)
50+
51+
if i >= k - 1 {
52+
if windowArray.first! + k == i { windowArray.removeFirst() }
53+
outputArray.append(nums[windowArray.first!])
54+
}
55+
}
56+
return outputArray
57+
}
58+
59+
print(maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/where-will-the-ball-fall/
4+
5+
//Time complexity :- O(m * n) Here, mis the number of rows, and n is the number of columns.
6+
//SpaceComplexity : O(1)
7+
8+
func findBall(_ grid: [[Int]]) -> [Int] {
9+
var result = Array(repeating: -1, count: grid[0].count)
10+
11+
for col in 0..<grid[0].count {
12+
var current_col = col
13+
14+
for row in 0..<grid.count {
15+
let next_col = current_col + grid[row][current_col]
16+
17+
if next_col < 0 || next_col > grid[0].count - 1 || grid[row][current_col] != grid[row][next_col] {
18+
break
19+
}
20+
if row == grid.count - 1 {
21+
result[col] = next_col
22+
}
23+
current_col = next_col
24+
}
25+
}
26+
return result
27+
}
28+
print("--FIND BALL FALL EXAMPLE--")
29+
print(findBall([[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]))
30+
print(findBall([[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]))
31+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import UIKit
2+
3+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/set-matrix-zeroes/
4+
5+
6+
func setZeroes(_ matrix: inout [[Int]]) {
7+
var rows = matrix.count
8+
var columns = matrix[0].count
9+
var fRow = false
10+
var fColumn = false
11+
12+
for i in 0..<rows {
13+
if matrix[i][0] == 0 {
14+
fColumn = true
15+
}
16+
}
17+
18+
for j in 0..<columns {
19+
if matrix[0][j] == 0 {
20+
fRow = true
21+
}
22+
}
23+
24+
for row in 1..<rows {
25+
for col in 1..<columns {
26+
if matrix[row][col] == 0 {
27+
matrix[0][col] = 0
28+
matrix[row][0] = 0
29+
}
30+
}
31+
}
32+
33+
for row in 1..<rows {
34+
if matrix[row][0] == 0 {
35+
for col in 1..<columns {
36+
matrix[row][col] = 0
37+
}
38+
}
39+
}
40+
41+
for col in 1..<columns {
42+
if matrix[0][col] == 0 {
43+
for row in 1..<rows {
44+
matrix[row][col] = 0
45+
}
46+
}
47+
}
48+
49+
if fColumn == true {
50+
for row in 0..<rows {
51+
matrix[row][0] = 0
52+
}
53+
}
54+
55+
if fRow == true {
56+
for col in 0..<columns {
57+
matrix[0][col] = 0
58+
}
59+
}
60+
}
61+
var matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]// [[1,1,1],[1,0,1],[1,1,1]]
62+
setZeroes(&matrix)
63+
print(matrix)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import UIKit
2+
3+
//https://leetcode.com/problems/repeated-dna-sequences/description/
4+
//The average case time complexity of this solution is O(n), where n is the length of the input string.
5+
func find_repeated_sequences(s: String, k: Int) -> [String] {
6+
let window_size = k
7+
if s.count <= window_size {
8+
return []
9+
}
10+
let base = 4
11+
let hi_place_value = pow(Double(base), Double(window_size - 1))
12+
let mapping: [Character: Int] = ["A": 1, "C": 2, "G": 3, "T": 4]
13+
var numbers: [Int] = []
14+
for char in s {
15+
numbers.append(mapping[char]!)
16+
}
17+
var hashing = 0
18+
var substring_hashes: Set<Int> = []
19+
var output: Set<String> = []
20+
for start in 0..<(s.count - window_size + 1) {
21+
if start != 0 {
22+
hashing = (hashing - numbers[start - 1] * Int(hi_place_value)) * base
23+
+ numbers[start + window_size - 1]
24+
} else {
25+
for end in 0..<window_size {
26+
hashing = hashing * base + numbers[end]
27+
}
28+
}
29+
if substring_hashes.contains(hashing) {
30+
output.insert(String(s[s.index(s.startIndex, offsetBy: start)..<s.index(s.startIndex, offsetBy: start + window_size)]))
31+
}
32+
substring_hashes.insert(hashing)
33+
}
34+
return Array(output)
35+
}
36+
37+
print(find_repeated_sequences(s: "AAAAACCCCCAAAAACCCCCC", k: 10))
38+
//print(pow(4.00, 9.00) + pow(4.00, 8.00) + pow(4.00, 7.00) + pow(4.00, 6.00) + pow(4.00, 5.00) + (pow(4.00, 4.00) * 2) + (pow(4.00, 3.00) * 2) + (pow(4.00, 2.00) * 2) + (pow(4.00, 1.00) * 2) + (pow(4.00, 0.00) * 2) )
39+
print(find_repeated_sequences(s: "AGACCTAGAC", k: 10))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)