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
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
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
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
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
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
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
// 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; | |
} |
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 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]); | |
} | |
} |
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+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]) { |