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
/**
* @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;
/**
* The rand7() API is already defined for you.
* var rand7 = function() {}
* @return {number} a random integer in the range 1 to 7
*/
/*
IDEA: Make two calls to rand7 to get a value from this table:
(each value is the sum of the two calls to rand7())
1. 2 3. 4. 5. 6. 7.
1. 2 3 4 5 6 7 8
/*
IDEA: If we have a sorted list of starts and ends,
then scanning from left to right, every time we
see a start, that must be the closest interval to
the right of all the ends we've seen so far.
O(nlogn) // nlogn for sorting, + n for a single scan.
*/
var findRightInterval = function(intervals) {
// 1. Break intervals into start and end events
@RP-3
RP-3 / Sorry parity.js
Created August 21, 2020 12:17
Leetcode sort by parity
/**
* @param {number[]} A
* @return {number[]}
*/
var sortArrayByParity = function(A) {
let [l,r] = [0,A.length-1]
while(l<r){
if(A[l] %2){
[A[l],A[r]] = [A[r],A[l]]
r--
// O(n) time, O(1) space
var reorderList = function(head) {
if(!head || !head.next) return;
// split in half
let [slow, fast, prev] = [head, head, null];
while(fast && fast.next){
prev = slow;
slow = slow.next;
fast = fast.next.next;
/**
* @param {string} S
* @return {string}
*/
const v = 'aeiouAEIOU';
var toGoatLatin = function(S) {
return S.split(' ')
.map((w, i) => (v.indexOf(w[0]) >= 0 ? w : (w.slice(1) + w[0])) + 'ma' + 'a'.repeat(i+1))
/*
IDEA: We can build up a valid solution from left to right.
- Start with every legal number (1-9)
- For each starting point
- append N+K, and keep building if legal
- append N-K, and keep building if legal
- If you get to K digits, this is an answer, so add it to your result
Time Complexity:
- This is a recursive function with a branching factor of 2 and a
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function(intervals) {
intervals.sort((a, b) => a[0] - b[0]); // asc by start time
let [result, slow, fast] = [0, 0, 1];
/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function(intervals) {
intervals.sort((a, b) => a[0] - b[0]); // asc by start time
let [result, slow, fast] = [0, 0, 1];