Skip to content

Instantly share code, notes, and snippets.

@fengyitsai
fengyitsai / leetcode1091.swift
Created February 13, 2020 17:47
1091. Shortest Path in Binary Matrix
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 {
@fengyitsai
fengyitsai / leetcode1342.swift
Created February 13, 2020 16:53
1342. Number of Steps to Reduce a Number to Zero
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 {
@fengyitsai
fengyitsai / leetcode791.swift
Created February 11, 2020 12:53
791. Custom Sort String
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
}
@fengyitsai
fengyitsai / leetcode1320.swift
Created February 10, 2020 17:32
1320. Minimum Distance to Type a Word Using Two Fingers
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
@fengyitsai
fengyitsai / leetcode1249.swift
Created February 5, 2020 17:17
1249. Minimum Remove to Make Valid Parentheses
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
@fengyitsai
fengyitsai / leetcode928.swift
Created January 21, 2020 13:09
928. Minimize Malware Spread II
class Solution {
func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
var record = [Int:[Int]]()
let initialSet = Set(initial)
for start in initial {
var visited = initialSet
@fengyitsai
fengyitsai / leetcode841.swift
Created January 15, 2020 13:07
841. Keys and Rooms
class Solution {
func canVisitAllRooms(_ rooms: [[Int]]) -> Bool {
var visited = Set<Int>()
var queue = [0]
while !queue.isEmpty {
var nextQueue = [Int]()
@fengyitsai
fengyitsai / leetcode802.swift
Created January 14, 2020 13:41
802. Find Eventual Safe States
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
}
@fengyitsai
fengyitsai / leetcode785.swift
Created January 13, 2020 12:08
785. Is Graph Bipartite?
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
@fengyitsai
fengyitsai / leetcode765.swift
Created January 10, 2020 16:24
765. Couples Holding Hands
class Solution {
func minSwapsCouples(_ row: [Int]) -> Int {
var indices = row
var row = row
for i in 0..<row.count {
indices[row[i]] = i
}