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 distanceK(_ root: TreeNode?, _ target: TreeNode?, _ K: Int) -> [Int] { | |
guard let root = root else { return [] } | |
var parentDict = [TreeNode: TreeNode]() | |
dfsFillParentDict(root, parentDict) | |
var queue = [root, nil] | |
var level = 0 | |
var visited = Set<TreeNode>() | |
while level != K && !queue.isEmpty { | |
if let node = queue.removeFirst() { |
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 MaxStack { | |
// two stack | |
// one regular stack | |
// one max stack, keep track latest max | |
var stack: [Int] = [] | |
var maxStack: [Int] = [] | |
init() { | |
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 MinStack { | |
private var arr: [(Int, Int)] | |
/** initialize your data structure here. */ | |
init() { | |
arr = [(Int,Int)]() | |
} | |
func push(_ x: Int) { | |
let prevMax = peek().1 | |
arr.append((x, min(prevMax,x))) |
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 sortedSquares(_ A: [Int]) -> [Int] { | |
var res = [Int](repeating: 0, count: A.count) | |
var index = A.count - 1 | |
var left = 0 | |
var right = A.count - 1 | |
while left <= right { | |
var a = A[left] * A[left] | |
var b = A[right] * A[right] | |
if a > b { | |
res[index] = a |
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 isCousins(_ root: TreeNode?, _ x: Int, _ y: Int) -> Bool { | |
var queue: [(node: TreeNode, depth: Int, parentVal: Int)] = [(root!, 0, -1)] | |
var xd: (depth: Int?, parentVal: Int) = (nil, -1), yd: (depth: Int?, parentVal: Int) = (nil, -1) | |
while queue.count > 0 { | |
let (node, depth, parentVal) = queue.removeLast() | |
if node.val == x { xd = (depth, parentVal) } | |
if node.val == y { yd = (depth, parentVal) } | |
if xd.depth != nil && yd.depth != nil { return xd.depth == yd.depth && xd.parentVal != yd.parentVal } | |
if node.left != nil { queue.append((node.left!, depth + 1, node.val)) } | |
if node.right != nil { queue.append((node.right!, depth + 1, node.val)) } |
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
/// Input - [1, 2, 3, 6, 9, 8, 7, 4, 5], m = 3, n = 3 | |
func spiralTo2DMatrix(_ arr: [Int], row: Int, col: Int) -> [[Int]] { | |
/// Base condition | |
var result:[[Int]] = [[Int]]() | |
guard arr.isEmpty == false else { return result } | |
/// Logic | |
for _ in 0..<row { | |
result.append(Array(repeating: 0, count: col)) | |
} | |
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 spiral(_ matrix: [[Int]]) -> [Int] { | |
/// Base condition | |
var result = [Int]() | |
guard matrix.isEmpty == false else { return result } | |
/// Logic | |
var r1 = 0, c1 = 0 | |
var r2 = matrix.count - 1, c2 = matrix[0].count - 1 | |
while r1 <= r2 && c1 <= c2 { | |
/// Go from c1 to c2 and append the element |
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
///input - ["a": ["b": ["c": "1", "d": "1"]], | |
"e" : "1" , | |
"f" : ["g": ["h": "1"]]] | |
///output - ["h": "1", "d": "1", "g": 1, "a": 3, "e": "1", "c": "1", "f": 2, "b": 2] | |
func flatten(dictionary: [String: Any]) -> [String: Any] { | |
func flattenRec(output: inout [String: Any], keyPath: String, value: Any, count: inout Int) { | |
let nCount = count | |
if value is String { | |
output[keyPath] = value |
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
names = input("Enter names separated by commas: ").title().split(",") | |
assignments = input("Enter assignment counts separated by commas: ").split(",") | |
grades = input("Enter grades separated by commas: ").split(",") | |
message = "Hi {},\n\nThis is a reminder that you have {} assignments left to \ | |
submit before you can graduate. You're current grade is {} and can increase \ | |
to {} if you submit all assignments before the due date.\n\n" | |
for name, assignment, grade in zip(names, assignments, grades): | |
print(message.format(name, assignment, grade, int(grade) + int(assignment)*2)) |
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
def add_binary(a,b): | |
#your code here | |
sum = a + b | |
return bin(sum)[2:] |
NewerOlder