티스토리 뷰
You are given an integer array bloomDay, an integer m and an integer k.
You want to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1
문제해석 및 요약 : 날짜 배열이 주어질 때 해당 날짜가 지난 후에 꽃이 생긴다. m개의 부케를 만드는데 인접한 꽃을 가지고 k개만큼 묶어서 부케를 만들어라. 그 때 최소날짜는??
예시
bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
7일 일 때
0,0,0,0,x,O,O -----> 이렇게 꽃이 생기는데 인접한 꽃을 가지고 3개씩 묶음을 가지고 만들어야 해서 12일 칸에는 꽃이 없어 부캐를 최대 1개 밖에 못 만듬
https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets/
문제해결 KEY POINT
- bloomDay의 min 값과 max 값 사이에 정답이 있다
- 이진탐색을 이용해서 최솟값을 구해보자
정답코드
import java.util.*;
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
int N = bloomDay.length;
if(N < m*k) return -1;
int min = 1000000010;
int max = 0;
for(int i=0; i<bloomDay.length; i++){
min = Math.min(min, bloomDay[i]);
max = Math.max(max, bloomDay[i]);
}
int s = min;
int e = max;
int ans = 0;
while(s <= e){
int mid = (s+e)/2;
int cnt = 0;
int tmp = 0;
for(int i=0; i<N; i++){
if(mid >= bloomDay[i]){
tmp++;
if(tmp == k) {
cnt++;
tmp = 0;
}
}
else
tmp = 0;
}
if(cnt >= m){
e = mid - 1;
ans = mid;
}
else
s = mid + 1;
}
if(ans == 0)
return -1;
else
return ans;
}
}
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 2563 Count the Number of Fair Pairs - 이진탐색, 투포인터 (0) | 2024.11.13 |
---|---|
[LeetCode] 2070 Most Beautiful Item for Each Query - 이진탐색 (1) | 2024.11.12 |
[LeetCode] 2601 Prime Subtraction Operation (0) | 2024.11.11 |
[LeetCode] 1598 Crawler Log Folder (0) | 2024.07.10 |
LeetCode 1552 Magnetic Force Between Two Balls (0) | 2024.06.20 |
- Total
- Today
- Yesterday
- #스프링부트 자바버전 오류
- BFS
- 백준 경사로 자바
- 이진탐색
- 티스토리챌린지
- sql
- 위상정렬
- 투포인터
- 백준 14890 경사로
- 스프링부트3 java 버전오류
- 1482
- 백준 경사로
- 스프링부트3
- 구현
- 조합
- 슬라이딩윈도우
- 백준
- 바텀업
- 탑다운
- 스프링부트3 자바 17 오류
- 백준 14890
- 탑다운dp
- leetcode 1552
- 백준 14890 자바
- 누적합
- 유니온파인드
- 스프링부트3 자바 버전
- dp
- dfs
- 오블완
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |