[Programmers] #181836 - 그림 확대 [Java][C++][Python]
[Programmers] #181836 - 그림 확대 [Java][C++][Python]
1. 아이디어
직사각형 형태의 그림 파일을 가로 세로 k배 늘린 그림 파일로 반환하는 문제로 가로로 k배를 늘리는 것은 각 문자를 k번 반복하면 되며, 세로로 k배 늘리는 것은 같은 행을 k번 반복하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N \times M \times K)$ | $O(N \times M \times K)$ |
($N$ = picture의 길이, $M$ = 각 원소의 길이, $K$ = 확대 배수. C++은 push_back(s)로 확대된 한 행을 k번 복사해 담으므로 시간·공간 모두 $O(N \times M \times K^2)$)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public String[] solution(String[] picture, int k) {
String[] arr = new String[picture.length * k];
int idx = 0;
for (String s : picture) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
sb.repeat(c, k);
}
String res = sb.toString();
for (int j = 0; j < k; j++) {
arr[idx++] = res;
}
}
return arr;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
vector<string> solution(vector<string> picture, int k) {
vector<string> v;
for (string& row : picture) {
string s;
for (char c : row) {
s += string(k, c);
}
for (int j = 0; j < k; j++) {
v.push_back(s);
}
}
return v;
}
1
2
3
4
5
6
7
def solution(picture, k):
res = []
for row in picture:
line = "".join(c * k for c in row)
res.extend([line] * k)
return res
This post is licensed under CC BY 4.0 by the author.