Skip to content

Instantly share code, notes, and snippets.

View usk83's full-sized avatar

Yusuke Takahashi usk83

View GitHub Profile
@usk83
usk83 / 01_ClimbingStairs.swift
Created March 19, 2020 23:54
Problem Set (DP)
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]
@usk83
usk83 / CheapestFlightsWithinKStops.swift
Last active March 13, 2020 23:56
Problem Set 8 (Shortest Path Algorithms)
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
@usk83
usk83 / MinimumCostFlow.swift
Created March 11, 2020 19:45
Minimum Cost Flow - Greedy
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 {
@usk83
usk83 / 1029-TwoCityScheduling.swift
Created March 10, 2020 19:37
Problem Set 7 (Greedy Algorithms)
/*
* > 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)
@usk83
usk83 / 207_CourseSchedule.swift
Last active March 6, 2020 01:05
Problem Set 6 (DAG + Topological Sort)
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]()
@usk83
usk83 / CyclicPermutation.swift
Created March 2, 2020 20:22
Problem Set 2 (Graph)
import Foundation
/*
* DFS
* - Time: O(N)
* - Space: O(N)
*/
func solveCyclicPermutationByDFS() {
let t = Int(readLine()!)!
for _ in 0..<t {
@usk83
usk83 / SushiRestaurantReviews.swift
Last active February 29, 2020 00:55
Sushi Restaurant Reviews
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 {
@usk83
usk83 / UF.swift
Last active February 27, 2020 23:56
Union-Find (Disjoint sets)
//
// UF.swift
// SwiftAlgorithmsDataStructures
//
// Created by Derrick Park on 2/24/20.
// Copyright © 2020 Derrick Park. All rights reserved.
//
import Foundation
@usk83
usk83 / LowestCommonAncestor.swift
Last active February 27, 2020 00:36
LCA (Lowest Common Ancestor)
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])
}
@usk83
usk83 / Topic2.swift
Created February 24, 2020 22:50
Find the substring pattern of length M in a text of length N.
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]()