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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"math/rand" | |
"os" | |
"strconv" | |
"strings" | |
"sync" |
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
from typing import * | |
from abc import ABCMeta, abstractmethod | |
from collections import deque | |
class PuzzleSolver(metaclass=ABCMeta): | |
"""Abstract class for solve puzzle that have initial state and end state | |
Simple breath first search | |
from any state -> generate next reachable state and eliminated duplicate state | |
until reach goal or max depth reached |
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
""" | |
Inital we have N empty cup with capacity(c1, c2, c3... cn) | |
Posible move: | |
empty one cup | |
fill one cup | |
pour from one cup to another | |
We have unlimited water source, find a way to fill a target amount | |
""" | |
from collections import deque |
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
""" | |
Merged to sorted array O(M + N) then find median O(1) | |
Space: O(M + N) for temporary array | |
""" | |
def merge(first: List[int], second: List[int]) -> List[int]: | |
M = len(first) | |
N = len(second) | |
i = j = 0 | |
result = [] | |
while i < M and j < N: |
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
from collections import deque, defaultdict | |
def tarjan_recursive(graph): | |
unvisited = set(graph.keys()) | |
low_link = {} | |
visited_ids = {} # Node id, also keep track of visited node | |
stack_set = set() | |
stack = [] | |
groups = [] |
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
/* Using array to store and search for key. Time complexity O(n) */ | |
class DOMStoreArray { | |
constructor () { | |
this.data = []; | |
} | |
find (node) { | |
for (let item of this.data) { | |
if (item[0] === node) { | |
return item; |
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
from collections import deque | |
''' | |
Graph problems | |
A | |
C | |
E | |
F | |
D | |
G |
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 EventEmitter { | |
constructor (isAsync = false) { | |
this.events = new Map(); | |
this.isAsync = isAsync; // Allow to notify listener async | |
} | |
/* | |
* Subcribe a callback to an event. Callback will not be duplicate. | |
* return unsibcribe function | |
* Usage: const unsub = ee.subcribe(eventName, callback); |
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 Node: | |
def __init__(self, value): | |
self.value = value | |
self.next = None | |
def length(head): | |
"""Get linked list length""" | |
count = 0 | |
node = head | |
while node: |