diff --git a/2022KInternship/1.js b/2022KInternship/1.js new file mode 100644 index 0000000..1f14b37 --- /dev/null +++ b/2022KInternship/1.js @@ -0,0 +1,18 @@ +function solution(survey, choices) { + var answer = '' + const characterMap = { R: 0, T: 0, C: 0, F: 0, J: 0, M: 0, A: 0, N: 0 } + const characterPair = ['RT', 'CF', 'JM', 'AN'] + const choiceScoreMap = { 1: 3, 2: 2, 3: 1, 4: 0, 5: 1, 6: 2, 7: 3 } + survey.forEach((eachSurvey, i) => { + const [pro, con] = eachSurvey.split('') + const answer = choices[i] + const character = answer < 5 ? pro : con + const point = choiceScoreMap[answer] + characterMap[character] += point + }) + characterPair.forEach((pair) => { + const [first, second] = pair.split('') + answer += characterMap[first] >= characterMap[second] ? first : second + }) + return answer +} diff --git a/2022KInternship/2.js b/2022KInternship/2.js new file mode 100644 index 0000000..166f18e --- /dev/null +++ b/2022KInternship/2.js @@ -0,0 +1,57 @@ +const getSum = (q) => { + return q.reduce((a, b) => a + b, 0) +} + +function solution(queue1, queue2) { + //투포인터 + var answer = -2 + const result = [] + const newQueue1 = [0, ...queue1, ...queue2] + const newQueue2 = [0, ...queue2, ...queue1] + const dp1 = Array.from({ length: newQueue1.length }, () => 0) + const dp2 = Array.from({ length: newQueue2.length }, () => 0) + //초기값만들어놓기 + for (let i = 1; i < queue1.length + 1; i++) { + dp1[i] = newQueue1[i] + dp1[i - 1] + dp2[i] = newQueue2[i] + dp2[i - 1] + } + const sum1 = queue1.reduce((a, b) => a + b, 0) + const sum2 = queue2.reduce((a, b) => a + b, 0) + if (sum1 === sum2) return 0 + const half = Math.floor((sum1 + sum2) / 2) + //newQueue1에 대해서 검증 + let left = 0 + let rightStart = queue1.length + let right = queue1.length + while (left < right && left < newQueue1.length && right < newQueue1.length) { + const currentSum = dp1[right] - dp1[left] + if (currentSum === half) { + result.push(right - rightStart + left) + break + } else if (currentSum > half) { + left++ + } else { + right++ + dp1[right] = newQueue1[right] + dp1[right - 1] + } + } + //newQueue2에 대해서 검증 + left = 0 + rightStart = queue2.length + right = queue2.length + while (left < right && left < newQueue2.length && right < newQueue2.length) { + const currentSum = dp2[right] - dp2[left] + if (currentSum === half) { + result.push(right - rightStart + left) + break + } else if (currentSum > half) { + left++ + } else { + right++ + dp2[right] = newQueue2[right] + dp2[right - 1] + } + } + result.sort((a, b) => a - b) + answer = result.length ? result[0] : -1 + return answer +} diff --git a/2022KInternship/3.js b/2022KInternship/3.js new file mode 100644 index 0000000..29f914b --- /dev/null +++ b/2022KInternship/3.js @@ -0,0 +1,4 @@ +function solution(alp, cop, problems) { + var answer = 0 + return answer +} diff --git a/2022KInternship/4.js b/2022KInternship/4.js new file mode 100644 index 0000000..0bb914d --- /dev/null +++ b/2022KInternship/4.js @@ -0,0 +1,59 @@ +function solution(n, paths, gates, summits) { + var answer = [] + const adjMap = {} + const nodes = {} + gates.forEach((gate) => { + nodes[gate] = { isSummit: false, isGate: true } + }) + summits.forEach((summit) => { + nodes[summit] = { isSummit: true, isGate: false } + }) + paths.forEach((path) => { + const [start, end, weight] = path + start in adjMap ? adjMap[start].push([end, weight]) : (adjMap[start] = [end, weight]) + end in adjMap ? adjMap[end].push([start, weight]) : (adjMap[end] = [start, weight]) + if (!(start in nodes)) { + nodes[start] = { isSummit: false, isGate: false } + } + if (!(end in nodes)) { + nodes[end] = { isSummit: false, isGate: false } + } + }) + console.log(nodes) + const nNodes = Object.keys(nodes).length + const graph = Array.from({ length: nNodes + 1 }, (_, i) => + Array.from({ length: nNodes + 1 }, (_, j) => (i === j ? 0 : Infinity)), + ) + paths.forEach((path) => { + const [start, end, weight] = path + adjMap[start][end] = weight + adjMap[end][start] = weight + }) + + for (let m = 1; m < nNodes + 1; m++) { + for (let s = 1; s < nNodes + 1; s++) { + for (let e = 1; e < nNodes + 1; e++) { + //여기서 걸러보기 + //오로지 게이트에서 출발해서 산봉우리여야함 intensity는 그 값의 2배를 하면 된다. 왕복이니까 + if (nodes[s].isGate) { + if ( + (!nodes[m].isGate && !nodes[m].isSummit && nodes[e].isSummit) || + (!nodes[m].isGate && !nodes[m].isSummit && !nodes[e].isSummit) + ) { + graph[s][e] = Math.min(graph[s][e], graph[s][m] + graph[m][e]) + } + } else if (nodes[s].isSummit) { + if (!nodes[m].isSummit && !nodes[e].isSummit) { + graph[s][e] = Math.min(graph[s][e], graph[s][m] + graph[m][e]) + } + } else { + if (!nodes[m].isGate && !nodes[m].isGate) { + graph[s][e] = Math.min(graph[s][e], graph[s][m] + graph[m][e]) + } + } + } + } + } + console.log(graph) + return answer +} diff --git a/2022KInternship/5.js b/2022KInternship/5.js new file mode 100644 index 0000000..7557592 --- /dev/null +++ b/2022KInternship/5.js @@ -0,0 +1,151 @@ +class Matrix { + constructor(board) { + this.board = board + } + + shiftRow() { + const popped = this.board.pop() + this.board = [popped].concat(this.board) + } + unshiftRow() { + const shifted = this.board.shift() + this.board.push(shifted) + } + rotate() { + const rowSize = this.board.length + const colSize = this.board[0].length + const lastNum = this.board[0][0] + //1row부터 length번 row 0번 컬럼부터 위로 땡기기 + for (let row = 1; row < rowSize; row++) { + this.board[row - 1][0] = this.board[row][0] + } + //마지막row의 1번컬럼부터 마지막까지 좌로 땡기기 + for (let col = 1; col < colSize; col++) { + this.board[rowSize - 1][col - 1] = this.board[rowSize - 1][col] + } + //마지막에서 두 번째 row부터 0row까지 아래로 땡기기 + for (let row = rowSize - 2; row >= 0; row--) { + this.board[row + 1][colSize - 1] = this.board[row][colSize - 1] + } + //첫 row 오른쪽으로 당기기 + for (let col = colSize - 2; col >= 1; col--) { + this.board[0][col + 1] = this.board[0][col] + } + this.board[0][1] = lastNum + } + reverseRotate() { + const rowSize = this.board.length + const colSize = this.board[0].length + const lastNum = this.board[0][0] + for (let col = 1; col < colSize; col++) { + this.board[0][col - 1] = this.board[0][col] + } + for (let row = rowSize - 1; row >= 0; row--) { + this.board[row - 1][colSize - 1] = this.board[row][colSize - 1] + } + for (let col = colSize - 2; col >= 0; col--) { + this.board[rowSize - 1][col + 1] = this.board[rowSize - 1][col] + } + for (let row = 1; row < rowSize; row++) { + this.board[row + 1][0] = this.board[row][0] + } + this.board[1][0] = lastNum + } + jumpRotate(n) { + const rowSize = this.board.length + const colSize = this.board[0].length + const nums = [] + for (let col = 0; col < colSize; col++) { + nums.push(this.board[0][col]) + } + for (let row = 1; row < rowSize; row++) { + nums.push(this.board[row][colSize - 1]) + } + for (let col = colSize - 2; col >= 0; col--) { + nums.push(this.board[rowSize - 1][col]) + } + for (let row = rowSize - 2; row >= 1; row--) { + nums.push(this.board[row][0]) + } + const jumped = [...nums.slice(-n), ...nums.slice(0, nums.length - n)] + //다시 넣어주기 + let pointer = 0 + for (let col = 0; col < colSize; col++) { + this.board[0][col] = jumped[pointer++] + } + for (let row = 1; row < rowSize; row++) { + this.board[row][colSize - 1] = jumped[pointer++] + } + for (let col = colSize - 2; col >= 0; col--) { + this.board[rowSize - 1][col] = jumped[pointer++] + } + for (let row = rowSize - 2; row >= 1; row--) { + this.board[row][0] = jumped[pointer++] + } + } + + print() { + return this.board + } +} + +function solution(rc, operations) { + var answer = [[]] + const matrix = new Matrix(rc) + const rowSize = rc.length + const colSize = rc[0].length + let rotates = 0 + let shifts = 0 + let status = '' + const edgeLength = 2 * (rc.length + rc[0].length) - 4 + operations.forEach((operation, i) => { + if (i === 0) { + status = operation + status === 'Rotate' ? rotates++ : shifts++ + return + } + if (operation === status) { + if (operation === 'Rotate') { + rotates = (rotates + 1) % edgeLength + } else { + shifts = (shifts + 1) % rowSize + } + } else { + //새로운 스태터스라면 + //누적된 이전 스태터스 작업을 해주면됨 + if (operation === 'Rotate') { + //누적된 shift작업 + if (shifts <= rowSize / 2) { + for (let i = 0; i < shifts; i++) { + matrix.shiftRow() + } + } else { + for (let i = 0; i < rowSize - shifts; i++) { + matrix.shiftRow() + } + } + shifts = 0 + rotates = 1 + status = 'Rotate' + } else { + //누적된 rotate작업 + matrix.jumpRotate(rotates) + shifts = 1 + rotates = 0 + status = 'ShiftRow' + } + } + }) + //남은 작업 + if (status === 'Rotate') { + //누적된 rotate작업 + matrix.jumpRotate(rotates) + } else { + //누적된 shift작업 + for (let i = 0; i < shifts; i++) { + matrix.shiftRow() + } + } + answer = matrix.print() + return answer +}