Skip to content

Instantly share code, notes, and snippets.

@balloonio
balloonio / installing_cassandra.md
Created June 4, 2020 19:46 — forked from hkhamm/installing_cassandra.md
Installing Cassandra on Mac OS X

Installing Cassandra on Mac OS X

Install Homebrew

Homebrew is a great little package manager for OS X. If you haven't already, installing it is pretty easy:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
@balloonio
balloonio / leetcode_notes.md
Last active May 11, 2020 01:28
Some notes for leetcode

Palindrome

  • dynamic programing with f[i][j] = whether s[i:j+1] aka s[i]..s[j] is palindrome or its length
  • enumerate pivots, and expand from each pivot

Subsequence

  • if using dynamic programing, in general f[i][j] = f[i+1][j-1] || f[i][j-1] || f[i+1]f[j] (meaning you can either take both end, left only, or right only

DP

@balloonio
balloonio / weighted_random_cache_test.py
Created November 12, 2018 20:38
Weighted random cache testing code
wr = WeightRandom()
wr.setObjectWeight(1, 1)
wr.setObjectWeight(2, 1)
wr.setObjectWeight(2, 0)
wr.setObjectWeight(3, 2)
for i in range(10):
print(wr.getRandom())
@balloonio
balloonio / weighted_random_cache.py
Created November 12, 2018 20:37
A weighted random implementation with O(1) setObjectWeight(key, weight) and O(n) getRandom()
class WeightRandom:
def __init__(self):
self.key2node = {} # object key or id : the node contains the object info (weight, id)
self.nodelist = NodeList()
self.totalsum = 0
# O(1)
def setObjectWeight(self, obj_key, weight):
# not in map and also try to zero weight, no op
if weight == 0 and obj_key not in self.key2node:
@balloonio
balloonio / nodelist.py
Last active November 12, 2018 20:41
A generic double linked list that is similar to the one from LRU cache, except the insertion takes place at the tail instead of at the head
class Node:
def __init__(self, key, weight):
self.key = key # object key or id
self.weight = weight
self.prev = None
self.nxt = None
class NodeList:
def __init__(self):
self.head = None
@balloonio
balloonio / lru_cache.py
Last active November 12, 2018 20:41
A LRU implementation using the above double linked list and a hashmap
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.key2node = {}
self.nodelist = NodeList()
def get(self, key):
if key not in self.key2node:
return -1
node = self.key2node[key]
@balloonio
balloonio / nodelist.py
Last active April 11, 2019 18:35
A generic double linked list
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.nxt = None
class NodeList:
def __init__(self):
self.size = 0