Skip to content

Commit 88a33f1

Browse files
committed
2350. Shortest Impossible Sequence of Rolls
1 parent a5aa6c2 commit 88a33f1

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
1. 아이디어: 풀이법을 고민하다가 AI에게 질문
3+
핵심은 1부터 k크기의 모든 배열에서 존재하지 않는 배열의 최소길이를 구하기
4+
5+
이때 모든 경우의 수는 구할수 없음 (최대 사이즈 10000)
6+
7+
그렇다면 모든 배열이 존재 가능한 조건을 판단
8+
1 ~ k 개의 원소가 모두 존재하는 범위를 기준으로 배열을 나누면 최소 길이 판단 가능
9+
10+
ex) 1 1 3 3 2 || 1 3 k = 3
11+
위 나눠진 경우를 보면 완전한 배열 1개와 숫자가 1개 부족한 배열 존재
12+
13+
완성된 구간에서는 모든 숫자 중 하나를 선택할 수 있고,
14+
마지막 미완성 구간에는 반드시 없는 숫자가 존재한다.
15+
16+
2. 시간복잡도: O(N)
17+
18+
3. 자료구조/알고리즘: 그리디
19+
*/
20+
21+
class Solution {
22+
public int shortestSequence(int[] rolls, int k) {
23+
// k면체 주사위 n번 굴리기
24+
25+
int ans = 0;
26+
int sum = 0;
27+
28+
boolean[] check = new boolean[k+1];
29+
30+
for(int i=0; i<rolls.length; i++) {
31+
int nums = rolls[i];
32+
33+
if(check[nums]) continue;
34+
35+
check[nums] = true;
36+
37+
sum++;
38+
39+
if(sum == k) {
40+
ans++;
41+
sum = 0;
42+
check = new boolean[k+1];
43+
}
44+
}
45+
46+
return ans+1;
47+
48+
}
49+
}

0 commit comments

Comments
 (0)