Skip to content

Instantly share code, notes, and snippets.

View uztbt's full-sized avatar

Yuji Tabata uztbt

View GitHub Profile
@uztbt
uztbt / Dijkstra.py
Created January 15, 2021 11:50
A Dijkstra's shortest path algorithm implementation in Python 3
class Graph:
def __init__(self, V: set[str], E: dict[dict[str, int]]) -> None:
super().__init__()
self.V = V
self.E = E
def dijkstra(self, s: str, t: str) -> int:
"""
Returns the shortest path from s to t
"""
@uztbt
uztbt / RoadsAndLibrariesSlow.py
Created January 17, 2021 13:33
Slow solution to Roads And Libraries
# https://www.hackerrank.com/challenges/torque-and-development/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=graphs
from collections import defaultdict
def roadmap(cities: list[list[int]]) -> dict[int, set[int]]:
d = defaultdict(set)
for item in cities:
d[item[0]].add(item[1])
d[item[1]].add(item[0])
return d
@uztbt
uztbt / RoadsAndLibrariesFast.py
Last active January 17, 2021 13:37
Fast solution to Roads and Libraries
# https://www.hackerrank.com/challenges/torque-and-development/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=graphs
from collections import defaultdict
def roadmap(cities: list[list[int]]) -> dict[int, set[int]]:
d = defaultdict(set)
for item in cities:
d[item[0]].add(item[1])
d[item[1]].add(item[0])
return d
@uztbt
uztbt / LargestRectangle.py
Created January 23, 2021 12:39
My solution to a Hacker Rank problem "Largest Rectangle"
# My solution to https://www.hackerrank.com/challenges/largest-rectangle/problem?h_r=internal-search
def largestRectangle(h: list):
stack = [(0, 0)]
h.append(0)
largest = 0
for i, height in enumerate(h, 1):
tallest = stack[-1]
leftWall = i
while tallest[0] > height:
@uztbt
uztbt / annuity.tex
Created February 7, 2021 16:14
My solution to a corporate finance homework
\documentclass[11pt]{report}
% To set margin width, text height, space for footnotes and all sorts
% of other settings related to the geometry of the pages in your
% report, use the 'geometry' package.
%
% http://tug.ctan.org/cgi-bin/ctanPackageInformation.py?id=geometry
\usepackage[margin=2cm]{geometry}
import { RenderCustomNodeElementFn } from "react-d3-tree/lib/types/common";
export const LogiNode: RenderCustomNodeElementFn = ({ nodeDatum, toggleNode}) => {
const [x, y] = [0, -50];
const [width, height] = [100, 100];
return(
<g>
<rect width={width} height={height} x={x} y={y} onClick={toggleNode}>
</rect>
<text
@uztbt
uztbt / LogiNode.tsx
Created March 6, 2021 07:12
Centering the text in nodes with a <foreignObject> tag
import { RenderCustomNodeElementFn } from "react-d3-tree/lib/types/common";
export const LogiNode: RenderCustomNodeElementFn = ({ nodeDatum, toggleNode}) => {
const [x, y] = [0, -50];
const [width, height] = [100, 100];
return(
<g>
<rect x={x} y={y} width={width} height={height} onClick={toggleNode}>
</rect>
<foreignObject x={x} y={y} width={width} height={height}>
def isValid(s: str) -> bool:
counter = Counter(s)
freqList = counter.most_common(None)
freqDict = defaultdict(list)
for freqTuple in freqList:
freqDict[freqTuple[1]].append(freqTuple[0])
fd = dict(freqDict)
if len(fd.keys()) == 1:
return "YES"
if len(fd.keys()) == 2:
def quicksort(arr: List[int])->List[int]:
l = len(arr)
if l == 0:
return []
elif l == 1:
return arr
else:
pivot = arr.pop(0)
former = []
latter = []
def abbreviation(a: str, b: str) -> str:
dfs = [(len(a) - 1, len(b) - 1)]
dp = set()
while len(dfs) > 0:
pa, pb = dfs.pop()
if (pa, pb) in dp:
# (pa, pb) has already tested false
continue
dp.add((pa, pb))
if pb < 0: