Skip to content

Commit

Permalink
restore javascript files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-A0 committed Aug 21, 2022
1 parent a076cfe commit e80c1c3
Show file tree
Hide file tree
Showing 156 changed files with 6,391 additions and 7 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/updateCompletionTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,24 +508,30 @@ const buildTableColumn = (
}

tableMatrix[0].push(language);
for (const [index, [, problemNumber]] of Object.entries(
Object.values(problems)
)) {
tableMatrix[+index + 1].push(checkbox[problemNumber]);
for (const [index, complete] of Object.entries(Object.values(checkbox))) {
tableMatrix[+index + 1].push(complete);
}
};

const makeMarkdown = (table, urls) => {
return [
table[0]
.map((cell) => `<sub>${FOLDER_TO_LANG[cell] || cell}</sub>`)
.map(
(cell) =>
`<small><small><small>${
FOLDER_TO_LANG[cell] || cell
}</small></small></small>`
)
.join(' | '),
table[0].map(() => '----').join(' | '),
...table.slice(1).map((row, rowIndex) =>
row
.map((cell, index) => {
if (index == 0) return `<sub>[${cell}](${urls[rowIndex]})</sub>`;
return `<sub><div align='center'>${cell ? "✔️" : "❌"}</div></sub>`
if (index == 0)
return `<small><small><small>[${cell}](${urls[rowIndex]})</small></small></small>`;
return cell
? "<small><div align='center'>✔️</div></small>"
: "<small><div align='center'>❌</div></small>";
})
.join(' | ')
),
Expand Down
155 changes: 155 additions & 0 deletions README.md

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions javascript/1-Two-Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
let map = {};
for (let i = 0; i < nums.length; i++) {
if (target - nums[i] in map) {
return [map[target - nums[i]], i];
} else {
map[nums[i]] = i;
}
}
};
37 changes: 37 additions & 0 deletions javascript/10-Regular-Expression-Matching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {string} s
* @param {string} p
* @return {boolean}
*/
var isMatch = function(s, p) {
var lenS = s.length;
var lenP = p.length;
var map = {};

return check(0, 0);

function check(idxS, idxP) {
if (map[idxS + ':' + idxP] !== undefined) {
return map[idxS + ':' + idxP];
}

if (idxS > lenS) {
return false;
}

if (idxS === lenS && idxP === lenP) {
return true;
}

if (p[idxP] === '.' || p[idxP] === s[idxS]) {
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
check(idxS + 1, idxP) || check(idxS, idxP + 2) :
check(idxS + 1, idxP + 1);
} else {
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
check(idxS, idxP + 2) : false;
}

return map[idxS + ':' + idxP];
}
};
18 changes: 18 additions & 0 deletions javascript/100-Same-Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) return true;
if (!p || !q || p.val !== q.val) return false;
return isSameTree(p.right, q.right) && isSameTree(p.left, q.left);
};
32 changes: 32 additions & 0 deletions javascript/102-Binary-Tree-Level-Order-Traversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function (root) {
if (!root) return [];

const result = [];
const queue = [root];

while (queue.length) {
const numNodes = queue.length;
const temp = [];
for (let i = 0; i < numNodes; i++) {
const subtree = queue.shift();
temp.push(subtree.val);
if (subtree.left !== null) queue.push(subtree.left);
if (subtree.right !== null) queue.push(subtree.right);
}
result.push(temp);
}

return result;
};
23 changes: 23 additions & 0 deletions javascript/104-Maximum-Depth-of-Binary-Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = (root) => {
let maxDepth = 0;
let DFS = (node, depth) => {
if (!node) return maxDepth;
if (depth > maxDepth) maxDepth = depth;
DFS(node.right, depth + 1);
DFS(node.left, depth + 1);
};
DFS(root, 1);
return maxDepth;
};
73 changes: 73 additions & 0 deletions javascript/1046-Last-Stone-Weight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Heap {
constructor(stones) {
this.heap = stones;
this.size = stones.length;
this.heapify(0);
}
right(pos) {
return 2 * pos + 2;
}
left(pos) {
return 2 * pos + 1;
}
isleaf(pos) {
if (2 * pos + 1 >= this.size) return true;
return false;
}
swap(a, b) {
let temp = this.heap[a];
this.heap[a] = this.heap[b];
this.heap[b] = temp;
}
fix(pos) {
if (this.isleaf(pos)) return;
let left = this.left(pos);
let right = this.right(pos);
let bigger = left;
if (right < this.size)
bigger = this.heap[left] > this.heap[right] ? left : right;
if (this.heap[pos] < this.heap[bigger]) {
this.swap(pos, bigger);
this.fix(bigger);
}
}
heapify(pos) {
if (this.isleaf(pos)) return;
this.heapify(this.left(pos));
this.heapify(this.right(pos));
this.fix(pos);
}
delete() {
this.swap(0, --this.size);
this.fix(0);
return this.heap[0];
}
insert(val) {
this.size++;
this.heap[this.size - 1] = val;
this.heapify(0);
}
peek() {
return this.heap[0];
}
}
/**
* @param {number[]} stones
* @return {number}
*/
var lastStoneWeight = function (stones) {
//use a max heap to pop off the top 2 stones at each time
//if result is 0 we can do nothing
//if the result is a new weight we can push it back to the heap
const heap = new Heap(stones);
while (heap.size > 1) {
let x = heap.peek();
heap.delete();
let y = heap.peek();
heap.delete();
const res = x - y;
if (res > 0) heap.insert(res);
}
if (heap.size) return heap.peek();
return 0;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function buildTree(preorder, inorder) {
if (!preorder.length || !inorder.length) return null;

let root = new TreeNode(preorder[0]);
let mid = inorder.indexOf(preorder[0]);

root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
return root;
}
35 changes: 35 additions & 0 deletions javascript/11-Container-With-Most-Water.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* https://leetcode.com/problems/container-with-most-water/
* Time O(N) | Space(1)
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let [ left, right, max ] = [ 0, (height.length - 1), 0 ];

while (left < right) {
const [ leftHeight, rightHeight ] = getHeights(height, left, right);
const area = getArea(height, left, right);

max = Math.max(max, area);

const isRightGreater = leftHeight <= rightHeight;
if (isRightGreater) left++;

const isRightLess = rightHeight < leftHeight;
if (isRightLess) right--;
}

return max;
};

