Skip to content

Instantly share code, notes, and snippets.

a += b;
b = a - b;
a -= b;
@andriybuday
andriybuday / binarySearch.java
Created February 2, 2020 20:11
binary search
int i = 0, j = n-1;
while(i <= j) {
int m = i + (j-i) / 2;
if(arr[m] == target) {
return m;
} else if(arr[m] < target) {
i = m+1;
} else {
j = m-1;
}
@andriybuday
andriybuday / BreadthFirstSearchStructure.java
Last active February 2, 2020 20:42
Breadth First Search
boolean[] visited = new boolean[n];
Deque<Integer> queue = new LinkedList();
queue.offer(0);
while(!queue.isEmpty()) {
int x = queue.poll();
// do something to generate the output of your algorithm
// or just return x if that's what you are searching for
visited[x] = true;
int[] nextElements = getElementsAssessibleFromX(x);
for(int y: nextElements) {
boolean[] visited = new boolean[n];
Deque<Integer> stack = new LinkedList();
stack.push(0);
while(!stack.isEmpty()) {
int x = stack.pop();
// do something to generate the output of your algorithm
// or just return x if that's what you are searching for
visited[x] = true;
int[] nextElements = getElementsAssessibleFromX(x);
for(int y: nextElements) {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
backtrack(ans, new ArrayList<Integer>(), nums);
return ans;
}
private void backtrack(List<List<Integer>> ans, List<Integer> ongoing, int[] nums) {
if(ongoing.size() == nums.length) {
ans.add(new ArrayList<>(ongoing));
} else {
for(int i = 0; i < nums.length; i++) {
@andriybuday
andriybuday / DynamicProgramming.java
Created February 3, 2020 04:23
Dynamic Programming
int[] dp = new int[n];
dp[0] = // something that makes sense for initial value
for(int i = 1; i < n; ++i) {
dp[i] = // some way to get value using dp[i-1]
}
return dp[n-1];
@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) {
@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 / 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 / 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]) {