Skip to content

Instantly share code, notes, and snippets.

@denpazakura
denpazakura / HuffmanDecoding.swift
Created January 2, 2024 10:57
Huffman Decoding
class Node {
var freq: Int
var data: Character
var left: Node?
var right: Node?
init(freq: Int, data: Character) {
self.freq = freq
self.data = data
}
@denpazakura
denpazakura / PreorderTraversal.swift
Created January 1, 2024 14:51
Preorder Traversal
class Node {
var info: Int
var left: Node?
var right: Node?
var level: Int?
init(info: Int) {
self.info = info
}
}
@denpazakura
denpazakura / Cookies.swift
Created January 1, 2024 14:26
Cookies problem
func cookies(k: Int, A: [Int]) -> Int {
var sorted = A.sorted()
var iterations = 0
while sorted.count >= 2 && sorted[0] < k {
let leastSweet = sorted.removeFirst()
let secondLeastSweet = sorted.removeFirst()
let combined = leastSweet + 2 * secondLeastSweet
@denpazakura
denpazakura / TextEditor.swift
Last active December 29, 2023 14:10
Simple text editor implementation (not the optimal one)
import Foundation
class TextEditor {
static func process() {
guard let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"] else {
fatalError("OUTPUT_PATH not set")
}
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
guard let fileHandle = FileHandle(forWritingAtPath: stdout) else {
@denpazakura
denpazakura / BalancedBrackets.swift
Created December 29, 2023 11:17
Find if the brackets in a string are balanced
func isBalanced(s: String) -> String {
var stack = [Character]()
let openingBrackets: Set<Character> = ["(", "{", "["]
let closingBrackets: [Character: Character] = [")": "(", "}": "{", "]": "["]
for bracket in s {
if openingBrackets.contains(bracket) {
stack.append(bracket)
} else {
if let lastBracket = stack.popLast(), lastBracket == closingBrackets[bracket] {
@denpazakura
denpazakura / Queue.swift
Created December 28, 2023 17:31
Queue using two stacks + input reading
import Foundation
var stack1 = [Int]()
var stack2 = [Int]()
func enqueue(element: Int) {
stack1.append(element)
}
func dequeue() {
@denpazakura
denpazakura / MergedLists.swift
Created December 27, 2023 13:05
Merging two linked lists
func mergeLists(arrayA: [Int], arrayB: [Int]) -> [Int] {
var mergedArray = [Int]()
var indexA = 0
var indexB = 0
while indexA < arrayA.count && indexB < arrayB.count {
if arrayA[indexA] < arrayB[indexB] {
mergedArray.append(arrayA[indexA])
indexA += 1
} else {
@denpazakura
denpazakura / NewYearChaos.swift
Created December 26, 2023 14:11
Hackerrank New Year Chaos problem solution
func minimumBribes(q: [Int]) -> Void {
var bribes = 0
for (index, person) in q.enumerated() {
let originalPosition = person - 1
if originalPosition - index > 2 {
print("Too chaotic")
return
}
@denpazakura
denpazakura / RecursiveDigitSum.swift
Created December 26, 2023 13:33
Recursive digit sum solution
func superDigit(n: String, k: Int) -> Int {
let repeatedString = String(repeating: n, count: k)
let intArray = repeatedString.compactMap { $0.wholeNumberValue }
var superDigit = intArray.reduce(0, +)
if intArray.count == 1 {
return superDigit
}
@denpazakura
denpazakura / GridChallenge.swift
Created December 26, 2023 12:16
Hackerrank grid challenge solution
let gridArraySorted = grid.map { Array($0).sorted() }
// Check each column
guard let firstRow = gridArraySorted.first else {
return "NO"
}
let columnCount = firstRow.count
let areColumnsAscending = (0..<columnCount).allSatisfy { col in
let column = gridArraySorted.map { $0[col] }