Skip to content

Instantly share code, notes, and snippets.

View uztbt's full-sized avatar

Yuji Tabata uztbt

View GitHub Profile
@uztbt
uztbt / two_sum.rs
Last active April 25, 2022 15:26
My first competitive program in Rust - https://leetcode.com/problems/two-sum/
use std::collections::HashMap;
struct Solution();
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut hm: HashMap<i32, Vec<i32>> = HashMap::new();
for i in 0..nums.len() {
hm.entry(nums[i]).or_insert(vec![]).push(i as i32);
}
use std::ops::Deref;
struct CustomSmartPointer {
data: String,
}
impl Deref for CustomSmartPointer {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.data
}
@uztbt
uztbt / fib.rs
Created April 9, 2022 13:17
An exercise of Chaper 3 of the Book
use std::io;
fn main() {
loop {
println!("Input a number!");
let mut nth = String::new();
io::stdin()
.read_line(&mut nth)
.expect("Error in reading nth");
let nth: u32 = match nth.trim().parse() {
@uztbt
uztbt / topologicalSortForUndirectedGraph.ts
Created September 11, 2021 04:09
Topological sort for undirected graph.
function topologicalSort(n: number, edges: number[][]): number[] {
const numberOfEdges = Array(n).fill(0);
const bidirectionalEdges: number[][] = [];
for (let i = 0; i < n; i++) {
bidirectionalEdges.push([]);
}
const sorted: number[] = [];
// Construct the # of edges dictionary
for (const [nodeA, nodeB] of edges) {
numberOfEdges[nodeA]++;
@uztbt
uztbt / KMPSearch.ts
Last active April 10, 2022 10:05
Visit https://yuji.page/kmp-algorithm/ for more information.
export function KMPsearch(s: string, w: string): number[] {
const acc: number[] = [];
const LPS: number[] = createLPSArray(w);
let [i, j] = [0, 0];
while (i < s.length) {
if (s[i] === w[j]) {
i += 1;
j += 1;
if(j === w.length) {
acc.push(i-j);
function createLPSArray(w: string): number[] {
const lps: number[] = [0];
let len = 0;
let i = 1;
while (i < w.length) {
if (w.charAt(i) === w.charAt(len)) {
lps.push(++len);
i++;
} else {
if (len !== 0) len = lps[len-1]; // This is the big point!
class TNode {
letter: string;
num: number;
children: { [letter: string]: TNode };
constructor(letter: string) {
this.letter = letter;
this.num = 1;
this.children = {};
}
}
function prims(n: number, edges: number[][], start: number): number {
// Write your code here
const edgeDict: {[s: number]: number[][]} = {};
// Create the edgeDict
for (const edge of edges) {
const [s, t] = [edge[0], edge[1]];
if (typeof edgeDict[s] === "undefined") edgeDict[s] = [edge];
else edgeDict[s].push(edge);
if (typeof edgeDict[t] === "undefined") edgeDict[t] = [edge];
else edgeDict[t].push(edge);
class TNode {
v: number;
parent: TNode;
rank: number;
weight: number;
constructor(v: number) {
this.v = v;
this.parent = null;
this.rank = 0;
this.weight = 0;
import { Tree, TNode } from './printTree';
test("Only root", () => {
const root = new TNode("1");
const tree = new Tree(root);
tree.print();
expect(true).toBe(true);
})
test("same value length, height 2", () => {