Skip to content

Instantly share code, notes, and snippets.

View jan25's full-sized avatar
🎯
Focusing

Abhilash Gnan jan25

🎯
Focusing
View GitHub Profile
@jan25
jan25 / dinics-algorithm.py
Last active May 29, 2021 19:40
Dinics maximum flow in directed graph algorithm
# Dinics Algorithm
# - Find level graph (DAG)
# - Find blocking flows in level graph
# - Continue as long as no more flow can be added
# - O(EV^2) time O(EV) for each phase in O(V) level graphs
INF = 10**9
class Edge:
def __init__(self, u, v, cap):
@jan25
jan25 / researcher.py
Last active June 18, 2020 12:16
researcher.py
class Solution:
def hIndex(self, citations: List[int]) -> int:
cnt = defaultdict(int)
h, nGreaterThanH = 0, 0
for c in citations:
# Count elements greater than current h
if c > h:
cnt[c] += 1
def foo(dict, n=2):
if n == 0: return
dict1 = dict
dict[n] = n
foo(dict, n - 1)
dict2 = dict
class Solution:
def __init__(self, N):
self.n = N
# Auxilary DS to store left/right occupied room for a given occupied room
# Useful in updating heap when book() or leave() is called
self.left_occupied = {}
self.right_occupied = {}
@jan25
jan25 / circle-fights.cpp
Last active March 15, 2019 16:42
Players fighting in a circle
vector<int> get_losers_for_me(int player) {
return vector<int>();
}
bool dp(int opponent, int st, int en) {
int opponent_index = get_player_index(opponent, st, en);
if (opponent_index == -1) return false;
if (abs(st - en) == 0) return true;
if (DP[opponent][st][en] != -1) return DP[opponent][st][en];
class Solution:
def integerReplacement(self, n):
steps = 0
while n > 1:
if n == 3:
steps += 2; break
if n & 1 ^ 1:
n >>= 1
else:
if (n & 3) == 3:
@jan25
jan25 / odd-even-jump.cxx
Created January 20, 2019 20:20
Odd even jump -- weekly 118 Leetcode
class Solution {
public:
int oddEvenJumps(vector<int>& A) {
map<int, int> map_to_smallest_index;
map<int, int> next_even;
map<int, int> next_odd;
for (int i = A.size() - 1; i >= 0; --i) {
next_even[i] = i; // self edge
next_odd[i] = i; // self edge
class Solution:
def firstMissingPositive(self, nums):
nums += [0]
for i, num in enumerate(nums):
while nums[i] > 0 and nums[i] < len(nums) and nums[i] != i:
j = nums[i]
prev = nums[i]
nums[i], nums[j] = nums[j], nums[i]
if nums[i] == prev: break
class Solution:
def __init__(self, N, blacklist):
self.N = N - 1
self.blacklist = set(blacklist)
def pick(self):
while self.N in self.blacklist:
self.N -= 1
return self.N
class Solution:
def __init__(self, N, blacklist):
blacklist.sort()
self.N = N - len(blacklist)
self.free_pairs = self.prep_free_pairs(N, blacklist)
self.offsets = [0]
for pair in self.free_pairs:
self.offsets.append(pair[1] - pair[0] + 1)
self.offsets[-1] += self.offsets[-2]