Skip to content

Commit 63019ac

Browse files
committed
[level 3] Title: 산 모양 타일링, Time: 13.45 ms, Memory: 22.5 MB -BaekjoonHub
1 parent 138c104 commit 63019ac

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# [level 3] 산 모양 타일링 - 258705
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/258705)
4+
5+
### 성능 요약
6+
7+
메모리: 22.5 MB, 시간: 13.45 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2024 KAKAO WINTER INTERNSHIP
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 01월 23일 22:35:38
20+
21+
### 문제 설명
22+
23+
<p>한 변의 길이가 1인 정삼각형 <code>2n+1</code>개를 이어붙여 윗변의 길이가 <code>n</code>, 아랫변의 길이가 <code>n+1</code>인 사다리꼴을 만들 수 있습니다. 이때 사다리꼴의 윗변과 변을 공유하는 <code>n</code>개의 정삼각형 중 일부의 위쪽에 같은 크기의 정삼각형을 붙여 새로운 모양을 만들었습니다. 예를 들어 <code>n</code>이 4이고, 1번째, 2번째, 4번째 정삼각형 위에 정삼각형을 붙인 모양은 다음과 같습니다.</p>
24+
25+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/b1eb2bdf-c4a8-4750-8a0d-1fe1a304ccaf/pic1.png" title="" alt="pic1.png"></p>
26+
27+
<p>이렇게 만든 모양을 <strong>정삼각형 타일</strong> 또는 정삼각형 2개를 이어 붙인 <strong>마름모 타일</strong>로 빈 곳이 없도록 채우려고 합니다. 정삼각형 타일과 마름모 타일은 돌려서 사용할 수 있습니다.</p>
28+
29+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/f145dc29-ce8b-4077-ad71-9442b1c4434f/pic2.png" title="" alt="pic2.png"></p>
30+
31+
<p>타일을 놓을 때 다른 타일과 겹치거나 모양을 벗어나게 놓을 수는 없습니다. 위의 예시 모양을 채우는 방법 중 일부는 다음과 같습니다.</p>
32+
33+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/89dbb27a-f939-4b71-abd9-3de304f67c4e/pic3.png" title="" alt="pic3.png"></p>
34+
35+
<p>사다리꼴의 윗변의 길이를 나타내는 정수 <code>n</code>과 사다리꼴 윗변에 붙인 정삼각형을 나타내는 1차원 정수 배열 <code>tops</code>가 매개변수로 주어집니다. 이때 문제 설명에 따라 만든 모양을 정삼각형 또는 마름모 타일로 빈 곳이 없도록 채우는 경우의 수를 <code>10007</code>로 나눈 나머지를 return 하도록 solution 함수를 완성해 주세요.</p>
36+
37+
<hr>
38+
39+
<h5>제한사항</h5>
40+
41+
<ul>
42+
<li>1 ≤ <code>n</code> ≤ 100,000</li>
43+
<li><code>tops</code>의 길이 = <code>n</code>
44+
45+
<ul>
46+
<li><code>tops[i]</code>는 사다리꼴의 윗변과 변을 공유하는 <code>i+1</code>번째 정삼각형의 위쪽에 정삼각형을 붙이는 경우 1, 붙이지 않는 경우 0입니다.</li>
47+
</ul></li>
48+
</ul>
49+
50+
<hr>
51+
52+
<h5>입출력 예</h5>
53+
<table class="table">
54+
<thead><tr>
55+
<th>n</th>
56+
<th>tops</th>
57+
<th>result</th>
58+
</tr>
59+
</thead>
60+
<tbody><tr>
61+
<td>4</td>
62+
<td>[1, 1, 0, 1]</td>
63+
<td>149</td>
64+
</tr>
65+
<tr>
66+
<td>2</td>
67+
<td>[0, 1]</td>
68+
<td>11</td>
69+
</tr>
70+
<tr>
71+
<td>10</td>
72+
<td>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]</td>
73+
<td>7704</td>
74+
</tr>
75+
</tbody>
76+
</table>
77+
<hr>
78+
79+
<h5>입출력 예 설명</h5>
80+
81+
<p><strong>입출력 예 #1</strong></p>
82+
83+
<p>문제의 예시와 같습니다. 문제에서 설명한 방법을 포함해 총 149가지 방법이 존재합니다.</p>
84+
85+
<p>따라서 149를 return 해야 합니다.</p>
86+
87+
<p><strong>입출력 예 #2</strong></p>
88+
89+
<p>문제 설명에 따라 만든 모양은 다음과 같습니다.</p>
90+
91+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/d887399e-2335-4a99-8c3b-af0d71e595bd/pic4.png" title="" alt="pic4.png"></p>
92+
93+
<p>이 모양을 타일로 채우는 방법은 다음과 같이 총 11가지입니다.</p>
94+
95+
<p><img src="https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/aad9c7de-087c-4a6c-9b02-8c6712a42f69/pic5.png" title="" alt="pic5.png"></p>
96+
97+
<p>따라서 11을 return 해야 합니다.</p>
98+
99+
<p><strong>입출력 예 #3</strong></p>
100+
101+
<p>경우의 수는 총 17,711가지입니다. 따라서 17711을 10007로 나눈 나머지인 7704를 return 해야 합니다.</p>
102+
103+
104+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Foundation
2+
3+
func solution(_ n:Int, _ tops:[Int]) -> Int {
4+
let totalCount = (2*n)+1
5+
var dp = [Int](repeating: 0, count: totalCount+1)
6+
dp[1] = 1
7+
dp[2] = 2 + tops[0]
8+
for i in 3...totalCount {
9+
if i%2 == 0 {
10+
dp[i] = (dp[i-1]*(1+tops[(i/2)-1]))%10007 + dp[i-2]%10007
11+
} else {
12+
dp[i] = dp[i-2]%10007 + dp[i-1]%10007
13+
}
14+
dp[i] %= 10007
15+
}
16+
return dp[totalCount]
17+
}

0 commit comments

Comments
 (0)