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 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) { |
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
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]; |
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
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++) { |
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
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) { |
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
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) { |
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
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; | |
} |
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
a += b; | |
b = a - b; | |
a -= b; |
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
#!/bin/bash | |
db_set () { | |
echo "$1,$2" >> database | |
} | |
db_get () { | |
grep "^$1," database | sed -e "s/^$1,//" || tail -n 1 | |
} |
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
#!/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 |
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
Car doctorsDream = new AmbulanceCar(new MercedesCar()); | |
doctorsDream.Go(); |