Skip to content

Commit fea38e6

Browse files
committed
605_Can_Place_Flowers
1 parent 55a9b4c commit fea38e6

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Also, there are open source implementations for basic data structs and algorithm
153153
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/557_Reverse_Words_in_a_String_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/557_Reverse_Words_in_a_String_III.java) | String handle: Split with space than reverse word, O(n) and O(n). Better solution is that reverse can be O(1) space in array. |
154154
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/572_Subtree_of_Another_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/572_Subtree_of_Another_Tree.java) | 1. Tree traverse and compare<br>2. Tree to string and compare |
155155
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)|
156+
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/605_Can_Place_Flowers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/605_Can_Place_Flowers.java) | One time scan, check [i-1] [i] and [i+1], O(n) and O(1) |
156157
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) |
157158
| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/628_Maximum_Product_of_Three_Numbers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/628_Maximum_Product_of_Three_Numbers.java) | Actually, we should only care about min1, min2 and max1-max3, to find these five elements, we can use 1. Brute force, O(n^3) and O(1)<br>2. Sort, O(nlogn) and O(1)<br>3. Single scan with 5 elements keep, O(n) and O(1) |
158159
| 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) |

java/605_Can_Place_Flowers.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Solution {
2+
/*public boolean canPlaceFlowers(int[] flowerbed, int n) {
3+
int i = 0, count = 0;
4+
while (i < flowerbed.length) {
5+
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
6+
flowerbed[i++] = 1;
7+
count++;
8+
}
9+
if(count >= n)
10+
return true;
11+
i++;
12+
}
13+
return false;
14+
}*/
15+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
16+
int count = 0, curr;
17+
for (int i = 0; i < flowerbed.length; i++) {
18+
curr = flowerbed[i];
19+
if (i - 1 >= 0) curr += flowerbed[i - 1];
20+
if (i + 1 < flowerbed.length) curr += flowerbed[i + 1];
21+
if (curr == 0) {
22+
count++;
23+
flowerbed[i] = 1;
24+
}
25+
if (count >= n) return true;
26+
}
27+
return false;
28+
}
29+
}

python/605_Can_Place_Flowers.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def canPlaceFlowers(self, flowerbed, n):
3+
"""
4+
:type flowerbed: List[int]
5+
:type n: int
6+
:rtype: bool
7+
"""
8+
count = 0
9+
for i in range(len(flowerbed)):
10+
curr = flowerbed[i]
11+
if i - 1 >= 0:
12+
curr += flowerbed[i - 1]
13+
if i + 1 < len(flowerbed):
14+
curr += flowerbed[i + 1]
15+
if curr == 0:
16+
count += 1
17+
flowerbed[i] = 1
18+
if count >= n:
19+
return True
20+
return False

0 commit comments

Comments
 (0)