Skip to content

Laptop #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 247 additions & 0 deletions TreeMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// https://github.com/somdipdey/JavaScript-implementation-of-java.util.TreeMap-Class/blob/master/treeMap.js
module.exports = class TreeMap {
constructor(){
this.dict = {}
}

/*
* <summary>
* Value get(Object key)
* This method returns the value to which the specified key is mapped,
* or null if this map contains no mapping for the key.
* </summary>
*/
get(key) {
return this.dict[key];
};

/*
* <summary>
* boolean containsKey(Object key)
* This method returns true
* if this map contains a mapping for the specified key.
* </summary>
*/
containsKey(key) {
if( this.get(key) !== undefined) {
return true;
} else {
return false;
}
};

/*
* <summary>
* Value put(K key, V value)
* This method associates the specified value with the specified key in this map.
* </summary>
*/
put(key, value) {
this.dict[key] = value;
if(isNumber(key))
{
if(allKeysAreNumeral(this.dict)) {
this.dict = sortOnKeys(this.dict);
}
}
};

/*
* <summary>
* Value remove(Object key)
* This method removes the mapping for this key from this TreeMap if present.
* </summary>
*/
remove(key) {
'use strict';
delete this.dict[key];
};

/*
* <summary>
* void clear()
* This method removes all of the mappings from this map.
* </summary>
*/
clear() {
this.dict = {};
};


/*
* <summary>
* treeMap.foreach(V value)
* This method returns each value for each keys in the TreeMap.
* </summary>
*/
forEach(callback){
var len = this.size();
for (i = 0; i < len; i++) {
var item = this.get( Object.keys(this.dict)[i] );
callback(item);
}
}


/*
* <summary>
* int size()
* This method returns the number of key-value mappings in this map.
* </summary>
*/
size() {
return Object.keys(this.dict).length;
};

/*
* <summary>
* boolean isEmpty()
* This method returns a boolean
* determining whether the TreeMap is empty or not.
* </summary>
*/
isEmpty() {
return Object.keys(this.dict).length == 0;
};


/*
* <summary>
* Key floorKey(K key)
* This method returns the greatest key less than or equal
* to the given key, or null if there is no such key.
* </summary>
*/
floorKey(key) {
if(!isNumber(key))
throw "Invalid Operation: key has to be an integer value";

if(this.containsKey(key))
return this.get(key);

return this.floorKey(key - 1);
}


/*
* <summary>
* Key ceilingKey(K key)
* This method returns the least key greater than or equal
* to the given key, or null if there is no such key.
* </summary>
*/
ceilingKey(key) {
if(!isNumber(key))
throw "Invalid Operation: key has to be an integer value";

if(this.containsKey(key))
return this.get(key);

return this.floorKey(key + 1);
}


/*
* <summary>
* Object clone()
* This method returns a shallow copy of this TreeMap instance.
* </summary>
*/
clone(){
return this.dict;
}


/*
* <summary>
* boolean containsValue(Object value)
* This method returns true if this map maps one or more keys to the specified value.
* </summary>
*/
containsValue(value){
var len = this.size();
for (i = 0; i < len; i++) {
var item = this.get( Object.keys(this.dict)[i] );
if(value === item)
return true;
}

return false;
}


/*
* <summary>
* Set<K> keySet()
* This method returns a Set view of the keys contained in this map.
* </summary>
*/
keySet(){
var set = [];
var len = this.size();
for (i = 0; i < len; i++) {
set.push(Object.keys(this.dict)[i]);
}

return set;
}


/*
* <summary>
* Key firstKey()
* This method returns the first (lowest) key currently in this map.
* </summary>
*/
firstKey(){
return Object.keys(this.dict)[0];
}


/*
* <summary>
* Key lastKey()
* This method returns the last (highest) key currently in this map.
* </summary>
*/
lastKey(){
var len = this.size();
return Object.keys(this.dict)[len - 1];
}

}

// some more functions which might be required to complete the operations of TreeMap -->

// Checks if the input is a number or not
function isNumber( input ) {
return !isNaN( input );
}

// Sorts a JavaScript dictionary by key
function sortOnKeys(dict) {

var sorted = [];
for(var key in dict) {
sorted[sorted.length] = key;
}
sorted.sort();

var tempDict = {};
for(var i = 0; i < sorted.length; i++) {
tempDict[sorted[i]] = dict[sorted[i]];
}

return tempDict;
}

