Skip to content

Instantly share code, notes, and snippets.

View charlieInDen's full-sized avatar
😈
In a den

Nishant Sharma charlieInDen

😈
In a den
View GitHub Profile
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() {
class MaxStack {
// two stack
// one regular stack
// one max stack, keep track latest max
var stack: [Int] = []
var maxStack: [Int] = []
init() {
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)))
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
@charlieInDen
charlieInDen / Cousins.swift
Created January 19, 2021 08:23
Cousins in Binary Tree
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)) }
/// 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))
}
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
///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
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))
def add_binary(a,b):
#your code here
sum = a + b
return bin(sum)[2:]