-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
}; |
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; | ||
}; |
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 []; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// compare if these are same and return | ||
return JSON.stringify(firstList) === JSON.stringify(secondList); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바스립트스럽네요 ㅋㅋ |
||
}; |
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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는
Set
을 이용하여 문자가 몇 번 등장했는지 유지한 후 비교하도록 했어요~Set
을 사용하는 건 정렬보다 낮은 시간복잡도라는 장점이 있을 것 같아요!