Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
1. 아이디어: 풀이법을 고민하다가 AI에게 질문
핵심은 1부터 k크기의 모든 배열에서 존재하지 않는 배열의 최소길이를 구하기

이때 모든 경우의 수는 구할수 없음 (최대 사이즈 10000)

그렇다면 모든 배열이 존재 가능한 조건을 판단
1 ~ k 개의 원소가 모두 존재하는 범위를 기준으로 배열을 나누면 최소 길이 판단 가능

ex) 1 1 3 3 2 || 1 3 k = 3
위 나눠진 경우를 보면 완전한 배열 1개와 숫자가 1개 부족한 배열 존재

완성된 구간에서는 모든 숫자 중 하나를 선택할 수 있고,
마지막 미완성 구간에는 반드시 없는 숫자가 존재한다.

2. 시간복잡도: O(N)

3. 자료구조/알고리즘: 그리디
*/

class Solution {
public int shortestSequence(int[] rolls, int k) {
// k면체 주사위 n번 굴리기

int ans = 0;
int sum = 0;

boolean[] check = new boolean[k+1];

for(int i=0; i<rolls.length; i++) {
int nums = rolls[i];

if(check[nums]) continue;

check[nums] = true;

sum++;

if(sum == k) {
ans++;
sum = 0;
check = new boolean[k+1];
}
}

return ans+1;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*

1. 아이디어 : 각 구간의 시작점과 끝점이 주어졌을때, 구간별 겹치는 개수 파악하기
두 구간이 겹치는 경우의 수는 포함과 일부 겹침 두개가 존재하므로, 겹치지 않는 경우로 체크
모든 배열을 완전탐색해서 구간 a의 end가 구간 b의 start보다 작거나 반대 경우만 제외

2. 시간복잡도 : O(N^2)

3. 자료구조/알고리즘 : 완전탐색

*/

class Solution {
public int countIntersectingIntervals(int[][] intervals) {

int len = intervals.length;
int ans = 0;

for(int i=0; i<len-1; i++) {
for(int j=i+1; j<len; j++) {
if(intervals[i][1] < intervals[j][0] || intervals[j][1] < intervals[i][0]) continue;
ans++;
}
}

return ans;
}
}