This file contains hidden or 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
| struct Monoqueue<T: Comparable> { | |
| var array = [(value: T, count: Int)]() | |
| var max: T { | |
| return array.first!.value | |
| } | |
| mutating func push(_ value: T) { | |
| var count = 0 | |
| while !array.isEmpty, array.last!.value < value { |
This file contains hidden or 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
| extension Int { | |
| fileprivate func nextPowerOf2() -> Int { | |
| guard self != 0 else { | |
| return 1 | |
| } | |
| return 1 << (bitWidth - (self - 1).leadingZeroBitCount) | |
| } | |
| } | |
| struct SegmentTree<T: Comparable> { |
This file contains hidden or 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
| struct PriorityQueue<T> { | |
| private var array: [T] = [] | |
| private let priority: (T, T) -> Bool | |
| var count: Int { return array.count } | |
| var isEmpty: Bool { return array.isEmpty } | |
| var top: T? { return array.first } | |
| init(_ values: [T], priority: @escaping (T, T) -> Bool) { |
This file contains hidden or 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 TrieNode { | |
| var hash: [Character:TrieNode] = [:] | |
| var endOfWord: Bool = false | |
| } | |
| class Trie { | |
| var root = TrieNode() | |
| init() {} | |
This file contains hidden or 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
| extension Array where Element: Comparable { | |
| // This implementations are inspired in https://github.com/python/cpython/blob/master/Lib/bisect.py | |
| /// Return the index where to insert value in the receiver, assuming the receiver is sorted | |
| /// | |
| /// - Parameters: | |
| /// - value: The position of the text in current context | |
| /// - min: The lower bound where to perform the search | |
| /// - max: The upper bound where to perform the search | |
| /// - Returns: An index such that all elements in self[:i] have element < value, and all elements in |
This file contains hidden or 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
| struct HeightedUnion { | |
| typealias QuickUnionRootHeight = (root: Int, height: Int) | |
| private var ids: [Int] | |
| // Complexity: O(n) | |
| init(_ N: Int) { | |
| ids = Array(0..<N) | |
| } |
This file contains hidden or 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 greatestCommonDivisor(first: Int, second: Int) -> Int { | |
| return internalPrivateGreatestCommonDivisor(first, second, min(first, second)) | |
| } | |
| private func internalPrivateGreatestCommonDivisor(first: Int, second: Int, divider: Int) -> Int { | |
| if divider == 0 { | |
| return 0 | |
| } | |
This file contains hidden or 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
| NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; |
This file contains hidden or 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
| @weakify(self) | |
| [[self.firstTextField rac_signalForControlEvents:UIControlEventEditingDidEndOnExit] | |
| subscribeNext:^(id x) { | |
| @strongify(self) | |
| [self.secondTextField becomeFirstResponder]; | |
| }]; |
This file contains hidden or 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
| - (void)setImage:(UIImage *)image withCornerRadius:(CGFloat)cornerRadius | |
| { | |
| UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0); | |
| [[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius] addClip]; | |
| [image drawInRect:self.bounds]; | |
| self.image = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| } |
NewerOlder