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
import java.util.concurrent.CountedCompleter; | |
public class MergeSortTask extends CountedCompleter<Void> { | |
private static final long serialVersionUID = -5183127767439978300L; | |
private Comparable[] data; | |
private int start, end; | |
private int middle; | |
public MergeSortTask(Comparable[] data, int start, int end, MergeSortTask parent) { |
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
import java.io._ | |
import scala.io._ | |
import scala.collection.mutable._ | |
import scala.math._ | |
class MaxHeap(val maxSize: Int) { | |
private val heap = Array.fill[Int](maxSize)(0) | |
var size:Int = 0 | |
private def parent(i: Int): Int = i / 2 |
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
import types | |
from imp import reload | |
def status(module): | |
print('reloading ' + module.__name__) | |
def try_reload(module): #FAIL (sometimes in 3.3) | |
try: | |
reload(module) | |
except: |
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
''' | |
Given an array, all elements are unique, you can only perform rotation on 3 consecutive elements: | |
A B C | |
Example | |
A B C -> B C A -> C A B -> A B C | |
Given an array, find out if it is possible to sort it by only performing rotation. | |
Solution: | |
https://www.hackerrank.com/challenges/larrys-array/editorial | |
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
from sys import stdin, stdout | |
INF = float('Inf') | |
def build_floyd_table(n, m): | |
# initalize with all values to INF | |
distance_table = [[INF]*n for i in range(n)] | |
# set the diagonal values to 0 | |
for i in range(0, n): | |
distance_table[i][i] = 0 | |
# all has been set, input the edges |
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
def partition(arr, low, high): | |
# choose pivot as | |
pivot = arr[high] | |
i = -1 | |
j = 0 | |
# move all elements that are <= pivot to the left of it | |
# and bigger elements to the right | |
while j < high: | |
if arr[j] <= pivot: | |
i += 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
''' | |
Segmentree: | |
- construction: recursive like MergeSort, build leaves first then internal nodes | |
- Explanation: http://www.geeksforgeeks.org/segment-tree-set-1-range-minimum-query/ | |
- Basically: | |
We need to construct a segmentree from an input array, then when query, there are three case than can be happened: | |
+ Case1: Range of current node is inside query range (full overlap) | |
+ Case2: Range of current node is completely outside query range (no overlap) | |
+ Case3: Range of current node is part of query range (partion overlap) | |
''' |
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
def is_power_of_2(n): | |
return n > 0 and (n & (n-1)) == 0 | |
def next_power_of_2(n): | |
if is_power_of_2(n): | |
return n | |
count = 0 | |
while n > 0: | |
count += 1 | |
n = 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
# 1. using set (slow) | |
class Disjoint: | |
''' | |
Disjoinset class, support three main methods: | |
+ make_set(X): return a set for a vertexes v | |
+ find_set(X): find out which set X are in | |
+ union(a, b): union two set a,b and remove a,b | |
''' | |
def __init__(self): | |
# nested sets inside set |
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
''' | |
Tutorial: | |
https://www.youtube.com/watch?v=oP2-8ysT3QQ | |
''' | |
import bisect | |
class Graph: | |
def __init__(self, start, vertexes, edges): | |
self._vertexes = vertexes | |
self._start = start |
NewerOlder