This file contains 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 climbStairs(_ n: Int) -> Int { | |
if n <= 3 { | |
return n | |
} | |
var steps = [3, 1, 2] | |
for step in 4...n { | |
steps[step % 3] = steps[(step - 1) % 3] + steps[(step - 2) % 3] | |
} | |
return steps[n % 3] |
This file contains 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 findCheapestPrice(_ n: Int, _ flights: [[Int]], _ src: Int, _ dst: Int, _ K: Int) -> Int { | |
var adj = [[(to: Int, price: Int)]](repeating: [], count: n) | |
for flight in flights { | |
adj[flight[0]].append((flight[1], flight[2])) | |
} | |
var current = [(src, 0)] | |
var minPrices = [Int](repeating: -1, count: n) | |
var stops = 0 |
This file contains 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
import Foundation | |
func solveMinimumCostFlow() { | |
// 1 ≤ 𝑁 ≤ 100,000, 𝑁 - 1 ≤ 𝑀 ≤ 200,000, 0 ≤ D ≤ 109 | |
let meta = readLine()!.split(separator: " ").compactMap { Int($0) } | |
let n = meta[0] | |
let m = meta[1] | |
let d = meta[2] | |
struct Pipe { |
This file contains 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
/* | |
* > Runtime: 16 ms, faster than 100.00% | |
* > Memory Usage: 21.2 MB, less than 100.00% | |
*/ | |
class Solution { | |
func twoCitySchedCost(_ costs: [[Int]]) -> Int { | |
var left = [Int]() | |
left.reserveCapacity(costs.count >> 1) | |
var right = [Int]() | |
right.reserveCapacity(costs.count >> 1) |
This file contains 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 canFinish(_ n: Int, _ edges: [[Int]]) -> Bool { | |
var inDegrees = [Int](repeating: 0, count: n) | |
var graph = [[Int]](repeating: [Int](), count: n) | |
for edge in edges { | |
inDegrees[edge[0]] += 1 | |
graph[edge[1]].append(edge[0]) | |
} | |
var initialVs = [Int]() |
This file contains 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
import Foundation | |
/* | |
* DFS | |
* - Time: O(N) | |
* - Space: O(N) | |
*/ | |
func solveCyclicPermutationByDFS() { | |
let t = Int(readLine()!)! | |
for _ in 0..<t { |
This file contains 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
import Foundation | |
func readInput() -> ([[Int]], Set<Int>) { | |
let meta = readLine()!.split(separator: " ").map { Int($0)! } | |
var input = ([[Int]](repeating: [Int](), count: meta[0]), Set<Int>()) | |
let realNodes = readLine()!.split(separator: " ").map { Int($0)! } | |
input.1 = Set<Int>(realNodes) | |
for _ in 0..<meta[0] - 1 { |
This file contains 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
// | |
// UF.swift | |
// SwiftAlgorithmsDataStructures | |
// | |
// Created by Derrick Park on 2/24/20. | |
// Copyright © 2020 Derrick Park. All rights reserved. | |
// | |
import Foundation |
This file contains 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
import Foundation | |
func readTree() -> [[Int]] { | |
let n = Int(readLine()!)! | |
var tree = [[Int]](repeating: [Int](), count: n + 1) | |
for _ in 1..<n { | |
let nodes = readLine()!.split(separator: " ").map { Int($0)! } | |
tree[nodes[0]].append(nodes[1]) | |
tree[nodes[1]].append(nodes[0]) | |
} |
This file contains 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
import Foundation | |
func findSubstring(_ text: String, _ pattern: String) -> String.Index? { | |
return findSubstringBM(text, pattern) | |
} | |
func findSubstringBM(_ text: String, _ pattern: String) -> String.Index? { | |
let pLength = pattern.count | |
let tLength = text.count | |
var table = [Character:Int]() |
NewerOlder