Skip to content
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
20 changes: 20 additions & 0 deletions doitchuu/BestTimeToBuyAndSellStock.js
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++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

시간 초과는 과거 최소값을 유지/갱신 하는 방향으로 코드를 수정하면 O(n)으로 줄일 수 있을 것 같아요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 못 풀었는데ㅠ 팁을 알려주셔서 감사합니다.

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;
};
15 changes: 15 additions & 0 deletions doitchuu/MergeTwoSortedLists.js
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: 이 문제는 풀지 못했습니다.
};
20 changes: 16 additions & 4 deletions doitchuu/TwoSum.js
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
}
}
};
35 changes: 35 additions & 0 deletions doitchuu/ValidParentheses.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

if만 사용하면 각 조건을 모두 독립적으로 검사하게 되는 것으로 알고 있는데, 그렇기 때문에 아래와 같은 의도치 않은 비교가 발생할 수도 있을 것 같아요.

  1. 첫 번째 if에서 pop()이 실행
  2. 두 번째 조건도 if기 때문에 또 검사함
  3. 이때 이미 이전 조건에서 pop()이 일어나 stack[stack.length - 1] 값이 바뀐 상태임

그래서 이런식으로 if-elseif를 사용해서 작성하는게 조금 더 안전하지 않을까? 하는 생각을 해보았습니다!

if (ch === ")" && top === "(") stack.pop();
else if (ch === "}" && top === "{") stack.pop();
else if (ch === "]" && top === "[") stack.pop();

Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 같은 의견입니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
};