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
import Foundation | |
//-------------Basic BST Operation------------------- | |
class Node | |
{ | |
var key: Int? | |
var left: Node? | |
var right: Node? | |
var description: String { |
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
import Foundation | |
func knapsack(_ weights: Array<Int>,_ values: Array<Int>,_ index: Int,_ remainingWeight: Int) -> Int { | |
if (index < 0) { return 0 } | |
var choosedValue: Int = 0 | |
if (remainingWeight >= weights[index]) { | |
// Choose | |
choosedValue = values[index] + knapsack(weights, values, index - 1, remainingWeight - weights[index]) |
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
import Foundation | |
func knapsack(_ weights: Array<Int>,_ values: Array<Int>,_ index: Int,_ remainingWeight: Int,_ cache: inout Array<Array<Int>>) -> Int { | |
if (index < 0) { return 0 } | |
if (cache[index][remainingWeight] != -1) { | |
return cache[index][remainingWeight] | |
} | |
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
import UIKit | |
var arr = [Int?]() | |
arr.append(nil) | |
func parent(_ i: Int) -> Int { | |
return i/2 | |
} |
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
import typing | |
from typing import List | |
import sys | |
class Solution: | |
def __init__(self) -> None: | |
self.amount_memorization = {} | |
def coinChange(self, coins: List[int], amount: int) -> int: |
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 MaxHeap { | |
private var array = Array<Int>() | |
func leftChild(_ i:Int) -> Int { | |
return 2*i+1 | |
} | |
func rightChild(_ i:Int) -> Int { | |
return 2*i+2 |
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 Solution { | |
func addBinary(_ a: String, _ b: String) -> String { | |
var result = "" | |
let aStrArr = Array(String(a.reversed())).map{ String($0) } | |
let bStrArr = Array(String(b.reversed())).map{ String($0) } | |
var index = 0 | |
var advanced = 0 | |
while index < aStrArr.count && index < bStrArr.count { |
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
private class ListNode { | |
var next: ListNode? | |
var prev: ListNode? | |
var key: Int = 0 | |
} | |
/* | |
push to back, peek/pop from front, size and is empty | |
*/ | |
class Queue { | |
private var head: ListNode? |
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
private class ListNode { | |
var next: ListNode? | |
var prev: ListNode? | |
var key: Int = 0 | |
} | |
class Stack { | |
private var head: ListNode? | |
private var tail: ListNode? | |
private var count: Int = 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
class ListNode { | |
var next: ListNode? | |
var key: Int = 0 | |
} | |
/* | |
push to back, peek/pop from front, size and is empty | |
*/ | |
class LinkedList { | |
private var head: ListNode? | |
private var tail: ListNode? |