From cd47959d9eb24d8f4e48fd4a38f49ed08dde9e68 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Sat, 4 May 2024 19:34:16 -0400 Subject: [PATCH 1/2] Add week 1 solutions --- best-time-to-buy-and-sell-stock/yolophg.js | 16 ++++++++++++++++ contains-duplicate/yolophg.js | 15 +++++++++++++++ two-sum/yolophg.js | 16 ++++++++++++++++ valid-anagram/yolophg.js | 7 +++++++ valid-palindrome/yolophg.js | 19 +++++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/yolophg.js create mode 100644 contains-duplicate/yolophg.js create mode 100644 two-sum/yolophg.js create mode 100644 valid-anagram/yolophg.js create mode 100644 valid-palindrome/yolophg.js diff --git a/best-time-to-buy-and-sell-stock/yolophg.js b/best-time-to-buy-and-sell-stock/yolophg.js new file mode 100644 index 000000000..b3854252f --- /dev/null +++ b/best-time-to-buy-and-sell-stock/yolophg.js @@ -0,0 +1,16 @@ +var maxProfit = function(prices) { + let lowBuy = prices[0]; + let profit = 0; + // loop over the array + for(let i = 0; i < prices.length; i++) { + // to set lowBuy to adjust as lower numbers + if ( prices[i] < lowBuy ) { + lowBuy = prices[i]; + } + // check the subtract current iteration on array - lowBuy price to calculate current profit + if ( prices[i] - lowBuy > profit) { + profit = prices[i] - lowBuy ; + } + } + return profit; +}; \ No newline at end of file diff --git a/contains-duplicate/yolophg.js b/contains-duplicate/yolophg.js new file mode 100644 index 000000000..978ca68d7 --- /dev/null +++ b/contains-duplicate/yolophg.js @@ -0,0 +1,15 @@ +var containsDuplicate = function(nums) { + let set = new Set([nums[0]]); + + // check if the current value is in the set + for(let i = 1; i < nums.length; i++){ + // if it's already in the set, return true + if(set.has(nums[i])){ + return true; + // if it's not in the set, add it to set and resume + } else { + set.add(nums[i]); + } + } + return false; +}; \ No newline at end of file diff --git a/two-sum/yolophg.js b/two-sum/yolophg.js new file mode 100644 index 000000000..a1753aa2a --- /dev/null +++ b/two-sum/yolophg.js @@ -0,0 +1,16 @@ +var twoSum = function(nums, target) { + const hash = new Map(); + + for( let i = 0 ; i < nums.length; i++) { + // find num to make a target + const findTarget = target - nums[i]; + + // if find num, return + if(hash.get(findTarget) !== undefined) return [hash.get(findTarget), i]; + + // if couldn't find, set in the map + hash.set(nums[i], i); + } + + return []; +}; \ No newline at end of file diff --git a/valid-anagram/yolophg.js b/valid-anagram/yolophg.js new file mode 100644 index 000000000..bbf1d3fa8 --- /dev/null +++ b/valid-anagram/yolophg.js @@ -0,0 +1,7 @@ +var isAnagram = function(s, t) { + firstList = s.split("").sort(); + secondList = t.split("").sort(); + + // compare if these are same and return + return JSON.stringify(firstList) === JSON.stringify(secondList); +}; \ No newline at end of file diff --git a/valid-palindrome/yolophg.js b/valid-palindrome/yolophg.js new file mode 100644 index 000000000..a5cc8cfdb --- /dev/null +++ b/valid-palindrome/yolophg.js @@ -0,0 +1,19 @@ +var isPalindrome = function(s) { + let regex = /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"" ]/g; + let convertToLowerCase = s.replace(regex, '').toLowerCase(); + let middleOftheString = parseInt(convertToLowerCase.length / 2); + let result; + + // iterate over each value of string till to reache the middle of the string + for (let i = 0; i <= middleOftheString; i++) { + // check if values match + if (convertToLowerCase[i] === convertToLowerCase[convertToLowerCase.length - 1 - i]) { + result = true; + // if there's nothing else to check for, return false and break the loop + } else { + result = false; + break; + } + } + return result; +}; \ No newline at end of file From 230d528c262135f591f78758b2a1064fcb047292 Mon Sep 17 00:00:00 2001 From: hwigyeompark Date: Sat, 4 May 2024 20:48:49 -0400 Subject: [PATCH 2/2] Update line break --- best-time-to-buy-and-sell-stock/yolophg.js | 2 +- contains-duplicate/yolophg.js | 2 +- two-sum/yolophg.js | 2 +- valid-anagram/yolophg.js | 2 +- valid-palindrome/yolophg.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/yolophg.js b/best-time-to-buy-and-sell-stock/yolophg.js index b3854252f..d92ab7c26 100644 --- a/best-time-to-buy-and-sell-stock/yolophg.js +++ b/best-time-to-buy-and-sell-stock/yolophg.js @@ -13,4 +13,4 @@ var maxProfit = function(prices) { } } return profit; -}; \ No newline at end of file +}; diff --git a/contains-duplicate/yolophg.js b/contains-duplicate/yolophg.js index 978ca68d7..5a8f2de1e 100644 --- a/contains-duplicate/yolophg.js +++ b/contains-duplicate/yolophg.js @@ -12,4 +12,4 @@ var containsDuplicate = function(nums) { } } return false; -}; \ No newline at end of file +}; diff --git a/two-sum/yolophg.js b/two-sum/yolophg.js index a1753aa2a..bf50f4111 100644 --- a/two-sum/yolophg.js +++ b/two-sum/yolophg.js @@ -13,4 +13,4 @@ var twoSum = function(nums, target) { } return []; -}; \ No newline at end of file +}; diff --git a/valid-anagram/yolophg.js b/valid-anagram/yolophg.js index bbf1d3fa8..c402689ea 100644 --- a/valid-anagram/yolophg.js +++ b/valid-anagram/yolophg.js @@ -4,4 +4,4 @@ var isAnagram = function(s, t) { // compare if these are same and return return JSON.stringify(firstList) === JSON.stringify(secondList); -}; \ No newline at end of file +}; diff --git a/valid-palindrome/yolophg.js b/valid-palindrome/yolophg.js index a5cc8cfdb..6688e1b8f 100644 --- a/valid-palindrome/yolophg.js +++ b/valid-palindrome/yolophg.js @@ -16,4 +16,4 @@ var isPalindrome = function(s) { } } return result; -}; \ No newline at end of file +};