Skip to content

Instantly share code, notes, and snippets.

View voxqhuy's full-sized avatar
:shipit:
Don’t comment bad code—rewrite it

Huy Vo voxqhuy

:shipit:
Don’t comment bad code—rewrite it
  • San Diego, CA
View GitHub Profile
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let datePicker = UIDatePicker()
// 1 3 800
// 1 2 500
// 2 3 500
// 1 70
// 2 40
// 1 3
public static class Station {
int city;
int price;
int capacity;
// https://leetcode.com/problems/add-and-search-word-data-structure-design/
// For add():
// Time: O(n), Space: O(n)
// For search():
// Time: O(n), Space: O(n) because of the recursive calls
// Where there are wildcards:
// The absolute worst case we can have 26 children at each node, traversing through all nodes with DFS will take 26^n (n nodes, each nodes have 26 children/characters). 26^n is technically 2^n. Therefore,
// Time: O(2^n), Space: O(n) because we only have at max n calls on the stack at any given time.
// or O(m) where m is the total number of characters on the trie.
// https://leetcode.com/problems/maximum-depth-of-binary-tree/
// Recursive
// Time: O(n), Space: O(n)
func maxDepth(_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }
return 1 + max(maxDepth(root.left), maxDepth(root.right))
}
// Iterative
// Time: O(n), Space: O(n)
// https://leetcode.com/problems/subsets/
// Approach 1: Cascading
// Time: O(n*2^n), Space: O(n*2^n)
func subsets(_ nums: [Int]) -> [[Int]] {
var result = [[Int]]()
result.append([])
for num in nums {
for currSet in result {
result.append(currSet + [num])
}
// https://leetcode.com/problems/binary-tree-maximum-path-sum/
// O(n), Space O(n)
var globalMax = Int.min
func maxPathSum(_ root: TreeNode?) -> Int {
maxPath(root)
return globalMax
}
func maxPath(_ root: TreeNode?) -> Int {
guard let root = root else { return 0 }
// ------------------MEDIUM------------------ //
// https://leetcode.com/problems/clone-graph/
// DFS
// Time: O(n), Space: O(n)
func cloneGraph(_ node: Node?) -> Node? {
var dict = [Int : Node]()
return cloneNode(node, &dict)
}
// https://leetcode.com/problems/top-k-frequent-elements/
// O(n), O(n)
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
var freqDict = [Int : Int]()
var bucket = [Int : [Int]]()
var result = [Int]()
var topFreq = 0
// O(n)
for num in nums {
// https://leetcode.com/problems/number-of-islands/
// Time: n*m, Space 1
func numIslands(_ grid: [[Character]]) -> Int {
var gridCopy = grid
var islandCount = 0
for i in 0..<gridCopy.count {
for j in 0..<gridCopy[0].count {
if gridCopy[i][j] != "0" {
markAdjacentLands(i, j, inGrid: &gridCopy)
// Toaster
class Bread {
var cookedTime: Double = 0.0
}
enum TrayLevel {
case down
case up
}