Skip to content

Instantly share code, notes, and snippets.

View ykdojo's full-sized avatar

YK ykdojo

View GitHub Profile
# Here is my Python implementation of the hash table data structure.
# And here's my video where I talk about it in depth: https://youtu.be/sfWyugl4JWA
class Hashtable:
# Assumption: table_length is a prime number (for example, 5, 701, or 30011)
def __init__(self, table_length):
self.table = [None] * table_length
## An internal search function.
# If it finds the given key in the table, it will return (True, index)
# If not, it will return (False, the index where it would be inserted)
# My video where I talk about this data structure: https://youtu.be/sfWyugl4JWA
class Hashtable:
# Assumption: table_length is a prime number (for example, 5, 701, or 30011)
def __init__(self, table_length):
self.table = [None] * table_length
## An internal search function.
# If it finds the given key in the table, it will return (True, index)
# If not, it will return (False, the index where it would be inserted)
class ST:
def build(self, arr):
max_len = 4 * len(arr)
self.st = [None] * max_len
self.arr_len = len(arr)
self.helper(arr, 1, 0, len(arr) - 1)
def helper(self, arr, v, l, r):
if l == r:
self.st[v] = arr[l]
/**
* @param {number[]} A
* @return {boolean}
*/
var validMountainArray = function(A) {
if (A.length < 3) {
return false;
}
let i = 0;
let j = 1;
/**
* @param {number[]} nums
* @return {boolean}
*/
var increasingTriplet = function(nums) {
let currentMin = nums[0];
let minWithSmaller = Infinity;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > minWithSmaller) {
/**
* @param {number[]} A
* @return {boolean}
*/
var validMountainArray = function(A) {
if (A.length < 3) {
return false;
}
let current = A[0];
let i = 1;
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
let memo = Array(nums.length);
return helper(nums, 0, memo);
};
var helper = function(nums, current, memo) {
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
return helper(nums, 0);
};
var helper = function(nums, current) {
if (current > nums.length) {
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
var helper = function(current, last, target) {
if (current == target) {
return 0;
} else if (current > target) {
return Infinity;
}
let option1 = last != current ? helper(current, current, target) + 1 : Infinity;
let option2 = last != 0 ? helper(current + last, last, target) + 1 : Infinity;
if (option1 < option2) {