This file contains hidden or 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
| func maxSumOfSubArray(_ a:[Int]) -> Int?{ | |
| guard a.count > 0 else { return nil } | |
| var maxCurrent = a[0] | |
| var maxGlobal = a[0] | |
| for i in a.dropFirst(){ | |
| maxCurrent = max(i, maxCurrent + i) | |
| if maxCurrent > maxGlobal{ | |
| maxGlobal = maxCurrent | |
| } | |
| } |
This file contains hidden or 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
| func uniquePaths(_ m: Int, _ n: Int) -> Int { | |
| var paths = [[Int]](repeating: [Int](repeating: 0, count: n+1), count: m+1) | |
| return helper(m, n, &paths) | |
| } | |
| func helper(_ row: Int, _ col: Int, _ paths: inout [[Int]]) -> Int{ | |
| if row == 1 || col == 1{ return 1 } | |
| if paths[row][col] == 0{ | |
| paths[row][col] = helper(row, col-1, &paths) + helper(row-1, col, &paths) | |
| } |
This file contains hidden or 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
| func lca(_ root: TreeNode<Int>, _ x: Int, _ y: Int) -> TreeNode<Int>?{ | |
| guard let pathToX = pathTo(root, x), let pathToY = pathTo(root, y) else { return nil} | |
| var current: TreeNode? = nil | |
| while !pathToX.isEmpty && !pathToY.isEmpty{ | |
| let a = pathToX.pop() | |
| let b = pathToY.pop() | |
| if a == b{ | |
| current = a | |
| }else{ | |
| break |
This file contains hidden or 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
| func longestConsecutive(_ str: String) -> [String: Int]{ | |
| var maxCount = 1 | |
| var maxChar = str[str.startIndex] | |
| var count = 1 | |
| var previousChar = str[str.startIndex] | |
| for char in str.dropFirst(){ | |
| if char == previousChar{ | |
| count += 1 |
This file contains hidden or 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
| //Top down implementation | |
| func mergeSort(_ a: [Int]) -> [Int]{ | |
| guard a.count > 1 else { return a } | |
| let mid = a.count/2 | |
| let leftArray = mergeSort(Array(a[0..<mid])) | |
| let rightArray = mergeSort(Array(a[mid..<a.count])) | |
| return merge(leftArray, rightArray) | |
| } |
This file contains hidden or 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
| func countOccurence(_ key: Int, _ a: [Int]) -> Int{ | |
| func leftBoundry() -> Int{ | |
| var low = 0 | |
| var high = a.count | |
| while low<high{ | |
| let mid = low + (high - low) / 2 | |
| if a[mid] < key{ | |
| low = mid + 1 | |
| }else{ | |
| high = mid |
This file contains hidden or 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
| func search(_ str: String, _ pattern: String) -> String.Index?{ | |
| if pattern.characters.count > str.characters.count{ | |
| return nil | |
| } | |
| var skipTable = [Character: Int]() | |
| let n = pattern.characters.count | |
| for (i, char) in pattern.characters.enumerated(){ | |
| skipTable[char] = n - 1 - i | |
| } |
This file contains hidden or 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
| func search(_ str: String, _ pattern: String) -> Int{ | |
| if pattern.characters.count > str.characters.count{ | |
| return -1 | |
| } | |
| for (i,index) in str.characters.indices.enumerated(){ | |
| var currentIndex = index | |
| var found = true | |
| for char in pattern.characters{ | |
| if currentIndex == str.endIndex || char != str[currentIndex]{ | |
| found = false |
This file contains hidden or 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
| func minmax(_ a: [Int]) -> (Int,Int)?{ | |
| if a.count < 1 { | |
| return nil | |
| } | |
| var minInArray = a[0] | |
| var maxInArray = a[0] | |
| for number in a{ | |
| if number < minInArray{ | |
| minInArray = number | |
| } |
This file contains hidden or 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
| extension String{ | |
| public func longestCommonSubsequence(_ other: String) -> String{ | |
| func lcsLength(_ other: String) -> [[Int]]{ | |
| let n = characters.count | |
| let m = other.characters.count | |
| var matrix = [[Int]](repeating:[Int](repeating: 0, count: m+1), count:n+1) | |
| for (i, char) in characters.enumerated(){ | |
| for (j, otherChar) in other.characters.enumerated(){ | |
| if char == otherChar{ | |
| // Common char found, add 1 to highest lcs found so far. |