Skip to content

Instantly share code, notes, and snippets.

View robert-king's full-sized avatar

Robert King robert-king

View GitHub Profile
@robert-king
robert-king / pizza_delivery.rs
Last active October 12, 2022 19:48
kickstart pizza delivery
/*
@robertkingnz
https://www.youtube.com/c/RobertKing/videos -> https://youtu.be/Z5WzJ9jbnKg
https://codingcompetitions.withgoogle.com/kickstart/round/00000000008cb0f5/0000000000ba86e6
*/
extern crate core;
fn calc_toll(c: i64, toll: &(char, i64)) -> i64 {
let ans = match toll.0 {
@robert-king
robert-king / winetasting.rs
Last active August 20, 2022 23:18
meta hacker cup winetasting
/*
https://www.youtube.com/c/RobertKing/videos
@robertkingnz
problem: https://www.facebook.com/codingcompetitions/hacker-cup/2011/round-1a/problems/C
video:
*/
use std::collections::HashMap;
pub struct Combination {
/*
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;
@robert-king
robert-king / hidden_pancakes.rs
Created May 8, 2022 04:26
codejam 2021 round 2
// 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 10:21
Rusty Rob Codejam 2022 1B
// 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 April 3, 2022 03:03
Google Codejam 2022 Chain Reactions with rust and topological sort
/*
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();
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 October 14, 2022 03:50
Google Kickstart 2020 practice: Milk Tea + Hex + H-Index solutions using Rust
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();
// @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 December 31, 2020 04:58
golang fast concurrent sorting
package main
import (
"math/rand"
"fmt"
"time"
"sort"
)
const N = 1 << 20