Skip to content
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

[Helena] Week 1 solutions #48

Merged
merged 2 commits into from
May 5, 2024
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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
15 changes: 15 additions & 0 deletions contains-duplicate/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 16 additions & 0 deletions two-sum/yolophg.js
Original file line number Diff line number Diff line change
@@ -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 [];
};
7 changes: 7 additions & 0 deletions valid-anagram/yolophg.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 Set을 이용하여 문자가 몇 번 등장했는지 유지한 후 비교하도록 했어요~
Set을 사용하는 건 정렬보다 낮은 시간복잡도라는 장점이 있을 것 같아요!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

솔루션 잘 봤습니다! 저도 처음 문제를 풀었을 때 sort 해서 비교하는 방식으로 문제를 풀었었는데요. 해당 방법이 간단하고 원라인코드로 작성가능한 점은 좋으나 sort 때문에 시간복잡도가 n logn이 되는 단점이 있습니다! O(n)으로도 풀이가 가능하니 해당 방법도 도전해보시면 좋을거 같습니다 :)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var isAnagram = function(s, t) {
firstList = s.split("").sort();
secondList = t.split("").sort();
Comment on lines +2 to +3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split("")이 있어야 배열(문자들의 배열)로 바뀌어 계산이 되는거군요 👍


// compare if these are same and return
return JSON.stringify(firstList) === JSON.stringify(secondList);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바스립트스럽네요 ㅋㅋ

};
19 changes: 19 additions & 0 deletions valid-palindrome/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};