-
Notifications
You must be signed in to change notification settings - Fork 1
/
0605--can-place-flowers.txt
156 lines (145 loc) · 3.92 KB
/
0605--can-place-flowers.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// https://leetcode.com/problems/can-place-flowers/description/
// 605. Can Place Flowers
// Easy
// 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.
// Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed 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
//
// Constraints:
// 1 <= flowerbed.length <= 2 * 104
// flowerbed[i] is 0 or 1.
// There are no two adjacent flowers in flowerbed.
// 0 <= n <= flowerbed.length
// .JS
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function (flowerbed, n) {
let count = 0;
for (let i = 0; i < flowerbed.length; i++) {
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;
}
return count >= n;
}
// -- OR --
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function (flowerbed, n) {
for (let i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] === 0 &&
(i === 0 || flowerbed[i - 1] === 0) &&
(i === flowerbed.length - 1 || flowerbed[i + 1] === 0)
) {
flowerbed[i] = 1;
n--;
i++;
}
if (n <= 0) return true;
}
return false;
}
// -- OR --
var canPlaceFlowers_______3 = function(flowerbed, n) {
let current = 0; const size = flowerbed.length;
for(var i = 0; i <= size; i++) {
if (i < size && flowerbed[i] == 0) {
current++;
if (i == 0) current++;
if (i == size - 1) current++;
} else {
n -= Math.trunc((current - 1) / 2);
if (n <= 0) return true;
current = 0;
}
}
return false;
};
// PHP
class Solution
{
/**
* @param Integer[] $flowerbed
* @param Integer $n
* @return Boolean
*/
function canPlaceFlowers(array $flowerbed, int $n): bool
{
for ($i = 0; $i < count($flowerbed); $i++) {
if ($flowerbed[$i] == 0 &&
($i == 0 || $flowerbed[$i - 1] == 0) &&
($i == count($flowerbed) - 1 || $flowerbed[$i + 1] == 0)
) {
$n--;
$flowerbed[$i] = 1;
$i++;
}
if ($n <= 0) {
return true;
}
}
return false;
}
}
-- OR --
class Solution
{
/**
* SOLUTION 2 - without modifying the input array
* @param Integer[] $flowerbed
* @param Integer $n
* @return Boolean
*/
function canPlaceFlowers($flowerbed, $n)
{
$count = 0;
$flowerbedLength = count($flowerbed);
for ($i = 0; $i <= $flowerbedLength; $i++) {
if ($flowerbed[$i] == 0 &&
$i < $flowerbedLength) {
$count++;
if ($i == 0) $count++;
if ($i == $flowerbedLength - 1) $count++;
} else {
$n -= intdiv($count - 1, 2);
if ($n <= 0) return true;
$count = 0;
}
}
return false;
}
}
// C++
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
flowerbed.insert(flowerbed.begin(),0);
flowerbed.push_back(0);
for(int i = 1; i < flowerbed.size()-1; ++i)
{
if(flowerbed[i-1] + flowerbed[i] + flowerbed[i+1] == 0)
{
--n;
++i;
}
}
return n <=0;
}
};