Skip to content

[RiaOh] Week 06 solutions #1427

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function (prices) {
// 가장 작은 수
let minNum = prices[0];
// 차이 값
let maxProfit = 0;
for (let i = 1; i < prices.length; i++) {
minNum = Math.min(minNum, prices[i - 1]); // 이전꺼 중 가장 작은 수
maxProfit = Math.max(maxProfit, prices[i] - minNum);
}
return maxProfit;
};
24 changes: 24 additions & 0 deletions container-with-most-water/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number[]} height
* @return {number}
*/
const maxArea = function (height) {
let left = 0;
let right = height.length - 1;
let max = 0;

while (left < right) {
const graphW = right - left;
const grapghH = Math.min(height[left], height[right]);

max = Math.max(max, graphW * grapghH);

if (height[left] <= height[right]) {
left++;
} else {
right--;
}
}

return max;
};
28 changes: 28 additions & 0 deletions valid-parentheses/RiaOh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
const newArr = [...s];
if (newArr[0] === ")" || newArr[0] === "}" || newArr[0] === "]") {
return false;
}

for (let i = 1; i < newArr.length; i++) {
if (newArr[i] === ")" && newArr[i - 1] === "(") {
newArr.splice(i - 1, 2);
i = i - 2;
}
Comment on lines +12 to +15
Copy link
Contributor

@minji-go minji-go May 5, 2025

Choose a reason for hiding this comment

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

안녕하세요☺️

배열의 index와 splice 함수를 활용한 풀이방법 흥미롭게 봤습니다.
배열의 첫번째 요소가 닫힌 괄호일 때 얼리 리턴되는게 좋은거 같아요 👍

Stack 개념을 활용한 풀이 방법도 참고해보시면 좋을거 같아요!


if (newArr[i] === "}" && newArr[i - 1] === "{") {
newArr.splice(i - 1, 2);
i = i - 2;
}

if (newArr[i] === "]" && newArr[i - 1] === "[") {
newArr.splice(i - 1, 2);
i = i - 2;
}
}
return newArr.length === 0 ? true : false;
};