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 <algorithm> | |
#include <cassert> | |
#include <cmath> | |
#include <complex> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#include <ctime> | |
#include <deque> | |
#include <iostream> |
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
#!/bin/python | |
import sys | |
MOD = 10**9 + 7 | |
n = int(raw_input().strip()) | |
adj_list = [[] for x in xrange(n)] | |
for a0 in xrange(n-1): | |
u,v = raw_input().strip().split(' ') |
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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
from collections import defaultdict, Counter, deque |
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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the countArray function below. | |
def countArray(n, k, x): |
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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the countArray function below. | |
def countArray(n, k, x): |
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
from functools import lru_cache | |
class Solution: | |
def minKnightMoves(self, x: int, y: int) -> int: | |
@lru_cache(None) | |
def DP(x,y): | |
if x + y == 0: | |
return 0 | |
elif x + y == 2: | |
return 2 | |
return min(DP(abs(x-1),abs(y-2)),DP(abs(x-2),abs(y-1)))+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 minKnightMoves(self, x: int, y: int) -> int: | |
if (x, y)==(0, 0):return 0 | |
def bfs(i, j, x, y): | |
open_list = [(i,j,0)] | |
seen = {(i, j)} | |
for i, j, d in open_list: | |
for di, dj in [(1,2),(2,1),(1,-2),(2,-1), | |
(-1,2),(-2,1), (-1,-2),(-2,-1)]: | |
r, c = i+di, j+dj |
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
from collections import deque | |
class Solution: | |
def minKnightMoves(self, x: int, y: int) -> int: | |
if (x, y)==(0, 0):return 0 | |
def bfs(i, j, x, y): | |
open_list = deque([(i,j,0)]) | |
seen = {(i, j)} | |
while open_list: | |
i, j, d = open_list.popleft() | |
for di, dj in [(1,2),(2,1),(1,-2),(2,-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 minKnightMoves(self, x: int, y: int) -> int: | |
if (x, y)==(0, 0):return 0 | |
def bfs(open_list, seen): | |
open_list_new = [] | |
for i, j in open_list: | |
for di, dj in [(1,2),(2,1),(1,-2),(2,-1), | |
(-1,2),(-2,1), (-1,-2),(-2,-1)]: | |
r, c = i+di, j+dj | |
if (r,c) not in seen and -4<r<abs(x)+4 and -4<c<abs(y)+4: |
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 minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int: | |
N, self.uf, self.uf1 =len(graph), {}, {} | |
def union(x, y, uf): | |
uf[find(y, uf)] = find(x, uf) | |
def find(x, uf): | |
uf.setdefault(x, x) | |
if x!=uf[x]: | |
uf[x] = find(uf[x], uf) | |
return uf[x] |