CanPlaceFlowers2 [source code]

public class CanPlaceFlowers2 {
static
/******************************************************************************/
public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {        
        int sum = 0, run = 0, len = flowerbed.length;
        boolean inRun = true;
        for (int i = 0; i < len; i++) {
            if (inRun && flowerbed[i] == 1) {
                sum += run - 2 <= 0 ? 0 : ((run - 2 - 1) / 2 + 1);
                run = 0;
            } else if (!inRun && flowerbed[i] == 0) {
                run++;
                inRun = true;
            } else if (inRun && flowerbed[i] == 0) {
                run++;
                if (i == 0) run++;
                if (i == len - 1) {
                    run++;
                    sum += run - 2 <= 0 ? 0 : ((run - 2 - 1) / 2 + 1);
                }
            } 
        }
        return n <= sum;
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        CanPlaceFlowers2.Solution tester = new CanPlaceFlowers2.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},
            {1,0,0,0,1,0,0}, {2}, {1},
            {0}, {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);
        }
    }
}

这个是自己尝试不用 string 的思路, 纯粹用 array 的 run(paragraph) 思路来做一个.

分段算法常见的需要考虑的几个 branch:

  1. 上边沿;
  2. 下边沿;
  3. 整个的头和尾巴;
  4. 在 run 内(需要更新run 的长度. 一般 run 外非边沿的位置基本可以不管的);

这个算法也做到了12(80)的最优解, 所以看来还是 string 的问题;
其实无论是用不用 string 的这两个版本, 最后都有一个 branch 不算特别好处理; 这里第三个 branch 比较难处理的是对应{0}的这个 case, 这时候0 is not only head but also the tail, 这个时候这个0应该获得两个额外的++; 你就要看怎么处理. 这里最好的处理方法是用conditional filter的思路来处理, 而不是用 elif chain的 branch 思路来处理. 这样写更简洁一些.
string 算法我记得也是有一点小窍门在里面的;

关于刷题的事情, 以后碰到这种题目, 有两个思路, 还是要坚持两个思路都写一下, 可以看到这两个算法其实都挺锻炼人的;

不过我这个代码好像比opt1那个人的要verbose很多; 但是我觉得我这个代码的可复制性还是比较强的, 分段算法是我的一个模板类的算法了;


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 ""