Language computer scientists use when comparing the performance of algorithms.
time Complexity
Space Complexity
※ bigocheatsheet.com
func commonItemsHash(_ A: [Int], _ B: [Int]) -> Bool {
var hashA = [Int: Bool]() //O(n)
for a in A { // O(n)
hashA[a] = true
}
for b in B {
if hashA[b] == true {
return true
}
}
return false
}
func commonItemsBrute(_ A: [Int], _ B: [Int]) -> Bool {
for i in 0..<A.count {
for j in 0..<B.count {
if A[i] == B[j] {
return true
}
}
}
return false
}
Hashmap | BruteForce | |
---|---|---|
time Complexity | O(n) | O(n^2) |
Space Complexity | O(n) | O(1) |