Skip to content

Commit 8d07243

Browse files
committed
deploy: 606e39e
0 parents  commit 8d07243

296 files changed

Lines changed: 16158 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.nojekyll

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// ApartmentHunting+.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 25/03/22.
6+
//
7+
8+
import Foundation
9+
10+
// T: O(BR), S: O(BR)
11+
func apartmentHunting1(_ blocks: [[String: Bool]], _ requirements: [String]) -> Int {
12+
var distances = [[Int]]()
13+
for idx in stride(from: 0, to: requirements.count, by: 1) {
14+
distances.append(findDistances(for: requirements[idx], blocks: blocks))
15+
}
16+
var minDistance = Int(Int32.max)
17+
var nearestIndex = -1
18+
for idx in stride(from: 0, to: distances[0].count, by: 1) {
19+
var maxDistance = Int(Int32.min)
20+
for rdx in stride(from: 0, to: requirements.count, by: 1) {
21+
maxDistance = max(maxDistance, distances[rdx][idx])
22+
}
23+
if maxDistance < minDistance {
24+
minDistance = maxDistance
25+
nearestIndex = idx
26+
}
27+
}
28+
29+
return nearestIndex
30+
}
31+
32+
func findDistances(for requirement: String, blocks: [[String: Bool]]) -> [Int] {
33+
var distances = Array(repeating: Int(Int32.min), count: blocks.count)
34+
var previous = Int(Int32.min)
35+
for idx in stride(from: 0, to: blocks.count, by: 1) {
36+
let block = blocks[idx]
37+
if let isReqExists = block[requirement], isReqExists {
38+
distances[idx] = 0
39+
previous = 0
40+
} else {
41+
if previous >= 0 {
42+
distances[idx] = distances[idx - 1] + 1
43+
previous = distances[idx]
44+
}
45+
}
46+
}
47+
48+
for idx in stride(from: distances.count - 2, through: 0, by: -1) {
49+
if distances[idx] < 0 {
50+
distances[idx] = distances[idx + 1] + 1
51+
} else {
52+
distances[idx] = min(distances[idx + 1] + 1, distances[idx])
53+
}
54+
}
55+
return distances
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// ApartmentHunting.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 25/03/22.
6+
//
7+
8+
import Foundation
9+
10+
// T: O(B^2R), S: O(1)
11+
func apartmentHunting(_ blocks: [[String: Bool]], _ requirements: [String]) -> Int {
12+
var nearestIdx = -1
13+
var distance = Int(Int32.max)
14+
for idx in stride(from: 0, to: blocks.count, by: 1) {
15+
var maximumDistance = Int(Int32.min)
16+
for req in requirements {
17+
var minimumDistance = Int(Int32.max)
18+
for jdx in stride(from: 0, to: blocks.count, by: 1) {
19+
let block = blocks[jdx]
20+
if let isExist = block[req], isExist {
21+
minimumDistance = min(minimumDistance, abs(jdx - idx))
22+
}
23+
}
24+
maximumDistance = max(maximumDistance, minimumDistance)
25+
}
26+
if maximumDistance < distance {
27+
distance = maximumDistance
28+
nearestIdx = idx
29+
}
30+
}
31+
return nearestIdx
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// ArrayOfProducts.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 18/07/21.
6+
//
7+
8+
import Foundation
9+
10+
/**
11+
Time Complexity: O(n)
12+
Space Complexity: O(n)
13+
Note:
14+
*/
15+
func arrayOfProducts(_ array: [Int]) -> [Int] {
16+
var products = Array(repeating: 1, count: array.count)
17+
var leftRunningProducts = Array(repeating: 1, count: array.count)
18+
var rightRunningProducts = Array(repeating: 1, count: array.count)
19+
for idx in stride(from: 1, to: array.count, by: 1) {
20+
leftRunningProducts[idx] = leftRunningProducts[idx - 1] * array[idx - 1]
21+
}
22+
23+
for idx in stride(from: array.count - 2, through: 0, by: -1) {
24+
rightRunningProducts[idx] = rightRunningProducts[idx + 1] * array[idx + 1]
25+
}
26+
27+
for idx in stride(from: 0, to: array.count, by: 1) {
28+
products[idx] = leftRunningProducts[idx] * rightRunningProducts[idx]
29+
}
30+
return products
31+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// BSTConstruction.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 24/07/21.
6+
//
7+
8+
import Foundation
9+
10+
extension BinarySearchTree {
11+
/**
12+
Time Complexity: O(logn)
13+
Space Complexity: O(1)
14+
Note:
15+
*/
16+
func insert(value: Int) -> BinarySearchTree {
17+
var currentNode: BinarySearchTree? = self
18+
while let node = currentNode {
19+
if value < node.value {
20+
if node.left == nil {
21+
node.left = BinarySearchTree(value: value)
22+
break
23+
} else {
24+
currentNode = node.left
25+
}
26+
} else {
27+
if node.right == nil {
28+
node.right = BinarySearchTree(value: value)
29+
break
30+
} else {
31+
currentNode = node.right
32+
}
33+
}
34+
}
35+
return self
36+
}
37+
38+
/**
39+
Time Complexity: O(logn)
40+
Space Complexity: O(1)
41+
Note:
42+
*/
43+
func contains(value: Int) -> Bool {
44+
var currentNode: BinarySearchTree? = self
45+
while let node = currentNode {
46+
if value < node.value {
47+
currentNode = node.left
48+
} else if value > node.value {
49+
currentNode = node.right
50+
} else {
51+
return true
52+
}
53+
}
54+
return false
55+
}
56+
57+
/**
58+
Time Complexity: O(logn)
59+
Space Complexity: O(1)
60+
Note:
61+
*/
62+
func remove(value: Int, parentNode: BinarySearchTree?) -> BinarySearchTree {
63+
var currentNode: BinarySearchTree? = self
64+
var parent = parentNode
65+
while let node = currentNode {
66+
if value < node.value {
67+
parent = currentNode
68+
currentNode = node.left
69+
} else if value > node.value {
70+
parent = currentNode
71+
currentNode = node.right
72+
} else {
73+
//1. When the nodes to be deleted has both left and right child node
74+
//2. When the node to be deleted is the root node (parent node is nil) and it has either left branch or right branch
75+
//3. When the node to be deleted has parent node and node to be deleted is either the only left or right child of parent node
76+
if let _ = node.left, let rightNode = node.right {
77+
node.value = rightNode.getMinimumValue()
78+
let _ = rightNode.remove(value: node.value, parentNode: node)
79+
} else if parent == nil {
80+
if let leftNode = node.left {
81+
node.value = leftNode.value
82+
node.right = leftNode.right
83+
node.left = leftNode.left
84+
} else if let rightNode = node.right {
85+
node.value = rightNode.value
86+
node.left = rightNode.left
87+
node.right = rightNode.right
88+
} else {
89+
90+
}
91+
} else if let parent = parent, node === parent.left {
92+
if let newLeft = node.left {
93+
parent.left = newLeft
94+
} else {
95+
parent.left = node.right
96+
}
97+
} else if let parent = parent, node === parent.right {
98+
if let newLeft = node.left {
99+
parent.right = newLeft
100+
} else {
101+
parent.right = node.right
102+
}
103+
}
104+
break
105+
}
106+
}
107+
return self
108+
}
109+
110+
func getMinimumValue() -> Int {
111+
var currentNode: BinarySearchTree = self
112+
while let node = currentNode.left {
113+
currentNode = node
114+
}
115+
return currentNode.value
116+
}
117+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// BSTTraversal.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 19/07/21.
6+
//
7+
8+
import Foundation
9+
10+
fileprivate class BST {
11+
var value: Int
12+
var left: BST?
13+
var right: BST?
14+
15+
init(value: Int) {
16+
self.value = value
17+
}
18+
}
19+
/**
20+
Time Complexity: O(n)
21+
Space Complexity: O(n)
22+
Note:
23+
*/
24+
fileprivate func inOrderTraversal(tree: BST?, array: inout [Int]) -> [Int] {
25+
if let node = tree {
26+
_ = inOrderTraversal(tree: node.left, array: &array)
27+
array.append(node.value)
28+
_ = inOrderTraversal(tree: node.right, array: &array)
29+
}
30+
return array
31+
}
32+
33+
/**
34+
Time Complexity: O(n)
35+
Space Complexity: O(n)
36+
Note:
37+
*/
38+
fileprivate func preOrderTraversal(tree: BST?, array: inout [Int]) -> [Int] {
39+
if let node = tree {
40+
array.append(node.value)
41+
_ = preOrderTraversal(tree: node.left, array: &array)
42+
_ = preOrderTraversal(tree: node.right, array: &array)
43+
}
44+
return array
45+
}
46+
47+
/**
48+
Time Complexity: O(n)
49+
Space Complexity: O(n)
50+
Note:
51+
*/
52+
fileprivate func postOrderTraversal(tree: BST?, array: inout [Int]) -> [Int] {
53+
if let node = tree {
54+
_ = postOrderTraversal(tree: node.left, array: &array)
55+
_ = postOrderTraversal(tree: node.right, array: &array)
56+
array.append(node.value)
57+
}
58+
return array
59+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// BalancedBrackets.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 31/03/22.
6+
//
7+
8+
import Foundation
9+
10+
// T: O(N), S: O(N)
11+
func balancedBrackets(string: String) -> Bool {
12+
var stack = [Character]()
13+
for char in string {
14+
if char == ")" || char == "}" || char == "]" {
15+
let lastBracket = pop(&stack)
16+
if lastBracket != openingBracket(for: char) {
17+
return false
18+
}
19+
} else {
20+
_ = push(char, &stack)
21+
}
22+
}
23+
return stack.isEmpty
24+
}
25+
26+
func push(_ char: Character, _ stack: inout [Character]) -> Bool {
27+
if char == "(" || char == "{" || char == "[" {
28+
stack.append(char)
29+
return true
30+
}
31+
return false
32+
}
33+
34+
func pop(_ stack: inout [Character]) -> Character? {
35+
guard !stack.isEmpty else { return nil }
36+
return stack.removeLast()
37+
}
38+
39+
func openingBracket(for bracket: Character) -> Character {
40+
if bracket == ")" {
41+
return "("
42+
} else if bracket == "}" {
43+
return "{"
44+
} else if bracket == "]" {
45+
return "["
46+
} else {
47+
// this should not come here
48+
}
49+
return "."
50+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// BinarySearch.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 17/07/21.
6+
//
7+
8+
import Foundation
9+
10+
/**
11+
Time Complexity: O(logn)
12+
Space Complexity: O(logn)
13+
Note:
14+
*/
15+
func binarySearch(array: [Int], target: Int) -> Int {
16+
return binarySearchHelper(array: array, target: target, start: 0, end: array.count - 1)
17+
}
18+
19+
func binarySearchHelper(array: [Int], target: Int, start: Int, end: Int) -> Int {
20+
if start > end { return -1 }
21+
let mid = start + (end - start) / 2
22+
if target < array[mid] {
23+
return binarySearchHelper(array: array, target: target, start: start, end: mid - 1)
24+
} else if target > array[mid] {
25+
return binarySearchHelper(array: array, target: target, start: mid + 1, end: end)
26+
} else {
27+
return mid
28+
}
29+
}

0 commit comments

Comments
 (0)