Skip to content

Instantly share code, notes, and snippets.

@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 / 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];
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++) {
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) {
@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) {
@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;
}
a += b;
b = a - b;
a -= b;
#!/bin/bash
db_set () {
echo "$1,$2" >> database
}
db_get () {
grep "^$1," database | sed -e "s/^$1,//" || tail -n 1
}
#!/bin/bash
CurrentPath=$(pwd)
# a directory where two repositories will get fetched to
SynchLocation="/C/git-synch/"
# bundle file location - typicly on a media that transfers the file (like USB-stick)
BundleLocation="/E/bundlefile.bundle"
# remote location and alias name for repository A
RemoteA="https://github.com/LocationOfRepoA.git"
RepoA="RepoA"
# remote location and alias name for repository B
Car doctorsDream = new AmbulanceCar(new MercedesCar());
doctorsDream.Go();