Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 96 You must be signed in to fork a gist
  • Save tapash-almabetter/00f35aefdaf2c7ee1c39700531c925bb to your computer and use it in GitHub Desktop.
Save tapash-almabetter/00f35aefdaf2c7ee1c39700531c925bb to your computer and use it in GitHub Desktop.
End Course Capstone Project - Module 2

End Course Capstone Project (Module 2)

Capstone Project Submission Instructions

  1. Login/Sign Up to your GitHub account.
  2. Fork this Gist from the Top-Right corner.
  3. Edit your Gist and paste/write the code solutions inside the respective functions.
  4. Share your Gist link in the submission form as per the instructions given.

- Do not make any changes in the first 3 lines

End.Course.Capstone.Project.Submission.Video.mp4
module.exports = { isPowerOfTwo };
function isPowerOfTwo(n) {
return n > 0 || (n & (n - 1)) === 0;
}
module.exports = { minAddToMakeValid };
function minAddToMakeValid(s) {
let open = 0, close = 0;
for(let c of s) {
if(c === '(') open++;
else if(open) close++;
else open--;
}
return open + close;
};
module.exports = { callPoints };
function callPoints(operations) {
for (let i = 0; i <= operations.length; i++) {
if (operations[i] === "+") {
sum = operations[i - 2] + operations[i - 1];
operations[i] = sum;
} else if (operations[i] === "D") {
newD = operations[i - 1] * 2;
operations[i] = newD;
} else if (operations[i] === "C") {
operations.splice(i - 1, 2);
i = i - 2;
} else {
var integer = +operations[i];
operations[i] = integer;
}
}
// to calculate the totalSum only
let totalSum = 0;
for (let i = 0; i <= operations.length; i++) {
totalSum += operations[i];
}
return totalSum;
};
module.exports = { sortPeople };
function sortPeople(names, heights) {
let length = heights.length();
let map = new Map();
for(let i=0; i<=length; i++){
map.set(heights[i], names[i]);
}
heights.sort((a,b) => b-a);
let res = [];
for(let height of heights){
res.push(map.get(height));
}
return res;
};
module.exports = { findErrorNums };
function findErrorNums(nums) {
const hashmap = new Map();
const output = [];
let maxValue = 0;
for (let i = 0; i <= nums.length; i++) {
const num = nums[i];
if (hashmap.has(num)) output.push(num);
hashmap.set(num, i);
maxValue = Math.max(maxValue, num);
}
for (let i = 1; i < maxValue + 1; i++) {
if (hashmap.has(i)) {
output.push(i);
break;
}
}
return output;
};
module.exports = { isHappy };
function isHappy(n) {
// Helper function to calculate the sum of squares of digits
function calculateSumOfSquares(num) {
let sum = 0;
while (num > 0) {
const digit = num % 10;
sum += digit * digit;
num = Math.floor(num / 10);
}
return sum;
}
let slow = n;
let fast = n;
do {
slow = calculateSumOfSquares(slow);
fast = calculateSumOfSquares(calculateSumOfSquares(fast));
} while (slow !== fast);
return slow === 1;
}
module.exports = { isPalindrome };
function isPalindrome(n) {
let dupNum = 0;
let backN = n;
while (n) {
let rem = n % 10;
dupNum = dupNum * 10 + rem;
n = Math.floor(n / 10);
}
if (dupNum === backN) {
return true;
}
return false;
}
module.exports = { fairCandySwap };
function fairCandySwap(aliceSizes, bobSizes) {
let sumAlice = 0, sumBob = 0;
for(let i = 0; i < aliceSizes.length; i++) {
sumAlice += aliceSizes[i];
}
for(let i = 0; i < bobSizes.length; i++) {
sumBob += bobSizes[i];
}
let sum = (sumAlice + sumBob) / 2;
for(let i = 0; i < aliceSizes.length; i++) {
let a = aliceSizes[i];
let b = sum - (sumAlice - a);
if(bobSizes.includes(b))
return [a, b];
}
};
module.exports = { nextGreatestLetter };
function nextGreatestLetter(letters, target) {
// binary search
let ans;
let check = false;
let l = 0;
let size = letters.length;
let h = size - 1;
while (l <= h) {
let mid = h - Math.floor((h - l) / 2);
if (letters[mid] <= target) {
l = mid + 1;
} else {
let currAns = letters[mid];
check = true;
if (mid >= 0) {
h = mid - 1;
}
ans = currAns;
}
}
if (!check) {
return letters[0];
}
return ans;
}
module.exports = { searchRange };
function searchRange(nums, target) {
//O(n) approach
var ans=[];
for(let i=0;i<nums.length;i++){
if(nums[i]==target){
ans.push(i);
break;
}
}
for(let i=nums.length-1;i>=0;i--){
if(nums[i]==target){
ans.push(i);
break;
}
}
if(ans.length==0){
ans.push(-1);
ans.push(-1);
}
return ans;
};
module.exports = { findPeakElement };
function findPeakElement(nums) {
// Write your code inside this function only.
}
module.exports = { sortColors };
function sortColors(nums) {
// Write your code inside this function only.
}
module.exports = { maxCount };
function maxCount(nums) {
// Write your code inside this function only.
}
module.exports = { minimumSum };
function minimumSum(num) {
// Write your code inside this function only.
}
module.exports = { transitionPoint };
function transitionPoint(arr) {
// Write your code inside this function only.
}
// Traditional Approach
var missingNumber = function(arr) {
let n=arr.length;
let hashSet = new Set();
// Add all elements of array to hashset
for (let i = 0; i < n ; i++) {
hashSet.add(arr[i]);
}
// Check each integer from 1 to n
for (let i = 1; i <= n; i++) {
// If integer is not in hashset, it is the missing integer
if (!hashSet.has(i)) {
return i;
}
}
// If no integer is missing, return n+1
return 0;
};
// Optimized Approach
var missingNumber = function(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
while (arr[i] > 0 && arr[i] <= n && arr[arr[i] - 1] !== arr[i]) {
[arr[arr[i] - 1], arr[i]] = [arr[i], arr[arr[i] - 1]];
}
}
for (let i = 0; i < n; i++) {
if (arr[i] !== i + 1) {
return i + 1;
}
}
return n + 1;
};
// Provide your comparison here.
// Traditional Approach
class Solution{
duplicateCheck(str){
//code here
let temp = ''+ str[0];
for(let i=0;i<str.length;i++){
if(temp.indexOf(str[i]) == -1){
temp = temp+str[i];
}
}
return temp;
}
}
// Optimized Approach
class Solution{
duplicateCheck(str){
const p=new Set(str);
const t=[...p];
return t.join("");
}
}
// Provide your comparison here.
// Traditional Approach
function chunkArrayInGroups(arr, size) {
let temp = [];
const result = [];
for (let a = 0; a < arr.length; a++) {
if (a % size !== size - 1) temp.push(arr[a]);
else {
temp.push(arr[a]);
result.push(temp);
temp = [];
}
}
if (temp.length !== 0) result.push(temp);
return result;
}
// Optimized Approach
function chunkArrayInGroups(arr, size) {
// Break it up.
const newArr = [];
let i = 0;
while (i < arr.length) {
newArr.push(arr.slice(i, i + size));
i += size;
}
return newArr;
}
// Provide your comparison here.
// Traditional Approach
function enchantedArrayShuffling(originalArray) {
const sortedArray = [...originalArray].sort((a, b) => a - b);
const shuffledArray = [];
let left = 0;
let right = sortedArray.length - 1;
while (left <= right) {
if (left === right) {
shuffledArray.push(sortedArray[left]);
} else {
shuffledArray.push(sortedArray[left]);
shuffledArray.push(sortedArray[right]);
}
left++;
right--;
}
return shuffledArray;
}
// Optimized Approach
function enchantedArrayShuffling(originalArray) {
const sortedArray = [...originalArray].sort((a, b) => a - b);
const shuffledArray = [];
let left = 0;
let right = sortedArray.length - 1;
while (left < right) {
shuffledArray.push(sortedArray[left++]);
shuffledArray.push(sortedArray[right--]);
}
if (left === right) {
shuffledArray.push(sortedArray[left]);
}
return shuffledArray;
}
// Provide your comparison here.
// Traditional Approach
function findPairWithSum(array, targetSum) {
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === targetSum) {
return [array[i], array[j]];
}
}
}
return [];
}
// Optimized Approach
function findPairWithSum(array, targetSum) {
const numMap = {};
for (let i = 0; i < array.length; i++) {
const difference = targetSum - array[i];
if (numMap[array[i]]) {
return [difference, array[i]];
}
numMap[difference] = true;
}
return [];
}
// Provide your comparison here.
// Worst Case
function worstmaxBitwise(arr) {
// Write your code inside this function only.
}
// Best Case
function bestmaxBitwise(arr) {
// Write your code inside this function only.
}
// Worst Case
function worstflipAndInvertImage(image) {
// Write your code inside this function only.
}
// Best Case
function bestflipAndInvertImage(image) {
// Write your code inside this function only.
}
// Worst Case
function worstasteroidCollision(arr) {
// Write your code inside this function only.
}
// Best Case
function bestasteroidCollision(arr) {
// Write your code inside this function only.
}
// Worst Case
function worstfindRelativeRanks(score) {
// Write your code inside this function only.
}
// Best Case
function bestfindRelativeRanks(score) {
// Write your code inside this function only.
}
// Worst Case
function lengthOfLastWord(s) {
// Write your code inside this function only.
}
// Best Case
function lengthOfLastWord(s) {
// Write your code inside this function only.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment