시간초과 난 풀이
원인: n의 범위가 1~10^7이다. 그리고 left와 right의 범위는 0~n^2까지이다.
n이 10^7일 경우, left와 right는 정수범위를 넘는 수를 가질 수 있다.
따라서 int 자료형을 쓴다면 시간초과 혹은 오답처리가 난다.
따라서 int 자료형 대신 long long을 사용하여 left와, right를 다뤄야 한다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, long long left, long long right) {
vector<int> answer;
int number = 0;
int row = 0;
int col = 0;
for(int i=left; i<=right; i++){
row = i / n + 1;
col = i % n + 1;
number = row > col ? row : col;
answer.push_back(number);
}
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; i<=right; i++){
long long row = i / n + 1;
long long col = i % n + 1;
answer.push_back( row > col ? row : col);
}
return answer;
}
'🎯 Algorithm > 🕊️ programmers' 카테고리의 다른 글
| [프로그래머스, C++] 2016년 (나보다 간결한 풀이) (0) | 2024.02.03 |
|---|---|
| [프로그래머스,C++] H-Index (알고리즘 깔끔하게 안 세움, 기존 테케가가 잡아주지 않음) (0) | 2024.02.03 |
| [프로그래머스, C++] 연속 부분 수열 합의 개수 (set 사용, 효율성 for문) (0) | 2024.02.01 |
| [프로그래머스, C++] 추억 점수 (map 사용 까먹음) (0) | 2024.01.29 |
| [프로그래머스, C++] 멀리 뛰기 (DP, 점화식) (1) | 2024.01.26 |