Skip to content

Instantly share code, notes, and snippets.

Avatar

Robert King robert-king

View GitHub Profile
View google_kickstart_parcels.rs
/*
problem: https://codingcompetitions.withgoogle.com/kickstart/round/00000000008f4a94/0000000000b55465
youtube: https://youtu.be/Yvj18KBThsE (subscribe)
twitter: https://twitter.com/robertkingNZ (follow)
*/
use std::collections::VecDeque;
use std::usize;
View hidden_pancakes.rs
// problem: https://codingcompetitions.withgoogle.com/codejam/round/0000000000435915/00000000007dc20c
// video: https://www.youtube.com/c/RobertKing/videos
// twitter: https://twitter.com/robertkingNZ
pub struct Combination {
fact: Vec<usize>,
inv_fact: Vec<usize>,
modulo: usize,
}
@robert-king
robert-king / controlled_inflation.rs
Last active May 2, 2022
Rusty Rob Codejam 2022 1B
View controlled_inflation.rs
// video here: https://www.youtube.com/c/RobertKing/videos
fn dist(x: u64, y: u64) -> u64 {
if x <= y {
return y - x;
}
x-y
}
fn min(x: u64, y: u64) -> u64 {
@robert-king
robert-king / chain-reactions.rs
Created Apr 3, 2022
Google Codejam 2022 Chain Reactions with rust and topological sort
View chain-reactions.rs
/*
twitter: @robertkingnz
youtube: https://www.youtube.com/c/RobertKing/videos
topological sort found here: https://github.com/kenkoooo/competitive-programming-rs/blob/master/src/graph/topological_sort.rs
Google Code Jam 2022 Chain Reactions: https://codingcompetitions.withgoogle.com/codejam/round/0000000000876ff1/0000000000a45ef7
*/
pub fn topological_sort(graph: &[Vec<usize>], mut fun: Vec<u64>) -> u64 {
let n = graph.len();
View challenge-nine.rs
fn main() {
let (r, w) = (std::io::stdin(), std::io::stdout());
let mut sc = IO::new(r.lock(), w.lock());
let t: usize = sc.read();
for case_num in 1..=t {
let mut v: Vec<usize> = sc.chars().into_iter().map(|c| {
((c as u8) - b'0') as usize
}).collect();
@robert-king
robert-king / h-index.rs
Last active Feb 17, 2022
Google Kickstart 2020 practice: Milk Tea + Hex + H-Index solutions using Rust
View h-index.rs
use std::cmp::Reverse;
fn main() {
let (r, w) = (std::io::stdin(), std::io::stdout());
let mut sc = IO::new(r.lock(), w.lock());
let t: usize = sc.read();
for case_num in 1..=t {
let mut heap = std::collections::BinaryHeap::new();
let n: usize = sc.read();
View fast_longest_palindromic_substring.rs
// @robertkingnz
// this rust implementation reduces the number of centers that need checking
// it does so by only trying one possible center for each group of repeating characters.
// this is one of the few 0ms solutions on leetcode,
// although there is a faster, more complex, O(N) solution possible.
impl Solution {
pub fn longest_palindrome(s: String) -> String {
let b = s.as_bytes();
fn expand(mut i: usize, mut j: usize, b: &[u8]) -> (usize, usize) {
@robert-king
robert-king / faster_sort.go
Created Dec 31, 2020
golang fast concurrent sorting
View faster_sort.go
package main
import (
"math/rand"
"fmt"
"time"
"sort"
)
const N = 1 << 20
@robert-king
robert-king / hashcode 2020 online qualification.py
Created Feb 20, 2020
google hashcode 2020 online qualification
View hashcode 2020 online qualification.py
# incomplete code by @robertkingNZ of team @perplexico_
import sys
import collections
Library = collections.namedtuple('Library', ['id', 'signup', 'per_day', 'bookids'])
def get_problem_from_file(f):
lines = (line for line in f.read().splitlines())
rows = (list(map(int, line.split())) for line in lines)
View shutdown_servers.py
# given matrix of servers, servers connect to servers in their row or column. 'C' denotes a server computer.
# shutdown the servers in an order such that shutdown servers pass their data on to nearby servers, such that
# the minimum number of servers need to stay on.
# author: @robertkingnz
def make():
M = []
rows = 10
cols = 20