Skip to content

Commit 2eece50

Browse files
committed
[level 2] Title: n^2 배열 자르기, Time: 21.16 ms, Memory: 24.3 MB -BaekjoonHub
1 parent 023a379 commit 2eece50

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [level 2] n^2 배열 자르기 - 87390
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/87390)
4+
5+
### 성능 요약
6+
7+
메모리: 24.3 MB, 시간: 21.16 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 월간 코드 챌린지 시즌3
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 05월 08일 17:14:42
20+
21+
### 문제 설명
22+
23+
<p>정수 <code>n</code>, <code>left</code>, <code>right</code>가 주어집니다. 다음 과정을 거쳐서 1차원 배열을 만들고자 합니다.</p>
24+
25+
<ol>
26+
<li><code>n</code>행 <code>n</code>열 크기의 비어있는 2차원 배열을 만듭니다.</li>
27+
<li><code>i = 1, 2, 3, ..., n</code>에 대해서, 다음 과정을 반복합니다.
28+
29+
<ul>
30+
<li>1행 1열부터 <code>i</code>행 <code>i</code>열까지의 영역 내의 모든 빈 칸을 숫자 <code>i</code>로 채웁니다.</li>
31+
</ul></li>
32+
<li>1행, 2행, ..., <code>n</code>행을 잘라내어 모두 이어붙인 새로운 1차원 배열을 만듭니다.</li>
33+
<li>새로운 1차원 배열을 <code>arr</code>이라 할 때, <code>arr[left]</code>, <code>arr[left+1]</code>, ..., <code>arr[right]</code>만 남기고 나머지는 지웁니다.</li>
34+
</ol>
35+
36+
<p>정수 <code>n</code>, <code>left</code>, <code>right</code>가 매개변수로 주어집니다. 주어진 과정대로 만들어진 1차원 배열을 return 하도록 solution 함수를 완성해주세요.</p>
37+
38+
<hr>
39+
40+
<h5>제한사항</h5>
41+
42+
<ul>
43+
<li>1 ≤ <code>n</code> ≤ 10<sup>7</sup></li>
44+
<li>0 ≤ <code>left</code> ≤ <code>right</code> &lt; n<sup>2</sup></li>
45+
<li><code>right</code> - <code>left</code> &lt; 10<sup>5</sup></li>
46+
</ul>
47+
48+
<hr>
49+
50+
<h5>입출력 예</h5>
51+
<table class="table">
52+
<thead><tr>
53+
<th>n</th>
54+
<th>left</th>
55+
<th>right</th>
56+
<th>result</th>
57+
</tr>
58+
</thead>
59+
<tbody><tr>
60+
<td>3</td>
61+
<td>2</td>
62+
<td>5</td>
63+
<td><code>[3,2,2,3]</code></td>
64+
</tr>
65+
<tr>
66+
<td>4</td>
67+
<td>7</td>
68+
<td>14</td>
69+
<td><code>[4,3,3,3,4,4,4,4]</code></td>
70+
</tr>
71+
</tbody>
72+
</table>
73+
<hr>
74+
75+
<h5>입출력 예 설명</h5>
76+
77+
<p><strong>입출력 예 #1</strong></p>
78+
79+
<ul>
80+
<li>다음 애니메이션은 주어진 과정대로 1차원 배열을 만드는 과정을 나타낸 것입니다.</li>
81+
</ul>
82+
83+
<p><img src="https://grepp-programmers.s3.amazonaws.com/production/file_resource/103/FlattenedFills_ex1.gif" title="" alt="ex1"></p>
84+
85+
<p><strong>입출력 예 #2</strong></p>
86+
87+
<ul>
88+
<li>다음 애니메이션은 주어진 과정대로 1차원 배열을 만드는 과정을 나타낸 것입니다.</li>
89+
</ul>
90+
91+
<p><img src="https://grepp-programmers.s3.amazonaws.com/production/file_resource/104/FlattenedFills_ex2.gif" title="" alt="ex2"></p>
92+
93+
94+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <string>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
vector<int> solution(int n, long long left, long long right) {
7+
vector<int> answer;
8+
9+
long long curr = left;
10+
11+
// 1234 2234 3334 4444
12+
13+
while(curr <= right){
14+
15+
long long row = curr / n;
16+
long long col = curr % n;
17+
18+
int value;
19+
20+
if(col <= row){
21+
value = row + 1;
22+
}
23+
else value = col + 1;
24+
25+
answer.push_back(value);
26+
curr++;
27+
}
28+
29+
return answer;
30+
}

0 commit comments

Comments
 (0)