Created
June 5, 2017 18:32
-
-
Save jianminchen/5944fd8b3f2e06273c3232296eaa54ab to your computer and use it in GitHub Desktop.
Leetcode 605 - can plant flower - short code, focus on basics - go over each element once, check left, right neighbors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PlantFlower | |
{ | |
/// <summary> | |
/// Leetcode 506 - Can Place flowers | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
/// <summary> | |
/// iterate the array, add first element with virtual left neigbhor with 0, and | |
/// last element with virtual right neighbor with 0. | |
/// The algorithm is to learn how to iterate the array, and work on previous, current | |
/// and next 3 elements in the same time. | |
/// </summary> | |
/// <param name="flowerbed"></param> | |
/// <param name="n"></param> | |
/// <returns></returns> | |
public static bool CanPlaceFlowers(int[] flowerbed, int n) | |
{ | |
int count = 0; | |
int length = flowerbed.Length; | |
for (int i = 0; i < length && count < n; i++) | |
{ | |
if (flowerbed[i] == 0) | |
{ | |
// get next and prev flower bed slot values. If i lies at the ends the next and | |
// prev are considered as 0. | |
int next = (i == length - 1) ? 0 : flowerbed[i + 1]; | |
int previous = (i == 0) ? 0 : flowerbed[i - 1]; | |
if (next == 0 && previous == 0) | |
{ | |
flowerbed[i] = 1; | |
count++; | |
} | |
} | |
} | |
return count == n; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment