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 Node { | |
var freq: Int | |
var data: Character | |
var left: Node? | |
var right: Node? | |
init(freq: Int, data: Character) { | |
self.freq = freq | |
self.data = data | |
} |
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 Node { | |
var info: Int | |
var left: Node? | |
var right: Node? | |
var level: Int? | |
init(info: Int) { | |
self.info = info | |
} | |
} |
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 | |
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 { |
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
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] { |
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 | |
var stack1 = [Int]() | |
var stack2 = [Int]() | |
func enqueue(element: Int) { | |
stack1.append(element) | |
} | |
func dequeue() { |
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
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 { |
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
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 | |
} |
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
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 | |
} | |
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
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] } |
NewerOlder