Skip to content

Commit e486327

Browse files
Merge pull request #2465 from robinyoon-dev/main
[robinyoon-dev] WEEK 04 Solutions
2 parents aef74ca + bdeaaaf commit e486327

File tree

6 files changed

+205
-11
lines changed

6 files changed

+205
-11
lines changed

best-time-to-buy-and-sell-stock/robinyoon-dev.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,49 @@
22
* @param {number[]} prices
33
* @return {number}
44
*/
5-
var maxProfit = function(prices) {
6-
7-
// NOTE: 해설 보고 쓴 코드입니다.
8-
// i 번째 날에 prices[i]의 가격으로 주식을 팔아서 가장 큰 이익을 내려면 주식을 언제 샀어야 했을까?
9-
// 정답은 바로 i 번째 날이 오기 전에 주식이 가장 쌌던 날 입니다!
5+
var maxProfit = function (prices) {
106

11-
let maxProfit = 0;
127
let minPrice = prices[0];
8+
let maxProfit = 0;
9+
10+
for (let i = 1; i < prices.length; i++) {
11+
let currentPrice = prices[i];
1312

14-
for(const price of prices){
15-
const profit = price - minPrice
16-
maxProfit = Math.max(maxProfit, profit);
17-
minPrice = Math.min(price, minPrice);
13+
if (currentPrice < minPrice) {
14+
minPrice = currentPrice;
15+
} else{
16+
let profit = currentPrice - minPrice;
17+
if(profit > maxProfit){
18+
maxProfit = profit;
19+
}
20+
}
1821
}
1922

2023
return maxProfit;
21-
2224
};
25+
26+
27+
// -----아래는 이전에 작성한 답안입니다.
28+
// /**
29+
// * @param {number[]} prices
30+
// * @return {number}
31+
// */
32+
// var maxProfit = function(prices) {
33+
34+
// // NOTE: 해설 보고 쓴 코드입니다.
35+
// // i 번째 날에 prices[i]의 가격으로 주식을 팔아서 가장 큰 이익을 내려면 주식을 언제 샀어야 했을까?
36+
// // 정답은 바로 i 번째 날이 오기 전에 주식이 가장 쌌던 날 입니다!
37+
38+
// let maxProfit = 0;
39+
// let minPrice = prices[0];
40+
41+
// for(const price of prices){
42+
// const profit = price - minPrice
43+
// maxProfit = Math.max(maxProfit, profit);
44+
// minPrice = Math.min(price, minPrice);
45+
// }
46+
47+
// return maxProfit;
48+
49+
// };
50+

coin-change/robinyoon-dev.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
var coinChange = function (coins, amount) {
7+
8+
let dp = new Array(amount + 1).fill(Infinity);
9+
dp[0] = 0;
10+
11+
for (let i = 1; i < dp.length; i++) {
12+
for (let coin of coins) {
13+
14+
if (coin > i) {
15+
continue;
16+
}
17+
18+
let remain = i - coin;
19+
dp[i] = Math.min(dp[i], dp[remain] + 1);
20+
21+
}
22+
};
23+
24+
let result = dp[amount] === Infinity ? -1 : dp[amount];
25+
return result;
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function(nums) {
6+
// nums는 rotated 된 array
7+
// return 값은 이 nums에서 최솟값을 return 해야함.
8+
const NUMS_LENGTH = nums.length;
9+
10+
let result = nums[0];
11+
12+
for(let i = 0; i < NUMS_LENGTH; i++ ){
13+
let currentIndex = NUMS_LENGTH - i;
14+
15+
if(nums[currentIndex - 1] > nums[currentIndex]){
16+
result = nums[currentIndex];
17+
}else{
18+
continue;
19+
}
20+
}
21+
return result;
22+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function (root) {
14+
15+
//NOTE: 현재 노드의 최대 깊이는 왼쪽 자식 트리의 최대 깊이와 오른쪽 자식 트리의 최대 깊이 중 큰 값에 1(현재 노드)을 더한 값
16+
if (root === null) {
17+
return 0;
18+
}
19+
20+
const leftDepth = maxDepth(root.left);
21+
const rightDepth = maxDepth(root.right);
22+
23+
return Math.max(leftDepth, rightDepth) + 1;
24+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function (list1, list2) {
14+
15+
let dummy = new ListNode(-1);
16+
let current = dummy;
17+
18+
while (list1 !== null && list2 !== null) {
19+
if (list1.val <= list2.val) {
20+
current.next = list1;
21+
list1 = list1.next;
22+
} else {
23+
current.next = list2;
24+
list2 = list2.next;
25+
}
26+
current = current.next;
27+
}
28+
29+
if (list1 !== null) {
30+
current.next = list1;
31+
} else {
32+
current.next = list2;
33+
}
34+
35+
return dummy.next;
36+
};
37+

word-search/robinyoon-dev.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
var exist = function (board, word) {
7+
8+
let tempResult = false;
9+
10+
for (let i = 0; i < board.length; i++) {
11+
for (let j = 0; j < board[0].length; j++) {
12+
if (board[i][j] === word[0]) {
13+
tempResult = dfs(i, j, 0);
14+
if (tempResult) {
15+
return true;
16+
}
17+
} else {
18+
continue;
19+
}
20+
}
21+
}
22+
23+
24+
return tempResult;
25+
26+
27+
28+
function dfs(x, y, currentIndex) {
29+
30+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length) {
31+
return false;
32+
}
33+
34+
if (board[x][y] !== word[currentIndex]) {
35+
return false;
36+
}
37+
38+
let nextIndex = currentIndex + 1;
39+
40+
if (nextIndex === word.length) {
41+
return true;
42+
}
43+
44+
let tempChar = board[x][y];
45+
board[x][y] = '*';
46+
47+
48+
let hasCharOnTheLeft = dfs(x - 1, y, nextIndex);
49+
let hasCharOnTheRight = dfs(x + 1, y, nextIndex);
50+
let hasCharAtTheTop = dfs(x, y + 1, nextIndex);
51+
let hasCharAtTheBottom = dfs(x, y - 1, nextIndex);
52+
53+
board[x][y] = tempChar;
54+
55+
return hasCharOnTheLeft || hasCharOnTheRight || hasCharAtTheTop || hasCharAtTheBottom;
56+
}
57+
};

0 commit comments

Comments
 (0)