const getHeights = (height, left, right) => [ height[left], height[right] ];

const getArea = (height, left, right) => {
const [ leftHeight, rightHeight ] = getHeights(height, left, right);
const _height = Math.min(leftHeight, rightHeight);
const width = right - left;

return _height * width;
};

31 changes: 31 additions & 0 deletions javascript/110-Balanced-Binary-Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function (root) {
const getHeight = (root) => {
if (!root) return [-1, true];

const [leftHeight, leftBalanced] = getHeight(root.left);
const [rightHeight, rightBalanced] = getHeight(root.right);

const balanced =
leftBalanced &&
rightBalanced &&
Math.abs(leftHeight - rightHeight) < 2;

return [1 + Math.max(leftHeight, rightHeight), balanced];
};

const balanced = getHeight(root)[1];

return balanced;
};
16 changes: 16 additions & 0 deletions javascript/1143-Longest-Common-Subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var longestCommonSubsequence = function (text1, text2) {
let m = text1.length,
n = text2.length,
DP = new Array(m + 1).fill(0).map((_) => new Array(n + 1).fill(0));

for (let x = m - 1; x >= 0; x--)
for (let y = n - 1; y >= 0; y--) {
if (text1[x] === text2[y]) {
DP[x][y] = 1 + DP[x + 1][y + 1];
} else {
DP[x][y] = Math.max(DP[x + 1][y], DP[x][y + 1]);
}
}

return DP[0][0];
};
Loading

0 comments on commit e80c1c3

Please sign in to comment.