-
Notifications
You must be signed in to change notification settings - Fork 4
[yulrang] 25.01.09 #18
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
Conversation
JooKangsan
left a comment
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.
수고하셨어요!
| var answer = ''; | ||
| answer = my_string.split('').reverse().join(''); |
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.
이건 한줄로 작성해도 될거같아요!
| if( arr[i] !== 'Z' ) { | ||
| newArr.push(arr[i]); | ||
| } else { | ||
| newArr.pop(arr[i-1]) |
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.
newArr.pop(); 으로 만 넣어도 될거같아요!
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.
매 순회마다 배열을 사용하고 배열의 값을 더한다음 배열을 변경하는 작업보다는 그냥 숫자를 직접 조작하는 방식이 더 좋을것 같아요!!
function solution(my_string) {
let answer = 0;
my_string.split(' ').forEach((el, idx, arr) => {
if(idx === 0) {
answer = Number(el);
}
if(el === '+' || el === '-') {
if (el === '+'){
answer += Number(arr[idx + 1]); // 다음 숫자를 더함
} else {
answer -= Number(arr[idx + 1]); // 다음 숫자를 뺌
}
}
})
return answer;
}
그리고 부호일 경우도 순회를 계속해서 불필요한 순회가 일어나고 있는거같아요.
function solution(my_string) {
let answer = 0;
const arr = my_string.split(' ');
answer = Number(arr[0]);
for(let i = 1; i < arr.length; i += 2) {
if(arr[i] === '+') {
answer += Number(arr[i + 1]);
} else {
answer -= Number(arr[i + 1]);
}
}
return answer;
}
이런식으로 순회를 줄일수도 있을거 같아요
| let basket = []; | ||
|
|
||
| const runBasket = () => { | ||
| for(let i=basket.length-1; i>0; i--) { |
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.
전체 순회를 돌 필요는 없을 것 같아요!
const runBasket = () => {
const lastIdx = basket.length - 1;
if (lastIdx > 0 && basket[lastIdx] === basket[lastIdx - 1]) {
basket.splice(lastIdx - 1, 2);
answer += 2;
}
}
이런식으로 돌수 있어요!! 그리고 slice메소드 보다는 pop 메소드가 더 시간 복잡도가 적습니다!! 근데 pop을 두번해야해서 지금상태도 괜찮을것 같아요
| } else if(el === ')' && pairArr.includes('(')) { | ||
| pairArr.pop(); | ||
| } else | ||
| pairArr.push('-'); |
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.
return 으로 조기 반환하면 좋을거 같아요
| pairArr.push('-'); | ||
| }) | ||
|
|
||
| answer = pairArr.length === 0 ? true : false; |
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.
이거는 삼항연산자를 안써도
pairArr.length === 0
여기서 true, false값이 구분 되요!
스택의 시간복잡도
1) Insertion
Stack에 데이터를 추가하는 것은 Stack의 맨 위에 하나를 올리기(push)를 하면된다. 스택에 들어있는 값들이 무수히 많아도 하나의 데이터의 삽입은 한 번이기 때문에 시간 복잡도는 O(1)이 된다.
2) delete
LIFO에 따라 삭제를 할 때도 최상위 데이터가 삭제되는 것이므로 스택의 크기에 상관없이 시간복잡도는 O(1)이된다.
3) Access
스택(Stack) 특성상 한쪽 끝으로만 자료를 넣고 뺄 수 있는 자료구조이므로 데이터 접근 또한 데이터가 삽입된 top을 통해서만 접근이 가능하다. 삭제 또한 top을 통해서만 가능하다. 따라서 n번 째 접근은 top데이터부터 순회하기 때문에 O(n)의 시간복잡도를 가진다.
4) Search
데이터를 찾을 때 만약 스택의 가장 최상단에 있는 데이터를 찾는다면 시간복잡도는 O(1)일 것이다. 하지만 가장 밑에있는 데이터를 찾는다면 데이터의 개수만큼 작업이 발생되므로 시간복잡도는 O(n)이 되겠다.
📌 푼 문제
문자열 뒤집기 | https://school.programmers.co.kr/learn/courses/30/lessons/120822
컨트롤 제트 | https://school.programmers.co.kr/learn/courses/30/lessons/120853
영어가 싫어요 | https://school.programmers.co.kr/learn/courses/30/lessons/120894
문자열 계산하기 | https://school.programmers.co.kr/learn/courses/30/lessons/120902
크레인 인형뽑기 게임 | https://school.programmers.co.kr/learn/courses/30/lessons/64061
📝 간단한 풀이 과정
크레인 인형뽑기 게임
움직임 배열의 인덱스값을 이차원 배열인 board에 넣고, 반복문으로 비어있는지 검사했습니다.
만약 비어있지 않다면 바스켓에 인형을 넣고, 해당 칸은 비우도록 처리했습니다.
또한, runBasket이라는 함수를 만들어 실행하게 함으로써 사라진 인형의 갯수를 계산하도록 했습니다.