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
14 changes: 14 additions & 0 deletions sik9252/BestTimeToBuyAndSellStock.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
@@ -0,0 +1,14 @@
var maxProfit = function (prices) {
let minPrice = Infinity;
let currentProfit = 0;

for (let i = 0; i < prices.length; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
} else if (prices[i] - minPrice > currentProfit) {
currentProfit = prices[i] - minPrice;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

else 문이 없다보니 아래처럼 풀어도 좋지 않을까 싶긴하네요 👍
기존 풀이도 깔끔해서 좋은 거 같아요!

for (let i = 0; i < prices.length; i++) {
  if (prices[i] < minPrice) {
    minPrice = prices[i];
    continue;
  }

  const profit = prices[i] - minPrice;
  if (profit > currentProfit) currentProfit = profit;
}


return currentProfit;
};
24 changes: 24 additions & 0 deletions sik9252/MergeTwoSortedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var mergeTwoLists = function (list1, list2) {
const start = new ListNode(-1);
let curr = start;

while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
curr.next = list1;
list1 = list1.next;
} else {
curr.next = list2;
list2 = list2.next;
}

curr = curr.next;
}

if (list1 !== null) {
curr.next = list1;
} else {
curr.next = list2;
}

return start.next;
};
13 changes: 9 additions & 4 deletions sik9252/TwoSum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// MEMO: 편하신 언어로 작성 부탁드립니다. 해당 주석은 풀이 입력 시 지워주세요.
function twoSum(nums, target) {

}
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length - 1; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
};
23 changes: 23 additions & 0 deletions sik9252/ValidParentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var isValid = function (s) {
if (s.length % 2 !== 0) return false;

const stack = [];
const map = {
")": "(",
"]": "[",
"}": "{",
};
Comment on lines +5 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.

오우 아이디어가 좋네요!


for (const char of s) {
if (map[char]) {
const topElement = stack.pop();
if (topElement !== map[char]) {
return false;
}
} else {
stack.push(char);
}
}

return stack.length === 0;
};