Skip to content

Instantly share code, notes, and snippets.

View RP-3's full-sized avatar

Rohan Rogers RP-3

  • San Francisco Bay Area
View GitHub Profile
@RP-3
RP-3 / solution.js
Last active July 8, 2022 03:10
LC: 2096. Step-By-Step Directions From a Binary Tree Node to Another
/*
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/
IDEA:
- If we find the lowest common ancestor of the two nodes we're looking for, then we just
need to find the path from the start to the LCA, and from the LCA to the target.
- Maintain two stacks; one for the start, and one for the end.
- The path from the LCA to the start node will just be some number of 'U's, because
the LCA must be equal to or above the start node (by definition).
- The path from the LCA to the end node will be some number of 'L's or 'R's.
@RP-3
RP-3 / gis.sh
Last active December 27, 2020 08:44
Useful GIS manipulation commands
#convert SRID to _______ outputDirectory inputShapefileWithAdjacent .dbf, .prj, .shx files
ogr2ogr -t_srs EPSG:4326 ~/Desktop/london_boroughs greater_london_const_region.shpu
#convert to psql
# creating tables where necessary
# inputShapefileWithAdjacent .dbf, .prj, .shx files
# piping the output to this postgis database
shp2pgsql -c ~/Desktop/london_boroughs/greater_london_const_region.shp | psql -d os_boundaries
# BotsonGIS cheatsheet
@RP-3
RP-3 / Writeup.js
Created October 18, 2020 22:31
Buy & Sell Stock with K Transactions
/*
Pure recursive approach.
Branching factor of, at worst, a.length.
Yields O(len(a)^len(a)), which is about has bad as it gets...
*/
const stockWithKTransactionsRecursive = (k, a) => {
const maxProfit = (i, k) => { // on the ith day, with k transactions remaining
if(i <= 0) return 0; // if it's day 0, we can't make a profit since we need at least one previous day to buy on
if(k === 0) return 0; // if we have no transactions remaining, just return 0;
type void struct{}
type g = map[string](map[string]float64)
type stringSet = map[string]void
var member void
func calcEquation(equations [][]string, values []float64, queries [][]string) []float64 {
// 1. Make a directed, weighted graph out of each equation
graph, seen := make(g), make(stringSet)
for i, vars := range equations {
/*
IDEA: Iff:
- The robot is facing North after the instructions, AND
- The robot was displaced by any non-zero amount, THEN
the robot will hare off into the sunset.
Otherwise, the robot will loop endlessly.
Proof: Lots of drawing on square-ruled paper.
*/
func findMaximumXOR(nums []int) int {
result := 0
root := trieNode{}
for _, num := range nums {
maxAgainstNum := root.maxAgainst(num)
if maxAgainstNum > result {
result = maxAgainstNum
}
root.insert(num)
/**
* // Definition for a Node.
* function Node(val) {
* this.val = val;
* this.left = null;
* this.right = null;
* this.parent = null;
* };
*/
/**
* @param {number[]} nums
* @return {number}
*/
/*
IDEA:
- Do a prefix-multiply, i.e., a rolling product from left to
right.
- This strategy depends on the number of negative numbers in
the input.
/**
* @param {string} secret
* @param {string} guess
* @return {string}
*/
var getHint = function(secret, guess) {
let [bulls, cows] = [0, 0];
const secretDigits = new Array(10).fill(0);
for(let i=0; i<secret.length; i++){
/**
* @param {number[]} A
* @return {number[]}
*/
// O(n**2) time. O(1) space.
// beats 95%!
var pancakeSort = function(A) {
const wc = [];
let end = A.length-1;