Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
kartikkukreja / IterativeDeepeningAlphaBetaSearch.py
Created July 11, 2015 18:13
Iterative Deepening Alpha Beta Search
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)
@kartikkukreja
kartikkukreja / Segment tree template.cpp
Last active December 13, 2022 12:02
Segment tree template
// 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;
@kartikkukreja
kartikkukreja / Tic Tac Toe Heuristic.cpp
Last active November 1, 2022 13:04
Tic Tac Toe Heuristic
#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 }
@kartikkukreja
kartikkukreja / A* Graph Search Pseudocode.py
Last active September 22, 2022 16:44
A* Graph Search Pseudocode
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():
@kartikkukreja
kartikkukreja / A* Tree Search Pseudocode.py
Last active September 22, 2022 16:44
A* Tree Search Pseudocode
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()
@kartikkukreja
kartikkukreja / Interpolation Search.py
Last active April 26, 2019 04:54
Interpolation Search
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)
@kartikkukreja
kartikkukreja / Range Updates and Range Queries.py
Last active November 1, 2017 20:07
Range Updates and Range Queries with BIT
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)
@kartikkukreja
kartikkukreja / matrix-median.cpp
Last active October 10, 2017 17:12
matrix-median
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;
@kartikkukreja
kartikkukreja / Minimum Subarray Problem.py
Last active July 11, 2017 00:42
Minimum Subarray Problem
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
@kartikkukreja
kartikkukreja / queue-with-min-using-deque.cpp
Created October 27, 2016 20:07
Queue with min operation using deque
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();