Can Place Flowers LT605

Suppose 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 - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

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


Note:

    1. The input array won't violate no-adjacent-flowers rule.
    2. The input array size is in the range of [1, 20000].
    3. n is a non-negative integer which won't exceed the input array size.

 Idea1. Count zeros, if there are K zeros between two 1s, it's (K-1)/2; if it's on the edge to the left or to the right, K/2, there is one 1; if no 1 at all, it's (K+1)/2;

Time complexity: O(N)

Space complexity: O(1)

 1 class Solution {
 2     public boolean canPlaceFlowers(int[] flowerbed, int n) {
 3         int prev = -1;
 4         int zeros = 0;
 5         int result = 0;
 6         for(int num: flowerbed) {
 7             if(num == 0) {
 8                 ++zeros;
 9             }
10             else {
11                 int k = 0;
12                 if(prev == -1) {
13                     k = zeros/2;
14                 } 
15                 else {
16                     k = (zeros-1)/2;
17                 }
18                 result += k;
19                 zeros = 0;
20                 prev = num;
21             }
22         }
23         if(prev == -1 ) {
24             result += (zeros +1)/2;
25         }
26         else {
27             result += zeros/2;
28         }
29         
30         return result >= n;
31     }
32 }

Idea 1.b smart idea by setting count = 1;

 1 class Solution {
 2     public boolean canPlaceFlowers(int[] flowerbed, int n) {
 3         int zeros = 1;
 4         int result = 0;
 5         for(int i = 0; i < flowerbed.length; ++i) {
 6             if(flowerbed[i] == 0) {
 7                 ++zeros;
 8             } 
 9             else {
10                 result += (zeros-1)/2;
11                 zeros = 0;
12             }
13         }
14         
15         if(zeros > 0) {
16             result += zeros/2;
17         }
18         
19         return result >= n;
20     }
21 }

 

Idea 2. Simpler logic, check each zero if there is zeros ajacent to it, note the first element flowerbed[0] = 0 or the last element flowerbed[n-1] = 0.

Time complexity: O(N)

Space complexity: O(1)

 1 class Solution {
 2     public boolean canPlaceFlowers(int[] flowerbed, int n) {
 3         int prev = 0;
 4         int result = 0;
 5         for(int i = 0; i < flowerbed.length; ++i) {
 6             if(prev == 0 && flowerbed[i] == 0 && (i == flowerbed.length-1 || flowerbed[i+1] == 0)) {
 7                 ++result;
 8                 prev = 1;
 9             }
10             else {
11                 prev = flowerbed[i];
12             }
13         }
14         return result >= n;
15     }
16 }

Idea 2.b slightly optimise it, terminate the loop earlier if result == n reaches earlier.

Note check the result (result >= n) outside of the ++result loop, there might cases this loop is not executed, for example if n == 0, it would return false as ++result does not get executed.

 1 class Solution {
 2     public boolean canPlaceFlowers(int[] flowerbed, int n) {
 3         int prev = 0;
 4         int result = 0;
 5         for(int i = 0; i < flowerbed.length; ++i) {
 6             if(prev == 0 && flowerbed[i] == 0 && (i == flowerbed.length-1 || flowerbed[i+1] == 0)) {
 7                 ++result;
 8                 prev = 1;    
 9             }
10             else {
11                 prev = flowerbed[i];
12             }
13             
14             if(result >= n) {
15                     return true;
16             }
17         }
18         return false;
19     }
20 }

 

posted on 2019-05-10 10:07  一直走在路上  阅读(106)  评论(0)    收藏  举报

导航