Skip to content

Instantly share code, notes, and snippets.

View revanth0212's full-sized avatar

Revanth Kumar Annavarapu revanth0212

View GitHub Profile
@revanth0212
revanth0212 / LowestCommonAncestor.js
Created June 24, 2018 18:32
Find the lowest common ancestor of 2 nodes in a BST.
class Relationship {
constructor(root) {
this.relObj = {}
this.iterate(root, null, 0)
}
iterate(root, parent, level) {
if (root) {
this.relObj[root.val] = { level: level, parent: parent }
this.iterate(root.left, root, level + 1)
@revanth0212
revanth0212 / InorderSuccessor.js
Created June 24, 2018 00:40
Find the In-Order Successor of a node in a BST.
class Stack {
constructor(root) {
this.stack = []
while (root) {
this.stack.push(root)
root = root.left
}
}
hasNext() {
@revanth0212
revanth0212 / NumberOfWaysToClimbNStairs.js
Created June 23, 2018 16:09
Find the number of ways to climb N number of stairs. (DP)
const hashMap = {}
function countNumberOfWays (steps) {
if (steps <= 0) {
return 0
} else if (steps === 1) {
return 1
} else if (steps === 2) {
return 2
} else {
@revanth0212
revanth0212 / LengthOfLongestSubStringWithoutRepeatingChars.js
Created April 22, 2018 01:00
Length of longest sub-string in a string without repeating chars.
function findLengthOfLongestSubString(str) {
var start = end = lengthOfLongestSubString = 0
var hashMap = {}
while(end < str.length) {
if (str[end] in hashMap) {
start = hashMap[str[end]] + 1
} else {
lengthOfLongestSubString = Math.max(lengthOfLongestSubString, (end-start+1))
}
hashMap[str[end]] = end
@revanth0212
revanth0212 / LengthOfLCSS.js
Last active April 22, 2018 00:59
Length of Longest Common Substring
function initializeMap(i, j) {
var result = new Array(i)
for (var x = 0; x < i; x++) {
var arr = new Array(j)
arr.fill(null)
result[x] = arr
}
return result
}
@revanth0212
revanth0212 / ProductExceptSelf.js
Created April 21, 2018 19:48
Find product of all the elements in the array except self. (without division)
var leftArray = rightArray = prod = []
var leftHashMap = rightHashMap = {}
function constructLeftArrayElement (arr, i) {
if (i <= 0) {
leftHashMap[i] = 1
return 1
} else {
if (leftHashMap.hasOwnProperty(i-1)) {
@revanth0212
revanth0212 / QuickSelect.js
Last active April 21, 2018 19:46
Quick Select. Also can be termed as Finding Kth Smallest Element in Unsorted array with no duplicates.
function swap(arr, i, j) {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
return arr
}
function partition(arr, start, end) {
var pivot = end
while(start <= end) {
@revanth0212
revanth0212 / QuickSort.js
Created April 21, 2018 19:41
Quick Sort
function swap(arr, i, j) {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
return arr
}
function partition(arr, start, end) {
var pivot = end
while(start <= end) {
@revanth0212
revanth0212 / FindKthLargest.js
Last active April 21, 2018 19:43
Find Kth Largest Element in an Unsorted array with no duplicates.
function swap(arr, i, j) {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
return arr
}
function partition(arr, start, end) {
var pivot = end
while(start <= end) {