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
- 스프링부트핵심가이드
- 서버설정
- /etc/network/interfaces
- 자료구조와함께배우는알고리즘입문
- 티스토리 쿠키 삭제
- 처음 만나는 AI수학 with Python
- 페이징
- 선형대수
- GIT
- 알파회계
- 리눅스
- d
- 이터레이터
- 친절한SQL튜닝
- 코드로배우는스프링부트웹프로젝트
- 자료구조와 함께 배우는 알고리즘 입문
- 구멍가게코딩단
- 자바편
- resttemplate
- 코드로배우는스프링웹프로젝트
- 네트워크 설정
- 데비안
- 처음 만나는 AI 수학 with Python
- 목록처리
- network configuration
- baeldung
- 스프링 시큐리티
- Kernighan의 C언어 프로그래밍
- iterator
- ㅒ
Archives
- Today
- Total
bright jazz music
605. Can place flowers 본문
605. Can Place Flowers
Solved
Easy
Topics
conpanies icon
Companies
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints:
1 <= flowerbed.length <= 2 * 104
flowerbed[i] is 0 or 1.
There are no two adjacent flowers in flowerbed.
0 <= n <= flowerbed.length
풀이
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
// 인접하게 심으면 안됨
// 양 옆에 0이여야 함. 1이 있으면 안됨
// 순회하면서 0인 원소를 찾음.
// 양 옆에 1이 있는지 확인
// 있으면 다음 원소로
// 없으면 1로 변경
// 순회
for(let i = 0; i < flowerbed.length; i++) {
if(flowerbed[i] === 0 ) {
// if(flowerbed[i-1] !== 1 || flowerbed[i+1] !== 1) {
if(flowerbed[i-1] !== 1 && flowerbed[i+1] !== 1) {
flowerbed[i] = 1;
n--;
}
}
}
// if(n === 0) return true;
// return false;
/*
greedy하게 심을 수 있는 곳은 다 심기 때문에, 필요한 것보다 더 많이 심으면 n이 음수가 됨.
따라서 n <= 0으로 체크해야 "요구량 이상을 심었다"는 것을 올바르게 판단할 수 있음
*/
return n <= 0;
};
Comments
