diff --git "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23408]\352\264\204\355\230\270\354\247\235\353\247\236\354\266\224\352\270\260.js" "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23408]\352\264\204\355\230\270\354\247\235\353\247\236\354\266\224\352\270\260.js" index 6739bef..e645156 100644 --- "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23408]\352\264\204\355\230\270\354\247\235\353\247\236\354\266\224\352\270\260.js" +++ "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23408]\352\264\204\355\230\270\354\247\235\353\247\236\354\266\224\352\270\260.js" @@ -1,15 +1,35 @@ -const fs = require('fs'); -let input = fs.readFileSync(0).toString().trim().split(''); +// const fs = require('fs'); +// let input = fs.readFileSync(0).toString().trim().split(''); -function solution() { - // 뒤에서부터 순회 (앞에서 지우면 인덱스가 밀리기 때문) - for (let i = input.length - 2; i >= 0; i--) { - if (input[i] === '(' && input[i + 1] === ')') { - // 두 문자를 배열에서 제거 - input.splice(i, 2); +// function solution() { +// // 뒤에서부터 순회 (앞에서 지우면 인덱스가 밀리기 때문) +// for (let i = input.length - 2; i >= 0; i--) { +// if (input[i] === '(' && input[i + 1] === ')') { +// // 두 문자를 배열에서 제거 +// input.splice(i, 2); +// } +// } +// } +// solution(); + +// console.log(input.length === 0 ? true : false); + +function solution(s) { + // "(" 괄호를 저장할 배열 + const arr = []; + for (let i = 0; i < s.length; i++) { + if (s[i] === '(') { + arr.push(s[i]); + } + // ")" 괄호인 경우 + else if (s[i] === ')') { + // 배열이 비어 있으면 짝이 안 맞으므로 false + if (arr.length === 0) { + return false; + } + // 짝이 맞으면 "(" 괄호 하나 제거 + arr.pop(); } } + return arr.length === 0; } -solution(); - -console.log(input.length === 0 ? true : false); diff --git "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23409]10\354\247\204\354\210\230\353\245\2742\354\247\204\354\210\230\353\241\234\353\263\200\355\231\230\355\225\230\352\270\260.js" "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23409]10\354\247\204\354\210\230\353\245\2742\354\247\204\354\210\230\353\241\234\353\263\200\355\231\230\355\225\230\352\270\260.js" index 8c32a89..0860e05 100644 --- "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23409]10\354\247\204\354\210\230\353\245\2742\354\247\204\354\210\230\353\241\234\353\263\200\355\231\230\355\225\230\352\270\260.js" +++ "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23409]10\354\247\204\354\210\230\353\245\2742\354\247\204\354\210\230\353\241\234\353\263\200\355\231\230\355\225\230\352\270\260.js" @@ -1,15 +1,27 @@ -const fs = require('fs'); -let num = Number(fs.readFileSync(0).toString()); +// const fs = require('fs'); +// let num = Number(fs.readFileSync(0).toString()); -let result = []; -function solution() { +// let result = []; +// function solution() { +// while (num > 0) { +// let last = num % 2; +// num = Math.floor(num / 2); +// result.push(last); +// } +// // 뒤집어서 올바른 이진수 순서로 변경 +// result.reverse(); +// console.log(result.join('')); +// } +// solution(); + +function solution(s) { + if (s === 0) return 0; + + const arr = []; while (num > 0) { let last = num % 2; num = Math.floor(num / 2); - result.push(last); + arr.push(last); } - // 뒤집어서 올바른 이진수 순서로 변경 - result.reverse(); - console.log(result.join('')); + return arr.reverse().join(''); } -solution(); diff --git "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23410]\352\264\204\355\230\270\355\232\214\354\240\204\355\225\230\352\270\260.js" "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23410]\352\264\204\355\230\270\355\232\214\354\240\204\355\225\230\352\270\260.js" index 31fc3e9..0da694c 100644 --- "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23410]\352\264\204\355\230\270\355\232\214\354\240\204\355\225\230\352\270\260.js" +++ "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23410]\352\264\204\355\230\270\355\232\214\354\240\204\355\225\230\352\270\260.js" @@ -1,32 +1,69 @@ -function isValid(s) { - let check = []; - for (let i = 0; i < s.length; i++) { - check.push(s[i]); +// function isValid(s) { +// let check = []; +// for (let i = 0; i < s.length; i++) { +// check.push(s[i]); - // 현재 스택 길이 - let num = check.length; +// // 현재 스택 길이 +// let num = check.length; - if (check[num - 2] === '[' && check[num - 1] === ']') - check.splice(num - 2, 2); - if (check[num - 2] === '{' && check[num - 1] === '}') - check.splice(num - 2, 2); - if (check[num - 2] === '(' && check[num - 1] === ')') - check.splice(num - 2, 2); - } - if (check.length == 0) { - return true; +// if (check[num - 2] === '[' && check[num - 1] === ']') +// check.splice(num - 2, 2); +// if (check[num - 2] === '{' && check[num - 1] === '}') +// check.splice(num - 2, 2); +// if (check[num - 2] === '(' && check[num - 1] === ')') +// check.splice(num - 2, 2); +// } +// if (check.length == 0) { +// return true; +// } +// } + +// function solution(s) { +// let count = 0; +// // 문자열을 회전시키면서 검사 +// for (let i = 0; i < s.length; i++) { +// // 현재 문자열이 올바른 괄호면 카운트 증가 +// if (isValid(s)) { +// count++; +// } +// // 문자열을 왼쪽으로 한 칸 회전 +// s = s.slice(1) + s[0]; +// } +// return count; +// } + +function isValid(str) { + const stack = []; + // 올바른 괄호 문자열을 객체형태로 저장 + const object = { + '(': ')', + '{': '}', + '[': ']', + }; + for (let i = 0; i < str.length; i++) { + const chr = str[i]; + // 현재 문자가 여는 괄호라면 stack에 push + if (object[chr]) { + stack.push(chr); + } else { + // stack 배열 길이가 0인경우, 배열 마지막에 저장된 값의 value값이 현재 chr과 같지 않은 경우 무조건 false + if (stack.length === 0 || object[stack.pop()] !== chr) { + return false; + } + } } + return stack.length === 0 ? true : false; } function solution(s) { let count = 0; - // 문자열을 회전시키면서 검사 + // s 길이만큼 for문 반복 for (let i = 0; i < s.length; i++) { - // 현재 문자열이 올바른 괄호면 카운트 증가 + // 문자열이 "올바른 괄호 문자열"인지 확인 if (isValid(s)) { count++; } - // 문자열을 왼쪽으로 한 칸 회전 + // s문자열 맨 앞 문자를 s문자열의 맨 뒤로 이동 s = s.slice(1) + s[0]; } return count; diff --git "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23411]\354\247\235\354\247\200\354\226\264\354\240\234\352\261\260\355\225\230\352\270\260.js" "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23411]\354\247\235\354\247\200\354\226\264\354\240\234\352\261\260\355\225\230\352\270\260.js" index 20d0505..3f340de 100644 --- "a/solutions/gwonwoo/0123/[\353\254\270\354\240\23411]\354\247\235\354\247\200\354\226\264\354\240\234\352\261\260\355\225\230\352\270\260.js" +++ "b/solutions/gwonwoo/0123/[\353\254\270\354\240\23411]\354\247\235\354\247\200\354\226\264\354\240\234\352\261\260\355\225\230\352\270\260.js" @@ -1,23 +1,35 @@ -function solution(s) { - let answer = 0; +// function solution(s) { +// let answer = 0; - // 짝을 비교하기 위한 스택 배열 - const stack = []; - // 문자열을 배열로 변환 (문자 하나씩 순회하기 위해) - const arr = [...s]; +// // 짝을 비교하기 위한 스택 배열 +// const stack = []; +// // 문자열을 배열로 변환 (문자 하나씩 순회하기 위해) +// const arr = [...s]; + +// for (let chr of arr) { +// // 스택이 비어있지 않고, 스택의 마지막 값이 현재 문자와 같다면 +// if (stack.length && stack[stack.length - 1] === chr) { +// // 짝이므로 제거 +// stack.pop(); +// } else { +// // 짝이 아니면 스택에 추가 +// stack.push(chr); +// } +// } + +// stack.length === 0 ? (answer = 1) : (answer = 0); + +// return answer; +// } - for (let chr of arr) { - // 스택이 비어있지 않고, 스택의 마지막 값이 현재 문자와 같다면 +function solution(s) { + const stack = []; + for (let chr of s) { if (stack.length && stack[stack.length - 1] === chr) { - // 짝이므로 제거 stack.pop(); } else { - // 짝이 아니면 스택에 추가 stack.push(chr); } } - - stack.length === 0 ? (answer = 1) : (answer = 0); - - return answer; + return stack.length === 0 ? 1 : 0; } diff --git a/solutions/gwonwoo/0123/refactoring b/solutions/gwonwoo/0123/refactoring new file mode 100644 index 0000000..e69de29 diff --git "a/solutions/gwonwoo/0127/[\353\254\270\354\240\23415]\354\232\224\354\204\270\355\221\270\354\212\244\353\254\270\354\240\234.js" "b/solutions/gwonwoo/0127/[\353\254\270\354\240\23415]\354\232\224\354\204\270\355\221\270\354\212\244\353\254\270\354\240\234.js" index d9b1ec7..a44da39 100644 --- "a/solutions/gwonwoo/0127/[\353\254\270\354\240\23415]\354\232\224\354\204\270\355\221\270\354\212\244\353\254\270\354\240\234.js" +++ "b/solutions/gwonwoo/0127/[\353\254\270\354\240\23415]\354\232\224\354\204\270\355\221\270\354\212\244\353\254\270\354\240\234.js" @@ -29,3 +29,5 @@ while (que.length) { } console.log(`<${result.join(', ')}>`); + +const queue = Array.from(queue.fill(1)); diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23418]\353\221\220\352\260\234\354\235\230\354\210\230\353\241\234\355\212\271\354\240\225\352\260\222\353\247\214\353\223\244\352\270\260.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23418]\353\221\220\352\260\234\354\235\230\354\210\230\353\241\234\355\212\271\354\240\225\352\260\222\353\247\214\353\223\244\352\270\260.js" new file mode 100644 index 0000000..1a5f45b --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23418]\353\221\220\352\260\234\354\235\230\354\210\230\353\241\234\355\212\271\354\240\225\352\260\222\353\247\214\353\223\244\352\270\260.js" @@ -0,0 +1,46 @@ +function solution(arr, target) { + for (let i = 0; i < arr.length - 1; i++) { + for (let j = i + 1; j < arr.length; j++) { + if (arr[i] + arr[j] === target) { + return true; + } + } + } + return false; +} +// 이중 for문을 사용함으로 시간복잡도는 O(n^2)가 발생한다. +// 그리고 가독성도 그렇게 좋아보이지 않는다. +// 누구나 쉽게 만들 수 있는 코드 +// =============================================== + +function solution(arr, target) { + const result = []; + for (let num of arr) { + if (result.includes(target - num)) { + return true; + } + // 값을 넣어줘야 다음에 이 값을 사용하는지 판단한다. + set.push(num); + } + return false; +} + +// target - num 값이 arr 배열안에 존재만 파악하는 코드 +// 이중 for문보다 보기 가독성이 높지만, 똑같이 시간복잡도는 O(n^2)가 발생한다. +// .includes 메서드는 내부를 순회함으로 O(n)을 갖는다. +// =============================================== + +function solution(arr, target) { + const set = new Set(); + for (let num of arr) { + if (set.has(target - num)) { + return true; + } + // 값을 넣어줘야 다음에 이 값을 사용하는지 판단한다. + set.add(num); + } + return false; +} +// target - num 값이 arr 배열안에 존재만 파악하는 코드 +// set을 이용하여 가독성도 높이고 시간복잡도도 줄였다. O(n) +// set.has()와 set.add() 둘다 O(1) 시간복잡도를 가진다. diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23419]\353\254\270\354\236\220\354\227\264\355\225\264\354\213\261\354\235\204\354\235\264\354\232\251\355\225\234\352\262\200\354\203\211\355\225\250\354\210\230\353\247\214\353\223\244\352\270\260.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23419]\353\254\270\354\236\220\354\227\264\355\225\264\354\213\261\354\235\204\354\235\264\354\232\251\355\225\234\352\262\200\354\203\211\355\225\250\354\210\230\353\247\214\353\223\244\352\270\260.js" new file mode 100644 index 0000000..571f2e1 --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23419]\353\254\270\354\236\220\354\227\264\355\225\264\354\213\261\354\235\204\354\235\264\354\232\251\355\225\234\352\262\200\354\203\211\355\225\250\354\210\230\353\247\214\353\223\244\352\270\260.js" @@ -0,0 +1,13 @@ +function solution(string, query) { + let answer = []; + + const set = new Set(query); + for (let str of string) { + if (set.has(str)) { + answer.push('True'); + } else { + answer.push('false'); + } + } + return answer; +} diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23420]\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23420]\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.js" new file mode 100644 index 0000000..23c56c9 --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23420]\354\231\204\354\243\274\355\225\230\354\247\200\353\252\273\355\225\234\354\204\240\354\210\230.js" @@ -0,0 +1,11 @@ +function solution(participant, completion) { + participant.sort(); // 참가자 이름 정렬 + completion.sort(); // 통과자 이름 정렬 + + for (let i = 0; i < participant.length; i++) { + // 통과하지 못한 딱 한 사람만 출력하기 때문에 정렬했을 때 맞지 않은 사람 출력 + if (participant[i] !== completion[i]) { + return participant[i]; + } + } +} diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23421]\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23421]\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" new file mode 100644 index 0000000..bd96145 --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23421]\354\230\201\354\226\264\353\201\235\353\247\220\354\236\207\352\270\260.js" @@ -0,0 +1,34 @@ +function solution(n, words) { + const answer = [0, 0]; // 정답 배열 + const set = new Set(); // 단어 재사용 확인 + + let count = 0; // 단어 인덱스 + let order = 1; // 반복 수 + while (count < words.length) { + if (count === 0) { + // 첫번째 문자일 때 + set.add(words[count]); // set에 해당 문자 저장 + count++; + continue; // 바로 다음 문자 진행 + } + // 문자가 중복이 아니고, 앞사람이 말한 단어의 마지막 문자로 시작하는 단어를 말해는지 확인 + else if (set.has(words[count]) || check(words[count - 1], words[count])) { + answer[0] = (count % n) + 1; // 사람 순번 계산 + answer[1] = order; + break; + } + set.add(words[count]); + count++; + + if (count % n === 0) { + // 로테이션이 돌 때 order++ + order++; + } + } + return answer; +} + +// 앞사람이 말한 단어의 마지막 문자로 시작하는 단어를 말해는지 확인 함수 +function check(a, b) { + return a[a.length - 1] !== b[0] ? true : false; +} diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23422]\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23422]\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.js" new file mode 100644 index 0000000..d801d92 --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23422]\354\240\204\355\231\224\353\262\210\355\230\270\353\252\251\353\241\235.js" @@ -0,0 +1,22 @@ +function solution(phone) { + phone.sort(); // 문자열 정렬 + for (let i = 0; i < phone.length - 1; i++) { + const a = phone[i]; + const b = phone[i + 1]; + + let count = 0; + let answer = true; + for (let j = 0; j < a.length; j++) { + if (a[j] !== b[j]) { + break; + } + count++; + } + if (count === a.length) { + answer = false; + } + if (!answer) return answer; + } + + return true; +} diff --git "a/solutions/gwonwoo/0128/[\353\254\270\354\240\23423]\355\225\240\354\235\270\355\226\211\354\202\254.js" "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23423]\355\225\240\354\235\270\355\226\211\354\202\254.js" new file mode 100644 index 0000000..a3c623a --- /dev/null +++ "b/solutions/gwonwoo/0128/[\353\254\270\354\240\23423]\355\225\240\354\235\270\355\226\211\354\202\254.js" @@ -0,0 +1,31 @@ +function solution(want, number, discount) { + let answer = 0; + const obj = {}; + // 원하는 상품과 수량을 객체로 매핑 + for (let i = 0; i < want.length; i++) { + { + obj[want[i]] = number[i]; + } + } + + // 마트 할인 기간 - 회원 자격 기간 (할인 기간 중 연속된 10일씩 확인) + for (let i = 0; i <= discount.length - 10; i++) { + const temp = { ...obj }; + for (let j = i; j < 10 + i; j++) { + // 해당 상품이 원하는 목록에 있으면 개수 감소 + if (temp[discount[j]]) { + temp[discount[j]]--; + } + } + // // 모든 상품이 조건을 만족했는지 확인 + let result = true; + for (let key in temp) { + if (temp[key] > 0) { + result = false; + } + } + // result 값이 false이면, answer++ + if (result) answer++; + } + return answer; +} diff --git "a/solutions/gwonwoo/0129/[\353\254\270\354\240\23424]\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.js" "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23424]\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.js" new file mode 100644 index 0000000..f6131de --- /dev/null +++ "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23424]\354\230\244\355\224\210\354\261\204\355\214\205\353\260\251.js" @@ -0,0 +1,22 @@ +function solution(record) { + const answer = []; + const user = {}; + + for (const r of record) { + const [pre, id, nick] = r.split(' '); + // Leave에서는 nick 값이 없음! + if (pre !== 'Leave') user[id] = nick; + } + + for (const r of record) { + const [pre, id] = r.split(' '); + + if (pre === 'Enter') { + answer.push(`${user[id]}님이 들어왔습니다.`); + } else if (pre === 'Leave') { + answer.push(`${user[id]}님이 나갔습니다.`); + } + } + + return answer; +} diff --git "a/solutions/gwonwoo/0129/[\353\254\270\354\240\23425]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23425]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" new file mode 100644 index 0000000..6e30648 --- /dev/null +++ "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23425]\353\262\240\354\212\244\355\212\270\354\225\250\353\262\224.js" @@ -0,0 +1,20 @@ +function solution(types, plays) { + const answer = []; + + // gpt 도움 받음 + const music = types.reduce((acc, cur, index) => {}); + + // 각 장르별 재생횟수 합계 + const totalSum = {}; + for (const type of types) { + let sum = 0; + for (const m of music[type]) { + sum += m.play; + } + totalSum[type] = sum; + } + + // 여기까지 구현함 + + return answer; +} diff --git "a/solutions/gwonwoo/0129/[\353\254\270\354\240\23426]\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260.js" "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23426]\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260.js" new file mode 100644 index 0000000..cbe690a --- /dev/null +++ "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23426]\354\213\240\352\263\240\352\262\260\352\263\274\353\260\233\352\270\260.js" @@ -0,0 +1,35 @@ +function solution(list, report, k) { + const answer = []; // 각 유저가 받은 결과 메일 수 + + // 동일한 신고는 중복으로 제거 + const set = [...new Set(report)]; + // 각 유저가 신고당한 수 + const user = set.reduce((acc, cur) => { + const [p, r] = cur.split(' '); + acc[r] = (acc[r] || 0) + 1; + return acc; + }, {}); + + // 신고가 k번 이상 당한 사람 확인 + const reported = Object.keys(user).filter((key) => user[key] >= k); + + // 메일 보낼 유저 확인 + const mail = list.reduce((acc, cur) => { + acc[cur] = 0; + return acc; + }, {}); + + // 몇 개의 메일을 받을지 확인 + for (let s of set) { + const [p, r] = s.split(' '); + if (reported.includes(r)) { + mail[p]++; + } + } + + // 객체를 배열로 변환 + for (let user of list) { + answer.push(mail[user]); + } + return answer; +} diff --git "a/solutions/gwonwoo/0129/[\353\254\270\354\240\23427]\353\251\224\353\211\264\353\246\254\353\211\264\354\226\274.js" "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23427]\353\251\224\353\211\264\353\246\254\353\211\264\354\226\274.js" new file mode 100644 index 0000000..8b1a904 --- /dev/null +++ "b/solutions/gwonwoo/0129/[\353\254\270\354\240\23427]\353\251\224\353\211\264\353\246\254\353\211\264\354\226\274.js" @@ -0,0 +1,14 @@ +function solution(orders, course) { + const result = course.reduce((acc, cur, index) => { + if (!acc[cur]) acc[cur] = []; + const order = orders[index]; + for (let i = 0; i < order.length - cur; i++) { + for (let j = 0; j < cur; j++) { + acc[cur].push(); + } + } + return acc; + }, {}); +} + +// 시밤 모르겠다 diff --git "a/solutions/gwonwoo/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" new file mode 100644 index 0000000..d5f1195 --- /dev/null +++ "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" @@ -0,0 +1,44 @@ +const fs = require('fs'); +const input = fs.readFileSync(0).toString().trim().split('\n'); +const n = Number(input[0]); + +const tree = {}; + +for (let i = 1; i <= n; i++) { + const [head, left, right] = input[i].split(' '); + tree[head] = [left, right]; +} + +let result = ''; +function preOrder(node) { + if (node === '.') return; + const [left, right] = tree[node]; + result += node; + preOrder(left); + preOrder(right); +} + +function inOrder(node) { + if (node === '.') return; + const [left, right] = tree[node]; + inOrder(left); + result += node; + inOrder(right); +} + +function postOrder(node) { + if (node === '.') return; + const [left, right] = tree[node]; + postOrder(left); + postOrder(right); + result += node; +} + +preOrder('A'); +console.log(result); +result = ''; +inOrder('A'); +console.log(result); +result = ''; +postOrder('A'); +console.log(result); diff --git "a/solutions/gwonwoo/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" new file mode 100644 index 0000000..fcef57e --- /dev/null +++ "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" @@ -0,0 +1,56 @@ +class Node { + constructor(value) { + this.value = value; + this.left = null; + this.right = null; + } +} + +class BinarySearchTree { + constructor() { + this.root = null; + } + + insert(value) { + const newNode = new Node(value); + + if (!this.root) { + this.root = newNode; + return; + } + + let current = this.root; + + while (true) { + if (value < current.value) { + if (!current.left) { + current.left = newNode; + return; + } + current = current.left; + } else { + if (!current.right) { + current.right = newNode; + return; + } + current = current.right; + } + } + } + + search(value) { + let current = this.root; + while (current) { + if (value === current.value) return true; + current = value < current.value ? current.left : current.right; + } + return false; + } + + inorder(node = this.root) { + if (!node) return; + this.inorder(node.left); + console.log(node.value); + this.inorder(node.right); + } +} diff --git "a/solutions/gwonwoo/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" new file mode 100644 index 0000000..cf81be5 --- /dev/null +++ "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" @@ -0,0 +1,9 @@ +function solution(n, a, b) { + let count = 0; + while (a !== b) { + a = Math.ceil(a / 2); + b = Math.ceil(b / 2); + count++; + } + return count; +} diff --git "a/solutions/gwonwoo/0130/[\353\254\270\354\240\23431]\353\213\244\353\213\250\352\263\204\354\271\253\354\206\224\355\214\220\353\247\244.js" "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23431]\353\213\244\353\213\250\352\263\204\354\271\253\354\206\224\355\214\220\353\247\244.js" new file mode 100644 index 0000000..e69de29 diff --git "a/solutions/gwonwoo/0130/[\353\254\270\354\240\23432]\352\270\270\354\260\276\352\270\260\352\262\214\354\236\204.js" "b/solutions/gwonwoo/0130/[\353\254\270\354\240\23432]\352\270\270\354\260\276\352\270\260\352\262\214\354\236\204.js" new file mode 100644 index 0000000..e69de29