-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK01] 추슬기 #2
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
[WEEK01] 추슬기 #2
Changes from all commits
79b467a
8b2c4e1
d4aecca
df40223
19d5c31
04982cb
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,20 @@ | ||
| /** | ||
| * @param {number[]} prices | ||
| * @return {number} | ||
| */ | ||
| var maxProfit = function(prices) { | ||
| // MEMO: 시간 초과! | ||
| let maxPrice = 0; | ||
|
|
||
| for (let i = 0; i < prices.length - 1; i++) { | ||
| for (let j = i + 1; j < prices.length; j++) { | ||
| const price = prices[i] - prices[j]; | ||
|
|
||
| if (price < 0) { | ||
| maxPrice = Math.max(maxPrice, Math.abs(price)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return maxPrice; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * function ListNode(val, next) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.next = (next===undefined ? null : next) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {ListNode} list1 | ||
| * @param {ListNode} list2 | ||
| * @return {ListNode} | ||
| */ | ||
| var mergeTwoLists = function(list1, list2) { | ||
| // MEMO: 이 문제는 풀지 못했습니다. | ||
| }; |
|
Collaborator
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 |
|---|---|---|
| @@ -1,4 +1,16 @@ | ||
| // MEMO: 편하신 언어로 작성 부탁드립니다. 해당 주석은 풀이 입력 시 지워주세요. | ||
| function twoSum(nums, target) { | ||
|
|
||
| } | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| */ | ||
| var twoSum = function(nums, target) { | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = i + 1; j < nums.length; j++) { | ||
| if (nums[i] + nums[j] === target) { | ||
| const output = [i, j]; | ||
|
|
||
| return output; | ||
| } | ||
| } | ||
| } | ||
| }; |
|
Collaborator
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. if만 사용하면 각 조건을 모두 독립적으로 검사하게 되는 것으로 알고 있는데, 그렇기 때문에 아래와 같은 의도치 않은 비교가 발생할 수도 있을 것 같아요.
그래서 이런식으로 if-elseif를 사용해서 작성하는게 조금 더 안전하지 않을까? 하는 생각을 해보았습니다! if (ch === ")" && top === "(") stack.pop();
Collaborator
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. 저도 같은 의견입니다!
Collaborator
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. count는 어떤 용도로 사용하고 있는 걸까요? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
|
|
||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| function isValid(s) { | ||
| if (s.length % 2 !== 0) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+7
to
+9
Collaborator
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. 오 생각도 못했는데 예외 처리 좋네요 |
||
|
|
||
| let count = 0; | ||
| const stack = []; | ||
|
|
||
| for (let i = 0; i < s.length; i++) { | ||
| if (s[i] === "(" || s[i] === "{" || s[i] === "[") { | ||
| stack.push(s[i]); | ||
| count++; | ||
| continue; | ||
| } | ||
|
|
||
| if (stack[stack.length - 1] === "(" && s[i] === ")") { | ||
| stack.pop(); | ||
| } | ||
|
|
||
| if (stack[stack.length - 1] === "{" && s[i] === "}") { | ||
| stack.pop(); | ||
| } | ||
|
|
||
| if (stack[stack.length - 1] === "[" && s[i] === "]") { | ||
| stack.pop(); | ||
| } | ||
| } | ||
|
|
||
| return stack.length > 0 && count !== (s.length / 2) ? false : true; | ||
| }; | ||
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.
시간 초과는 과거 최소값을 유지/갱신 하는 방향으로 코드를 수정하면 O(n)으로 줄일 수 있을 것 같아요!
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.
저도 못 풀었는데ㅠ 팁을 알려주셔서 감사합니다.