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 {string} s
* @return {number}
*/
var longestPalindrome = function(s) {
const counts = new Array(123 - 65).fill(0);
const A = 'A'.charCodeAt(0);
for(let i=0; i<s.length; i++){ // the only branching condition!
/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function(s) {
const counts = new Array(123 - 65).fill(0);
const A = 'A'.charCodeAt(0);
for(let i=0; i<s.length; i++){ // the only branching condition!
var CombinationIterator = function(characters, combinationLength) {
this.storage = characters.split('');
this.hasMore = combinationLength <= characters.length;
this.ptrs = new Array(combinationLength).fill(0).map((_, i) => i);
};
CombinationIterator.prototype.resetAfter = function(i){
let nextIndex = this.ptrs[i] + 1;
for(let j = i+1; j<this.ptrs.length; j++){
if(nextIndex >= this.storage.length) return false;
func getRow(rowIndex int) []int {
if rowIndex == 0 { return []int{1} }
if rowIndex == 1 { return []int{1, 1} }
result, next := []int{1, 1}, []int{}
for i := 2; i <= rowIndex; i++ { // for each row
for j := 0; j <= len(result); j++ { // generate it based on the prev row
var prev, curr int
switch j {
import (
"fmt"
"sort"
)
// Approach 1:
// Sort first in asc order.
// Then we know that for each index i,
// there are at least n-i papers with at least
// array[i] references. Iterate backwards
import "math"
func titleToNumber(s string) int {
result := 0
l := len(s)
zero := 'A' - 1
for i := l - 1; i >= 0; i-- {
result += int(math.Pow(26, float64(l-i-1)) * float64((s[i] - byte(zero))))
package main
// TreeNode Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func _pathSum(root *TreeNode, remainder, target int, memo map[int]int) int {
// Entry in our intermediate result
type Entry struct {
pos int
vals []int
}
// Entries is just a list of Entry-ies
type Entries []Entry
func (e Entries) Len() int {
/*
Idea: We've been told that each number in the list is in the range 1:length (inclusive), so
we can use each index in the array as a flag to check if we've seen a number before or not.
Approach: For each number, its index in the array is itself - 1 (because arrays are zero-
indexed). Flip the number at that index to be negative, so if we encounter a number and the
value already at that index is negative, we know we've seen it before, and can add it to our
result array.
*/
func findDuplicates(nums []int) []int {
/**
* Initialize your data structure here.
*/
var WordDictionary = function() {
this.storage = new Array(26).fill(null);
};
/**
* Adds a word into the data structure.
* @param {string} word