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 iterativeDeepeningAlphaBeta(state, evaluationFunc): | |
startTime = time() | |
def alphaBetaSearch(state, alpha, beta, depth): | |
def maxValue(state, alpha, beta, depth): | |
val = -MaxUtility | |
for successor in state.getSuccessors(): | |
val = max(val, alphaBetaSearch(successor, alpha, beta, depth)) | |
if val >= beta: return val | |
alpha = max(alpha, val) |
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
// T is the type of input array elements | |
// V is the type of required aggregate statistic | |
template<class T, class V> | |
class SegmentTree { | |
SegmentTreeNode* nodes; | |
int N; | |
public: | |
SegmentTree(T arr[], int N) { | |
this->N = N; |
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
#define Possible_Wins 8 | |
const int Three_in_a_Row[Possible_Wins][3] = { | |
{ 0, 1, 2 }, | |
{ 3, 4, 5 }, | |
{ 6, 7, 8 }, | |
{ 0, 3, 6 }, | |
{ 1, 4, 7 }, | |
{ 2, 5, 8 }, | |
{ 0, 4, 8 }, | |
{ 2, 4, 6 } |
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 A*-GRAPH-SEARCH (start): | |
Let pq be an empty min priority queue | |
Let closed be an empty set | |
g(start) = 0 | |
f(start) = h(start) | |
path(start) = [] | |
pq.push(start, f(start)) | |
while not pq.empty(): |
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 A*-TREE-SEARCH (start): | |
Let pq be an empty min priority queue | |
g(start) = 0 | |
f(start) = h(start) | |
path(start) = [] | |
pq.push(start, f(start)) | |
while not pq.empty(): | |
top = pq.pop() |
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
interpolation_search(A[0...n-1], x): | |
i = 0, j = n-1, lo = A[i], hi = A[j] | |
if x < lo: | |
return -1 | |
if x >= hi: | |
i = j | |
while i < j: | |
k = i + ((j - i) * (x - lo)) / (hi - lo) |
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
update(ft, p, v): | |
for (; p <= N; p += p&(-p)) | |
ft[p] += v | |
# Add v to A[a...b] | |
update(a, b, v): | |
update(B1, a, v) | |
update(B1, b + 1, -v) | |
update(B2, a, v * (a-1)) | |
update(B2, b + 1, -v * 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 findMedian(vector<vector<int> > &A) { | |
int mn = A[0][0], mx = A[0][0], n = A.size(), m = A[0].size(); | |
for (int i = 0; i < n; ++i) { | |
if (A[i][0] < mn) mn = A[i][j]; | |
if (A[i][m-1] > mx) mx = A[i][j]; | |
} | |
int desired = (n * m + 1) / 2; | |
while (mn < mx) { | |
int mid = mn + (mx - mn) / 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
let A be the given array of integers | |
let minSum = infinity, minLeft = 0, minRight = 0, currentMin = 0, left = 0, right = 0 | |
for i = 0 to A.length - 1 | |
currentMin += A[i] | |
if currentMin < minSum | |
minSum = currentMin | |
right = i; | |
minLeft = left | |
minRight = right | |
if currentMin > 0 |
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
template <typename T> | |
class QueueWithMin { | |
private: | |
queue<T> Q; | |
deque<T> D; | |
public: | |
void enqueue(T& x) { | |
Q.push(x); | |
while (!D.empty() && D.back() > x) | |
D.pop_back(); |
NewerOlder