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
class Solution { | |
func shortestPathBinaryMatrix(_ grid: [[Int]]) -> Int { | |
guard !grid.isEmpty, !grid[0].isEmpty else { | |
return -1 | |
} | |
var visited = grid | |
var step = 1 | |
var queue = [(0,0)] | |
if visited[0][0] == 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
class Solution { | |
func numberOfSteps (_ num: Int) -> Int { | |
var num = num | |
var counter = 0 | |
while num > 0 { | |
if num % 2 == 0 { | |
num = num/2 | |
counter += 1 | |
} else { |
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
class Solution { | |
func customSortString(_ S: String, _ T: String) -> String { | |
let sArray = Array(S) | |
var counter = [Character:Int]() | |
for char in T { | |
counter[char, default:0] += 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
class Solution { | |
func minimumDistance(_ word: String) -> Int { | |
let array = Array(word).compactMap{ $0.asciiValue }.map{ Int($0 - 65) } | |
var dp = [[Int]](repeating:[Int](repeating:Int.max/2, count:27), count:word.count + 1) | |
dp[0][26] = 0 | |
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
class Solution { | |
func minRemoveToMakeValid(_ s: String) -> String { | |
var array = s.map { String($0) } | |
var leftBraces = [Int]() | |
for i in 0..<array.count { | |
if array[i] == "(" { | |
leftBraces.append(i) | |
continue |
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
class Solution { | |
func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int { | |
var record = [Int:[Int]]() | |
let initialSet = Set(initial) | |
for start in initial { | |
var visited = initialSet | |
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
class Solution { | |
func canVisitAllRooms(_ rooms: [[Int]]) -> Bool { | |
var visited = Set<Int>() | |
var queue = [0] | |
while !queue.isEmpty { | |
var nextQueue = [Int]() | |
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
class Solution { | |
func eventualSafeNodes(_ graph: [[Int]]) -> [Int] { | |
var record = [Int:Bool]() | |
func visit(_ i:Int, _ visited:inout Set<Int>) -> Bool { | |
if let isSafe = record[i] { | |
return isSafe | |
} | |
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
class Solution { | |
func isBipartite(_ graph: [[Int]]) -> Bool { | |
var group = [Int:Bool]() | |
for (index, node) in graph.enumerated() { | |
guard group[index] == nil else { | |
continue | |
} | |
var queue = [index] | |
var currentGroup = true |
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
class Solution { | |
func minSwapsCouples(_ row: [Int]) -> Int { | |
var indices = row | |
var row = row | |
for i in 0..<row.count { | |
indices[row[i]] = i | |
} |
NewerOlder