From 11f1355d8595bd06e77bca9a726c5b270473b15e Mon Sep 17 00:00:00 2001 From: Becky McCarthy Date: Mon, 24 Aug 2020 16:30:25 -0700 Subject: [PATCH 1/4] Added algorithms page with Get the mean of an array --- becky/algorithms.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 becky/algorithms.md diff --git a/becky/algorithms.md b/becky/algorithms.md new file mode 100644 index 00000000..0186588e --- /dev/null +++ b/becky/algorithms.md @@ -0,0 +1,18 @@ +### Code Wars + +#### Get the mean of an array + +It's the academic year's end, fateful moment of your school report. The averages must be calculated. All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script. + +Return the average of the given array rounded down to its nearest integer. + +The array will never be empty. + + +```function getAverage(marks){ + let sum = 0; + for (let i = 0; i< marks.length; i++) { + sum += marks[i]; + } + return Math.floor(sum / marks.length); +}``` \ No newline at end of file From 61ecca19e8d09547366452f54dde2501c33f8186 Mon Sep 17 00:00:00 2001 From: Becky McCarthy Date: Tue, 25 Aug 2020 15:23:15 -0700 Subject: [PATCH 2/4] add Smallest unused ID, Keep Hydrated, Simple directions reversal, Multiplication table --- .DS_Store | Bin 6148 -> 6148 bytes becky/algorithms.md | 111 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 4 deletions(-) diff --git a/.DS_Store b/.DS_Store index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0fecd7903f404a7055f475393a1f2c06f5adb360 100644 GIT binary patch delta 358 zcmZoMXfc=|#>B)qF;Q%yo}w@_0|Nsi1A_nqLuFEVaY0f}e$vLom5lWuaaM*ThE#@R zhHQi!Q1oO{K~83IiGjg&MkZz!RyKAH4lWKZUar{SjQsN8lEjkIVyDESXb>+Tu_Pl2 z$_~lT&w;ZOlfp7n%i{$^ob&Ta5;OBsi@=&QQ&NFSV!|`?>QnN|o$^cbQi{O_1w&*w zI5;^t;{_zDs|_s-brcNE4Qh21s?E&}bQDaC&1!2oImA^BZ9NlmE32w&YU^eKT?+(^ zK>vXOKa_@1vw#dFKc*BX=Oh7L2~sYOBwiL=l$VpAmktzW+?e>BWwLAjHu~2NHo+1YW5HK<@2y9kle#WxdfcX^DW_AvK4xj>{$am(+{342+ UKzW7)kiy9(Jj$D6L{=~Z02rPQkN^Mx diff --git a/becky/algorithms.md b/becky/algorithms.md index 0186588e..2de697c6 100644 --- a/becky/algorithms.md +++ b/becky/algorithms.md @@ -1,6 +1,5 @@ ### Code Wars - -#### Get the mean of an array +### Get the mean of an array It's the academic year's end, fateful moment of your school report. The averages must be calculated. All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script. @@ -9,10 +8,114 @@ Return the average of the given array rounded down to its nearest integer. The array will never be empty. -```function getAverage(marks){ +``` +function getAverage(marks){ let sum = 0; for (let i = 0; i< marks.length; i++) { sum += marks[i]; } return Math.floor(sum / marks.length); -}``` \ No newline at end of file +} +``` + + +### Code Wars +### Smallest unused ID + +You've got much data to manage and of course you use zero-based and non-negative ID's to make each data item unique! + +Therefore you need a method, which returns the smallest unused ID for your next new data item... + +Note: The given array of used IDs may be unsorted. For test reasons there may be duplicate IDs, but you don't have to find or remove them! + +``` +function nextId(ids){ + const arr = new Set(ids); + for (let i = 0; i <= ids.length; i++) { + if (!arr.has(i)) { + return i; + } + } +} +``` + + +### Code Wars +### Keep Hydrated! + +Nathan loves cycling. + +Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling. + +You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value. + +For example: + +time = 3 ----> litres = 1 + +time = 6.7---> litres = 3 + +time = 11.8--> litres = 5 + +``` +function litres(time) { + return Math.floor(time * 0.5); +} +``` + + +### Code Wars +### Simple directions reversal + +In this Kata, you will be given directions and your task will be to find your way back. + +``` +solve(["Begin on Road A","Right on Road B","Right on Road C","Left on Road D"]) = ['Begin on Road D', 'Right on Road C', 'Left on Road B', 'Left on Road A'] +solve(['Begin on Lua Pkwy', 'Right on Sixth Alley', 'Right on 1st Cr']) = ['Begin on 1st Cr', 'Left on Sixth Alley', 'Left on Lua Pkwy'] +``` + +### my solution +``` +function solve(arr){ + let res = []; + let a = []; + for (let i of arr) + a.push(i.split(' ')); + res.push('Begin ' + a.slice(-1).pop().slice(1).join(' ')); + for (let i = a.length-1; i > 0; i--) { + a[i][0] === 'Right' ? res.push('Left ' + a[i-1].slice(1).join(' ')) : + res.push('Right ' + a[i-1].slice(1).join(' ')); + } + return res; +} +``` + +### Code Wars +### Multiplication table + +Your task, is to create NxN multiplication table, of size provided in parameter. + +for example, when given size is 3: +``` +1 2 3 +2 4 6 +3 6 9 +``` +for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]] + +### my solution +``` +multiplicationTable = function(size) { + var arr = [] + for (var i = 1; i <= size; i++ ) { + var result = []; + for (var j = 1; j <= size; j++) { + result.push(j*i); + } + arr.push(result); + } + return arr; +} +``` + + From 80aebbca92c00eba8ed1a9c29721393b2631870b Mon Sep 17 00:00:00 2001 From: Becky McCarthy Date: Wed, 26 Aug 2020 09:20:50 -0700 Subject: [PATCH 3/4] Min stack - leetcode --- becky/leet-code.md | 87 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 becky/leet-code.md diff --git a/becky/leet-code.md b/becky/leet-code.md new file mode 100644 index 00000000..7c85dfa3 --- /dev/null +++ b/becky/leet-code.md @@ -0,0 +1,87 @@ +### 155. Min Stack + +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. + +push(x) -- Push element x onto stack. +pop() -- Removes the element on top of the stack. +top() -- Get the top element. +getMin() -- Retrieve the minimum element in the stack. + +Example 1: +``` +Input +["MinStack","push","push","push","getMin","pop","top","getMin"] +[[],[-2],[0],[-3],[],[],[],[]] + +Output +[null,null,null,null,-3,null,0,-2] + +Explanation +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); // return -3 +minStack.pop(); +minStack.top(); // return 0 +minStack.getMin(); // return -2 +``` +Constraints: + +Methods pop, top and getMin operations will always be called on non-empty stacks. + +### my solution +``` +/** + * initialize your data structure here. + */ +var MinStack = function() { + this.stack = []; + // this.newMin = null; +}; + +/** + * @param {number} x + * @return {void} + */ +MinStack.prototype.push = function(x) { + this.stack.push(x); + // if (x < this.stack.length -2) { + // this.newMin = x; + // } +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function() { + this.stack.pop(); +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function() { + return this.stack[this.stack.length -1]; +}; + +/** + * @return {number} + */ +MinStack.prototype.getMin = function() { + return Math.min(...this.stack); + // return this.newMin; +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(x) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.getMin() + */ + ``` + ### my comments: + + __The commented out parts were attempt at running constant time. It worked but when pop() was run, it became inaccurate with out sorting within the pop() function which makes it O(n) instead of constant time. In order to do what the instructions say, there would have to be a trade off.__ \ No newline at end of file From 868b2de0c1031770126ede9deb2afa049deb0188 Mon Sep 17 00:00:00 2001 From: Becky McCarthy Date: Thu, 27 Aug 2020 12:36:59 -0700 Subject: [PATCH 4/4] binary tree leet code stuff --- becky/leet-code-Binary-Tree-stuff.md | 117 ++++++++++++++++++ .../{leet-code.md => leet-code-Min-stack.md} | 0 2 files changed, 117 insertions(+) create mode 100644 becky/leet-code-Binary-Tree-stuff.md rename becky/{leet-code.md => leet-code-Min-stack.md} (100%) diff --git a/becky/leet-code-Binary-Tree-stuff.md b/becky/leet-code-Binary-Tree-stuff.md new file mode 100644 index 00000000..e3c5a62c --- /dev/null +++ b/becky/leet-code-Binary-Tree-stuff.md @@ -0,0 +1,117 @@ +### Leet Code +### 226. Invert Binary Tree + +Invert a binary tree. + +Example: + +Input: +``` + 4 + / \ + 2 7 + / \ / \ +1 3 6 9 +``` +Output: +``` + 4 + / \ + 7 2 + / \ / \ +9 6 3 1 +``` +### my solution: +``` +/** + * 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 {TreeNode} + */ +var invertTree = function(root) { + // check if root null + var temp; + if (root === null) { + return null; + } + // swap root.left with root.right + temp = root.left; + root.left = root.right; + root.right = temp; + // invoke recursion by calling invertTree function + // on root.left and root.right + invertTree(root.left); + invertTree(root.right); + + return root +}; +``` + +### 617. Merge Two Binary Trees + +Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. + +You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. + +Example 1: +``` +Input: + Tree 1 Tree 2 + 1 2 + / \ / \ + 3 2 1 3 + / \ \ + 5 4 7 + +Output: +Merged tree: + 3 + / \ + 4 5 + / \ \ + 5 4 7 +``` +Note: The merging process must start from the root nodes of both trees. + +### my solution: +``` +/** + * 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} t1 + * @param {TreeNode} t2 + * @return {TreeNode} + */ +var mergeTrees = function(t1, t2) { + if (t1 === null && t2 === null) { + return null; + } + if (t1 === null) { + return t2; + } + if (t2 === null) { + return t1; + } else { + var t3Val = t1.val + t2.val; + } + var t3Left = mergeTrees(t1.left, t2.left); + var t3Right = mergeTrees(t1.right, t2.right); + return new TreeNode(t3Val, t3Left, t3Right); +}; +``` + + + diff --git a/becky/leet-code.md b/becky/leet-code-Min-stack.md similarity index 100% rename from becky/leet-code.md rename to becky/leet-code-Min-stack.md