Skip to content

Instantly share code, notes, and snippets.

type SomeStruct struct {
AnotherStructToEmbeddFieldsFrom
ThisOneIsVisible string
notThisOneThough string
}
@andriybuday
andriybuday / defer.go
Last active September 13, 2020 21:55
func main() {
a := 4
defer fmt.Println(a)
defer fmt.Println(3)
fmt.Println(1)
fmt.Println(2)
a = 5
// this prints 1, 2, 3, 4
}
@andriybuday
andriybuday / RollingHash.java
Created February 24, 2020 03:37
RollingHash
int mod = 1000000007;
int p = 97; //prime
long hash = 0, power = 1;
for(char c: str) {
long add = ((long)c * power);
hash += add;
hash %= mod;
power = power * p % mod;
}
// in mod space x/p is the same as x*pinv
@andriybuday
andriybuday / SimpleKnapsack.java
Created February 8, 2020 19:43
SimpleKnapsack
public boolean canConstructSum(int[] a, int sum) {
int n = a.length;
boolean[] dp = new boolean[sum+1];
dp[0] = true;
for(int i = 0; i < n; ++i) {
for(int s = sum - a[i]; s >= 0; --s) {
dp[s+a[i]] |= dp[s];
}
}
return dp[sum];
@andriybuday
andriybuday / RunnableWithSyncExample.java
Created February 8, 2020 04:11
RunnableWithSyncExample
public class RunnableWithSyncExample implements Runnable {
private Object someSharedObject;
public SomethingRunnableWithSync(Object sharedObject) {
someSharedObject = sharedObject;
}
public void run() {
synchronized(someSharedObject) {
// do something with someSharedObject
// and then optionally wait or notify/notifyAll
someSharedObject.wait();
@andriybuday
andriybuday / BoundedBlockingQueue.java
Created February 8, 2020 03:39
BoundedBlockingQueue
public class BoundedBlockingQueue {
private Deque<Integer> queue;
private Semaphore enqueue;
private Semaphore dequeue;
public BoundedBlockingQueue(int capacity) {
queue = new ConcurrentLinkedDeque<>();
enqueue = new Semaphore(capacity);
dequeue = new Semaphore(0);
}
@andriybuday
andriybuday / Djikstra.java
Last active February 7, 2020 03:07
Djikstra
boolean[] visited = new boolean[N+1];
// [distance, toNode]
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
pq.add(new int[]{0, startNode});
while(!pq.isEmpty()) {
int[] current = pq.poll();
int node = current[1];
int dist = current[0];
// if reached target node, return dist, or if task is to cover entire graph continue
if(!visited[node]) {
@andriybuday
andriybuday / RangeSumSegmentTree.java
Last active February 6, 2020 23:57
Range Sum Segment Tree
public class RangeSumSegmentTree {
private int n = 0;
private int[] tree;
public RangeSumSegmentTree(int[] a) {
n = a.length;
tree = new int[2*n];
for(int i = 0; i < n; ++i) {
update(i, a[i]);
}
}
@andriybuday
andriybuday / InOrderBinaryTreeTraversal.java
Created February 3, 2020 04:51
InOrder Binary Tree Traversal
// recursive
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList();
if(root == null) return ans;
ans.addAll(inorderTraversal(root.left));
ans.add(root.val);
ans.addAll(inorderTraversal(root.right));
return ans;
}
@andriybuday
andriybuday / DisjointSetUnion.java
Created February 3, 2020 04:28
DisjointSetUnion
class DisjointSetUnion {
int[] parent;
public DisjointSetUnion(int n) {
parent = new int[n];
for(int i = 0; i < n; ++i) {
parent[i] = i;
}
}
public int find(int x) {
if(parent[x] != x) {