From a040b911a39f2b7605da4d89a2ecfdb1c5017bc8 Mon Sep 17 00:00:00 2001 From: luckylooky2 Date: Tue, 16 Apr 2024 21:36:09 +0900 Subject: [PATCH] =?UTF-8?q?4.16:=20implement,=20brute=20force(n^2=20?= =?UTF-8?q?=EB=B0=B0=EC=97=B4=20=EC=9E=90=EB=A5=B4=EA=B8=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\236\220\353\245\264\352\270\260.js" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 "programmers/n^2 \353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.js" diff --git "a/programmers/n^2 \353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.js" "b/programmers/n^2 \353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.js" new file mode 100644 index 0000000..52cf081 --- /dev/null +++ "b/programmers/n^2 \353\260\260\354\227\264 \354\236\220\353\245\264\352\270\260.js" @@ -0,0 +1,34 @@ +// n^2 배열 자르기 : 구현, 브루트 포스 +function solution(n, left, right) { + const answer = new Array(right - left + 1).fill(0); + let [leftRow, leftCol] = [Math.floor(left / n), left % n]; + let [rightRow, rightCol] = [Math.floor(right / n), right % n]; + let idx = 0; + for (let r = leftRow; r <= rightRow; r++) { + let startCol, endCol; + + if (r === leftRow && r === rightRow) { + startCol = leftCol; + endCol = rightCol; + } else if (r === leftRow) { + startCol = leftCol; + endCol = n - 1; + } else if (r === rightRow) { + startCol = 0; + endCol = rightCol; + } else { + startCol = 0; + endCol = n - 1; + } + for (let c = startCol; c <= endCol; c++) { + if (c > r) { + answer[idx++] = c + 1; + } else { + answer[idx++] = r + 1; + } + } + } + return answer; +} + +// 34'05" / 60'00"