Back Original

Algorhythms

https://leetcode.com/problems/two-sum/description/ https://leetcode.com/problems/two-sum-ii-input-array-is-sorted

capo on 2 Am, Em, C, G Bridge: F and whatever

Given an array of integers nums and an integer target return indices of the two numbers such that they add up to target

Why / Who’s asking / Who needs to know?!

Well I suppose If I only knew my index I could reason about where I am

in this crazy world and now that you mention it If I could just find my complement these winters wouldn’t be so cold

answer to two sum](https://leetcode.com/problems/two-sum/description/)

var twoSum = function(nums, target) {
    for (let i = 0; i < nums.length; i++){
        for (let j = i+1; j < nums.length; j++){
            if (nums[i] + nums[j] === target) {
                return [i, j];
            }
        }
    }
};

but I digress let me venture forth armed with only my naivete I will wander through every point trying every permutation

Bridge F whatever and in this way I might find My target

stepping forward with full faith that I will discover everything

fails as two sum II answer because no 1 indexed

// okay fine
var twoSum = function(nums, target) {
    for (let i = 0; i < nums.length; i++){
        for (let j = i+1; j < nums.length; j++){
            if (nums[i] + nums[j] === target) {
                return [i+1, j+1];
            }
        }
    }
};

only to find I have neither the time nor the resources

Weirdly this actually works for two sum II where input is sorted, but it probably shouldn’t

brute force blood sweat and tears will yield salvation will it not?

This time it did but I count myself warned

in this world hope isn’t enough I must create my own luck grinding through those late nights scheming, bent over a hash map

var twoSum = function(nums, target) {
    let numMap = new Map()
 
    for (let i = 0; i < nums.length; i++){
        numMap.set(nums[i] ,i)
    }
 
    for (let i = 0; i < nums.length; i++){
        let compliment = target - nums[i]
        if (numMap.has(compliment) && numMap.get(compliment) != i){
            return [i, numMap.get(compliment)]
        }
    }
    
};
var search = function(nums, target, left=0, right=nums.length-1) {
    if (left > right) return -1
    const mid = Math.floor((right + left)/2)
    if (nums[mid] === target) return mid
    if (nums[mid] < target)return search(nums, target, mid + 1, right)
    return search(nums, target, left, mid - 1)
};

drafts----

7) Depth First Search 20) Flood Fill (Again)

to cherry wine? Depth first search is often implemented recursively it’s generally more memory efficient

drill down to the bottom node of a tree when exploring all possible paths is necessary

--- improvise chorus

breadth first search is usually queue based because it’s about deferring paths that have been found you’re like oh hey that’s a valid path but I already have stuff to do so I’m gonna do that first

— improvise chorus maybe cracking joke about how DFS is more ADHD and BFS is sort like like a brain with more efficient executive functioning?

function floodFill(image, sr, sc, color){
	let originalColor = image[sr][sc]
	if (originalColor === color) return image
 
	let rows = image.length
	let cols =  image[0].length
	
	function dfs(sr, sc){
		if (sr < 0 || sr >= rows || sc < 0 || sc >= cols) return
	    if (image[sr][sc] !== originalColor ) return
	    image[sr][sc] = color
	    dfs(sr+1, sc)
	    dfs(sr-1, sc)
	    dfs(sr, sc+1)
	    dfs(sr, sc-1)
	    return
	}
 
    dfs(sr, sc)
    return image
}

The row and column is relevant as

capo 2 Am E7sus4 E7 The only time I ever implemented a heap F C E7sus4 E7 was on a whiteboard in hopper and it kinda of made sense Dm G C Cmaj9/B Am wishing I knew python so I never had to worry about it G E7sus4 E Am in the end I just had to mention it sometimes to look like I knew what I was talking about

F                E7sus4  E7

And I long to sort things efficiently Am Gadd4 F Em Dm but rarely is it my top priority Dm E7sus4 E7 a priority queue, in all likelihood Am Gadd4 F Em F Em E7sus4 E7 is something I will copy and paste