Skip to content

Commit

Permalink
4.16: implement, brute force(n^2 배열 자르기)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Apr 16, 2024
1 parent 6c895d4 commit a040b91
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions programmers/n^2 배열 자르기.js
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit a040b91

Please sign in to comment.