// Checks if all the keys in the JavaScript dictionary are numeral.
// If Yes, then it returns true or else it returns false
function allKeysAreNumeral(dict) {
for(var key in dict) {
if(!isNumber(key))
return false;
}

return true;
}
19 changes: 18 additions & 1 deletion leetcode/dataStructures/pacificAtlanticWaterFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,22 @@ const grid2 = [
[1, 1]
]

const grid3 = [
[19,16,16,12,14,0,17,11,2,0,18,9,13,16,8,8,8,13,17,9,16,9,4,7,1,19,10,7,0,15],
[0,11,4,14,9,0,6,13,16,5,19,9,4,5,4,12,0,13,0,7,9,12,13,15,3,7,4,9,15,1],
[13,14,12,12,12,16,6,15,13,1,8,9,11,14,14,10,19,11,10,0,5,18,4,12,7,13,17,15,18,1],
[16,14,19,5,8,2,11,17,7,1,4,6,5,18,7,15,6,19,18,12,1,14,2,2,0,9,15,14,13,19],
[17,4,12,9,12,10,12,10,4,5,12,7,2,12,18,10,10,8,6,1,5,13,10,3,5,3,11,4,8,11],
[8,19,18,9,6,2,7,3,19,6,0,17,9,12,11,1,15,11,18,1,8,11,1,11,16,7,8,17,15,0],
[7,0,5,11,1,7,12,18,12,1,5,2,11,7,18,12,0,11,9,18,5,2,3,1,1,1,8,14,19,5],
[2,14,2,16,17,19,10,16,1,16,16,3,19,12,13,17,19,12,16,10,16,8,16,12,6,12,13,17,9,12],
[8,1,10,5,7,0,15,19,8,15,4,12,18,18,13,11,5,2,8,3,15,4,3,7,7,14,15,11,6,16],
[0,5,13,19,1,1,2,4,16,2,16,9,15,15,10,10,18,11,17,1,5,14,5,19,7,0,13,7,13,7],
[11,6,16,12,4,2,9,11,17,19,12,10,6,16,17,5,1,18,19,7,15,1,14,0,3,19,7,3,4,13],
[4,11,8,10,10,19,7,18,4,2,2,14,6,9,18,14,2,16,5,3,19,17,4,3,7,1,12,2,4,3],
[14,16,3,11,13,13,6,16,18,0,17,19,4,1,14,12,4,17,5,19,8,13,15,3,15,4,1,14,12,10],
[13,2,12,2,16,12,19,10,19,12,19,14,12,17,16,3,13,7,3,15,16,7,10,15,14,10,6,5,2,18]
]
console.log(pacificAtlantic(grid1)) // [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]]
console.log(pacificAtlantic(grid2)) // [[0,0],[0,1],[1,0],[1,1],[2,0],[2,1]]
console.log(pacificAtlantic(grid2)) // [[0,0],[0,1],[1,0],[1,1],[2,0],[2,1]]
console.log(pacificAtlantic(grid3)) // [[0,29],[1,28],[2,28],[12,0],[12,1],[13,0]]
63 changes: 63 additions & 0 deletions leetcode/non-linear-data-structures/invertBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class BST {
constructor(val) {
this.left = null;
this.right = null;
this.val = val
}

insert(val) {
const newNode = new BST(val);

let current = this;
while (true) {
if (val < current.val) {
if (current.left === null) {
current.left = newNode;
break;
} else {
current = current.left
}
} else {
if (current.right === null) {
current.right = newNode;
break;
} else {
current = current.right;
}
}
}
return this;
}
}

const tree1 = new BST(4).insert(2).insert(7).insert(1).insert(3).insert(6).insert(9);

// O(n) time: have to visit all the nodes | O(h) space: where h is height. height could be n so it is actually O(n)
function invertBST(head) {
if (!head) return null;

const right = invertBST(head.right);
const left = invertBST(head.left);
head.left = right;
head.right = left;
return head;
}

console.log('recursive -> ',invertBST(tree1));

// O(n) time | O(n) space
function invertBSTIterative(head) {
if (!head) return null;
const stack = [head];
while (stack.length) {
const current = stack.pop();
const temp = current.left;
current.left = current.right;
current.right = temp;
if (current.left !== null) stack.push(current.left);
if (current.right !== null) stack.push(current.right);
}
return head;
}

console.log('iterative -> ', invertBSTIterative(tree1));
Loading