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
using pairType = std::pair<int, int>; | |
using graphType = std::unordered_map<int, vector<pairType> >; | |
class Solution { | |
private: | |
int bellmanFord(const int & K, const int &N, std::vector<int> &delay, | |
graphType & graph, const int &TIMEOUT ){ | |
int d = -1; | |
delay[K] = 0; |
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
using pairType = std::pair<int, int>; | |
using graphType = std::unordered_map<int, vector<pairType> >; | |
using heapType = std::priority_queue<pairType, vector<pairType> >; | |
class Solution { | |
private: | |
int dijkstra(const int & K, const int &N, std::vector<int> &delay, | |
graphType & graph, const int &TIMEOUT ){ | |
int d = -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
class Solution: | |
def dijkstra(self, K, N, delay, graph, TIMEOUT): | |
Q = set(range(N)) | |
delay[K-1] = 0 | |
hp = [] | |
heapq.heappush(hp, (0, K-1)) | |
while len(hp) > 0: | |
_, u = heapq.heappop(hp) | |
for v, w in graph[u].items(): | |
if delay[u] + w < delay[v]: |
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 bellman_ford(self, K, N, delay, graph, TIMEOUT): | |
delay[K-1] = 0 | |
for i in range(N): | |
for u, vs in graph.items(): | |
for v, w in vs.items(): | |
if delay[v] > delay[u] + w: | |
delay[v] = delay[u] + w | |
d = max(delay) |
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 prim(nodes, edges): | |
conn = {} | |
for u, v, w in edges: | |
if u not in conn: | |
conn[u] = [(w,u,v)] | |
else: | |
conn[u].append((w, u, v)) | |
if v not in conn: | |
conn[v] = [(w,v,u)] | |
else: |
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 union_find(u, arr): | |
if arr[u] == u: | |
return u | |
return union_find(arr[u], arr) | |
def union(u, v, arr): | |
arr[union_find(v, arr)] = union_find(u, arr) | |
def kruskal(nodes, edges): | |
mst = [] |
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
#include <iostream> | |
template<typename T=int> | |
class edgeType{ | |
private: | |
char from; | |
char to; | |
T w; | |
public: | |
edgeType(char a, char b, int c):from(a),to(b),w(c) {} |
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 PredictTheWinner(self, c): | |
""" | |
:type nums: List[int] | |
:rtype: bool | |
""" | |
r = len(c) | |
t = sum(c) | |
s = [[0 for i in range(r)] for j in range(r)] | |
for l in range(r): # l is for the length - 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
class Solution { | |
public: | |
bool PredictTheWinner(vector<int>& nums) { | |
int length = nums.size(); | |
std::vector< std::vector<int> > s(length, std::vector<int> (length)); | |
for(int l = 0; l < length; l++){ | |
for(int i=0; i < length - l; i++){ | |
if(l==0) s[i][l] = nums[i]; | |
else if (l==1) s[i][l] = max(nums[i], nums[i+1]); | |
else if (l==2) s[i][l] = max(nums[i] + min(nums[i+1], nums[i+2]), |
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: | |
bool check_deadends(unordered_set<string>& deadends, const string & target){ | |
if(deadends.find(target) != deadends.end()) return true; | |
deadends.insert(target); | |
return false; | |
} | |
bool check_target(const string & a, const string & b){ | |
return (a==b); |