CanPlaceFlowersOPT [source code]

public class CanPlaceFlowersOPT {
static
/******************************************************************************/
public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {        
        int empty = 0;        
        for (int i = 0; i < flowerbed.length; i++) {
            if ((i - 1 < 0 || flowerbed[i - 1] == 0) && flowerbed[i] == 0 &&
             (i + 1 >= flowerbed.length || flowerbed[i + 1] == 0)) {
                flowerbed[i] = 1;
                empty++;
            }
        }        
        return empty >= n;
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        CanPlaceFlowersOPT.Solution tester = new CanPlaceFlowersOPT.Solution();
        int[][] inputs = {
            {1,0,0,0,0,1}, {2}, {0},
            {1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1}, {7}, {0},
            {1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1}, {5}, {1},
            {1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1}, {6}, {1},
            {0,0,1,0,1}, {1}, {1},
            {1,0,0,0,1}, {1}, {1},
        };
        for (int i = 0; i < inputs.length / 3; i++) {
            int[] flowerbed = inputs[3 * i];
            int n = inputs[1 + 3 * i][0];
            boolean expected = inputs[2 + 3 * i][0] == 1;
            Printer.openColor("magenta");
            System.out.print(Printer.array(flowerbed) + " AND " + n);
            Printer.closeColor();
            boolean res = tester.canPlaceFlowers(flowerbed, n);
            System.out.print(" -> " + res);
            Printer.openColor(res == expected ? "green" : "red");
            System.out.println(", expected : " + expected);
        }
    }
}

submission 最优解: 12(80);

这个代码比我的ver2要简练很多, 他这里没有从分段这种模板思路去考虑, 而是单独针对这个问题: 就是找000的组合, 只要找到a zero that is surrounded on both sides by other zeros(或者头尾的特殊情况), 那么我们可以摆放的位置就多了一个; 这个逻辑其实确实是比较直接的;


editorial里面对此提出一个小的优化: 加上一个premature exit:

public class Solution {  
    public boolean canPlaceFlowers(int[] flowerbed, int n) {  
        int i = 0, count = 0;  
        while (i < flowerbed.length) {  
            if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {  
                flowerbed[i++] = 1;  
                count++;  
            }  
             if(count>=n)  
                return true;  
            i++;  
        }  
        return false;  
    }  
}

Problem Description

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:
The input array won't violate no-adjacent-flowers rule.
The input array size is in the range of [1, 20000].
n is a non-negative integer which won't exceed the input array size.
Difficulty:Easy
Total Accepted:9.3K
Total Submissions:31.1K
Contributor: fallcreek
Companies
linkedin
Related Topics
array
Similar Questions
Teemo Attacking

results matching ""

    No results matching ""