정수 n, left, right가 주어집니다. 다음 과정을 거쳐서 1차원 배열을 만들고자 합니다.
n행 n열 크기의 비어있는 2차원 배열을 만듭니다.
i = 1, 2, 3, ..., n에 대해서, 다음 과정을 반복합니다.
1행 1열부터 i행 i열까지의 영역 내의 모든 빈 칸을 숫자 i로 채웁니다.
1행, 2행, ..., n행을 잘라내어 모두 이어붙인 새로운 1차원 배열을 만듭니다.
새로운 1차원 배열을 arr이라 할 때, arr[left], arr[left+1], ..., arr[right]만 남기고 나머지는 지웁니다.
정수 n, left, right가 매개변수로 주어집니다. 주어진 과정대로 만들어진 1차원 배열을 return 하도록 solution 함수를 완성해주세요.
제한사항
1 ≤ n ≤ 10^7
0 ≤ left ≤ right < n^2
right - left < 10^5
#include <string>
#include <vector>
using namespace std;
vector<int> two_dim_arr(int n, int left, int right){
vector<vector<int>> two(n, vector<int>(n));
vector<int> answers;
int count = 0;
for(int i = 0; i < n; i++){
int tuple = i + 1;
for(int j = 0; j < n; j++){
int cardi = j + 1;
if(count >= left && count <= right){
if(tuple >= cardi){
answers.push_back(tuple);
}else{
answers.push_back(cardi);
}
}
count++;
}
}
return answers;
}
vector<int> solution(int n, long long left, long long right) {
vector<int> answer;
answer = two_dim_arr(n, left, right);
return answer;
}
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, long long left, long long right) {
vector<int> answer;
for(long long i = left / n; i <= right / n; i++) {
for(long long j = (i == left / n) ? left % n : 0; j <= (i == right / n ? right % n : n-1); j++) {
answer.push_back(max(i+1, j+1));
}
}
return answer;
}