Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- daily challenge
- 자바 5464
- leetcode 1721
- 구현
- 백준 18222
- 백준 16935
- DP
- java leetcode
- 코딩테스트
- 인텔리제이 에러
- java 프로그래머스
- 자바
- 스프링 에러
- 프로그래머스 java
- 프로그래머스
- leetcode
- dfs
- 스택
- 자바 리트코드
- 분할정복
- 리트코드 1557
- 백준
- 코테
- 리트코드
- 파이썬
- Java
- 그래프 자바
- BFS
- 카카오
- 리트코드 자바
Archives
- Today
- Total
레벨업 일지
[Java] leetcode 352. Data Stream as Disjoint Intervals 본문
문제
https://leetcode.com/problems/data-stream-as-disjoint-intervals/description/
Data Stream as Disjoint Intervals - LeetCode
Data Stream as Disjoint Intervals - Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals. Implement the SummaryRanges class: * SummaryRanges() Initializes the object with an e
leetcode.com
주어진 조건에 맞추어 메소드를 구현하는 문제
알아야 할 개념
- 이진 탐색
풀이
풀이는 다음과 같다.
SummaryRanges()
- 리스트 전역변수 arr 를 초기화 한다.
void addNum(int value)
- 이진 검색을 구현해서, arr 리스트를 탐색한다. arr.contains(value) 일 경우 -1 을 리턴 (중복 제거 )
- 그 이외에는 삽입할 위치를 리턴한다.
int[][] getIntervals()
- arr 리스트를 탐색을 하여 prev 보다 1 증가할 경우에만 업데이트를 해준다.
- 그 이외에는 새로운 리스트를 추가한다.
코드
자바
class SummaryRanges {
ArrayList<Integer> arr;
public SummaryRanges() {
arr = new ArrayList<>();
}
public void addNum(int value) {
int i = bnS(arr, value);
if(i == -1)return;
if(arr.size() <= i)arr.add(value);
else{
arr.add(i,value);
}
}
public int bnS(List<Integer> l, int v){
int left = 0;
int right = l.size()-1;
while(left <= right){
int mid = left + (right - left)/2;
if(l.get(mid) == v)return -1;
else if(l.get(mid) < v)left = mid+1;
else right = mid-1;
}
//System.out.println(left);
return left;
}
public int[][] getIntervals() {
ArrayList<ArrayList<Integer>> temp = new ArrayList<>();
int prev =-1;
int cnt = 0;
//System.out.println(arr);
for(int i = 0 ;i < arr.size() ;i++){
if(i > 0 && arr.get(i) == arr.get(i-1))continue;
if(prev ==-1){
prev = arr.get(i);
temp.add(new ArrayList<Integer>());
temp.get(cnt).add(prev);
temp.get(cnt).add(prev);
}else{
if(arr.get(i)-1 == prev){
temp.get(cnt).set(1,arr.get(i));
prev = arr.get(i);
}else {
cnt++;
prev = arr.get(i);
temp.add(new ArrayList<Integer>());
temp.get(cnt).add(prev);
temp.get(cnt).add(prev);
}
}
}
int[][] ret = new int[temp.size()][2];
for(int i =0 ;i < temp.size() ;i++){
ret[i] = temp.get(i).stream().mapToInt(x->x).toArray();
}
return ret;
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(value);
* int[][] param_2 = obj.getIntervals();
*/
'알고리즘 > leetcode' 카테고리의 다른 글
[Java] leetcode 1626. Best Team With No Conflicts (0) | 2023.02.01 |
---|---|
[Java/Python] leetcode 1137. N-th Tribonacci Number (0) | 2023.01.31 |
[Java] leetcode 787. Cheapest Flights Within K Stops (0) | 2023.01.27 |
[Java] leetcode 909. Snakes and Ladders (0) | 2023.01.25 |
[Java] leetcode 815. Bus Routes (2) | 2023.01.23 |
Comments