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 |
Tags
- 처음 만나는 AI 수학 with Python
- network configuration
- ㅒ
- 처음 만나는 AI수학 with Python
- 구멍가게코딩단
- Kernighan의 C언어 프로그래밍
- 스프링부트핵심가이드
- d
- 스프링 시큐리티
- 데비안
- 목록처리
- 선형대수
- /etc/network/interfaces
- 리눅스
- 친절한SQL튜닝
- 자료구조와 함께 배우는 알고리즘 입문
- 코드로배우는스프링웹프로젝트
- 코드로배우는스프링부트웹프로젝트
- iterator
- 알파회계
- 페이징
- baeldung
- 자바편
- 티스토리 쿠키 삭제
- 네트워크 설정
- 자료구조와함께배우는알고리즘입문
- GIT
- resttemplate
- 서버설정
- 이터레이터
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;
};'Algorithm Practice > LeetCode' 카테고리의 다른 글
| 151. Reverse Words in a String (0) | 2026.01.15 |
|---|---|
| 345. Reverse Vowels of a String (0) | 2026.01.14 |
| 1. Two Sum (1) | 2022.11.27 |
| 183. Customers Who Never Order (0) | 2022.11.26 |
| 584. Find Customer Referee (0) | 2022.11.26 |
Comments