File tree 7 files changed +161
-0
lines changed
7 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def canPlaceFlowers (self , flowerbed : List [int ], n : int ) -> bool :
3
+ indx = 0
4
+ counter = 0
5
+ while indx < len (flowerbed ) :
6
+ if flowerbed [indx ] :
7
+ indx += 2
8
+ elif indx and flowerbed [indx - 1 ] :
9
+ indx += 1
10
+ elif indx < len (flowerbed ) - 1 and flowerbed [indx + 1 ] :
11
+ indx += 3
12
+ else :
13
+ counter += 1
14
+ indx += 2
15
+
16
+ if counter >= n :
17
+ return True
18
+
19
+ return False
Original file line number Diff line number Diff line change
1
+ bool canPlaceFlowers (int * flowerbed , int flowerbedSize , int n ) {
2
+ int indx = 0 ;
3
+ int counter = 0 ;
4
+ while (indx < flowerbedSize ) {
5
+ if (flowerbed [indx ] == 1 )
6
+ indx += 2 ;
7
+ else if (indx > 0 && flowerbed [indx - 1 ] == 1 )
8
+ indx += 1 ;
9
+ else if (indx < flowerbedSize - 1 && flowerbed [indx + 1 ] == 1 )
10
+ indx += 3 ;
11
+ else {
12
+ counter ++ ;
13
+ indx += 2 ;
14
+ }
15
+
16
+ if (counter >= n )
17
+ return true;
18
+ }
19
+
20
+ return false;
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool canPlaceFlowers (vector<int >& flowerbed, int n) {
4
+ int indx = 0 ;
5
+ int counter = 0 ;
6
+ while (indx < flowerbed.size ()) {
7
+ if (flowerbed[indx] == 1 )
8
+ indx += 2 ;
9
+ else if (indx > 0 && flowerbed[indx - 1 ] == 1 )
10
+ indx += 1 ;
11
+ else if (indx < flowerbed.size () - 1 && flowerbed[indx + 1 ] == 1 )
12
+ indx += 3 ;
13
+ else {
14
+ counter++;
15
+ indx += 2 ;
16
+ }
17
+
18
+ if (counter >= n)
19
+ return true ;
20
+ }
21
+
22
+ return false ;
23
+ }
24
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean canPlaceFlowers (int [] flowerbed , int n ) {
3
+ int indx = 0 ;
4
+ int counter = 0 ;
5
+ while (indx < flowerbed .length ) {
6
+ if (flowerbed [indx ] == 1 )
7
+ indx += 2 ;
8
+ else if (indx > 0 && flowerbed [indx - 1 ] == 1 )
9
+ indx += 1 ;
10
+ else if (indx < flowerbed .length - 1 && flowerbed [indx + 1 ] == 1 )
11
+ indx += 3 ;
12
+ else {
13
+ counter ++;
14
+ indx += 2 ;
15
+ }
16
+
17
+ if (counter >= n )
18
+ return true ;
19
+ }
20
+
21
+ return false ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } flowerbed
3
+ * @param {number } n
4
+ * @return {boolean }
5
+ */
6
+ var canPlaceFlowers = function ( flowerbed , n ) {
7
+ let indx = 0 ;
8
+ let counter = 0 ;
9
+ while ( indx < flowerbed . length ) {
10
+ if ( flowerbed [ indx ] == 1 )
11
+ indx += 2 ;
12
+ else if ( indx > 0 && flowerbed [ indx - 1 ] == 1 )
13
+ indx += 1 ;
14
+ else if ( indx < flowerbed . length - 1 && flowerbed [ indx + 1 ] == 1 )
15
+ indx += 3 ;
16
+ else {
17
+ counter ++ ;
18
+ indx += 2 ;
19
+ }
20
+
21
+ if ( counter >= n )
22
+ return true ;
23
+ }
24
+
25
+ return false ;
26
+ } ;
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn can_place_flowers ( flowerbed : Vec < i32 > , n : i32 ) -> bool {
3
+ let mut indx = 0 ;
4
+ let mut counter = 0 ;
5
+ while ( indx < flowerbed. len ( ) ) {
6
+ if ( flowerbed[ indx] == 1 ) {
7
+ indx += 2 ;
8
+ }
9
+ else if ( indx > 0 && flowerbed[ indx - 1 ] == 1 ) {
10
+ indx += 1 ;
11
+ }
12
+ else if ( indx < flowerbed. len ( ) - 1 && flowerbed[ indx + 1 ] == 1 ) {
13
+ indx += 3 ;
14
+ }
15
+ else {
16
+ counter += 1 ;
17
+ indx += 2 ;
18
+ }
19
+
20
+ if ( counter >= n) {
21
+ return true ;
22
+ }
23
+ }
24
+
25
+ return false ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ function canPlaceFlowers ( flowerbed : number [ ] , n : number ) : boolean {
2
+ let indx = 0 ;
3
+ let counter = 0 ;
4
+ while ( indx < flowerbed . length ) {
5
+ if ( flowerbed [ indx ] == 1 )
6
+ indx += 2 ;
7
+ else if ( indx > 0 && flowerbed [ indx - 1 ] == 1 )
8
+ indx += 1 ;
9
+ else if ( indx < flowerbed . length - 1 && flowerbed [ indx + 1 ] == 1 )
10
+ indx += 3 ;
11
+ else {
12
+ counter ++ ;
13
+ indx += 2 ;
14
+ }
15
+
16
+ if ( counter >= n )
17
+ return true ;
18
+ }
19
+
20
+ return false ;
21
+ } ;
You can’t perform that action at this time.
0 commit comments