This file contains hidden or 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
# 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): |
This file contains hidden or 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 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 |
This file contains hidden or 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 foo(dict, n=2): | |
if n == 0: return | |
dict1 = dict | |
dict[n] = n | |
foo(dict, n - 1) | |
dict2 = dict |
This file contains hidden or 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 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 = {} |
This file contains hidden or 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
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]; | |
This file contains hidden or 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 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: |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 | |
This file contains hidden or 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 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 |
This file contains hidden or 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 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] |
NewerOlder