Skip to content

Commit 8a0fd72

Browse files
committedNov 10, 2024
Add planting flowers problem
1 parent cf2af19 commit 8a0fd72

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ List of Programs related to data structures and algorithms
5757
| 16 | Two missing numbers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/16.twoMissingNumbers/twoMissingNumbers.md) | Medium | Sum and average calculations |
5858
| 17 | Pascal's Triangle | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.md) | Easy | Array traversal and sequential sum |
5959
| 18 | Remove Element | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.md) | Easy | Array traversal |
60+
| 19 | Can place flowers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.md) | Easy | Array traversal and comparison |
6061

6162
### String
6263

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package java1.algorithms.array.canPlaceFlowers;
2+
3+
public class CanPlaceFlowers {
4+
private static boolean canPlaceFlowers(int[] flowersbed, int n){
5+
int count = 0;
6+
7+
for (int i = 0; i < flowersbed.length; i++) {
8+
if(flowersbed[i] == 0 && (i == 0 || flowersbed[i-1] == 0) && (i == flowersbed.length-1 || flowersbed[i+1] == 0)) {
9+
flowersbed[i] = 1;
10+
count++;
11+
12+
if(count >= n) {
13+
return true;
14+
}
15+
16+
i++;
17+
}
18+
}
19+
return count >= n;
20+
}
21+
public static void main(String[] args) {
22+
int[] flowerbed1 = new int[]{1,0,0,1}; int n1= 1;
23+
int[] flowerbed2 = new int[]{1,0,0,0,1}; int n2= 1;
24+
int[] flowerbed3 = new int[]{1,0,0,0,1}; int n3= 2;
25+
System.out.println(canPlaceFlowers(flowerbed1, n1));
26+
System.out.println(canPlaceFlowers(flowerbed2, n2));
27+
System.out.println(canPlaceFlowers(flowerbed3, n3));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
Given an array of integers `flowerbed` in which some of the plots are planted, and others are not considering the fact that flowers cannot be planted in adjacent plots. Here, the number `0` represent an empty plot and `1` represent non-empty plot. Return `true` if `n` new flowers can be planted inside flowerbed without violating the adjaceny plots rule.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: flowerbed = [1,0,0,1], n = 1
8+
Output: false
9+
10+
Example 2:
11+
12+
Input: flowerbed = [1,0,0,0,1], n = 1
13+
Output: true
14+
15+
Example 3:
16+
17+
Input: flowerbed = [1,0,0,0,1], n = 2
18+
Output: false
19+
20+
**Algorithmic Steps**
21+
This problem is solved with the help of iteration over an array and compare adjacent elements. The algorithmic approach can be summarized as follows:
22+
23+
1. Create a function(`canPlaceFolowers`) by accepting flower bed array(`flowerbed`) and number of flowers to plant(`n`).
24+
25+
2. Initialize the counting variable(`count`) to determine number of flowers can be planted.
26+
27+
3. Iterate over the array and compare each value with their adjacent values if the current value is 0.
28+
1. If either the adjancent value is zero or a boundary value(i.e, first or last element in an array), the flower bed can be planted with a flower by updating it's value to 1. The counter can be incremented.
29+
30+
2. Perform a early exit by returning `true` if the count is greater than or equal to given number `n`.
31+
32+
3. Skip the next iteration by incrementing index variable(`i`). Since the flower is planted at the current position, it won't be planted for next adjacent position.
33+
34+
4. After completion of iteration, return expression `count >=n` to indicate given number of flowers can be planted or not.
35+
36+
**Time and Space complexity:**
37+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
38+
39+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than counting variable.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function canPlaceFlowers(flowerbed, n){
2+
let count = 0;
3+
4+
for (let i = 0; i < flowerbed.length; i++) {
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+
}
12+
i++;
13+
}
14+
15+
}
16+
17+
return count >= n;
18+
}
19+
20+
const flowerbed1 = [1,0,0,1], n1= 1;
21+
const flowerbed2 = [1,0,0,0,1], n2= 1;
22+
const flowerbed3 = [1,0,0,0,1], n3= 2;
23+
console.log(canPlaceFlowers(flowerbed1, n1));
24+
console.log(canPlaceFlowers(flowerbed2, n2));
25+
console.log(canPlaceFlowers(flowerbed3, n3));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
Given an array of integers `flowerbed` in which some of the plots are planted, and others are not considering the fact that flowers cannot be planted in adjacent plots. Here, the number `0` represent an empty plot and `1` represent non-empty plot. Return `true` if `n` new flowers can be planted inside flowerbed without violating the adjaceny plots rule.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: flowerbed = [1,0,0,1], n = 1
8+
Output: false
9+
10+
Example 2:
11+
12+
Input: flowerbed = [1,0,0,0,1], n = 1
13+
Output: true
14+
15+
Example 3:
16+
17+
Input: flowerbed = [1,0,0,0,1], n = 2
18+
Output: false
19+
20+
**Algorithmic Steps**
21+
This problem is solved with the help of iteration over an array and compare adjacent elements. The algorithmic approach can be summarized as follows:
22+
23+
1. Create a function(`canPlaceFolowers`) by accepting flower bed array(`flowerbed`) and number of flowers to plant(`n`).
24+
25+
2. Initialize the counting variable(`count`) to determine number of flowers can be planted.
26+
27+
3. Iterate over the array and compare each value with their adjacent values if the current value is 0.
28+
1. If either the adjancent value is zero or a boundary value(i.e, first or last element in an array), the flower bed can be planted with a flower by updating it's value to 1. The counter can be incremented.
29+
30+
2. Perform a early exit by returning `true` if the count is greater than or equal to given number `n`.
31+
32+
3. Skip the next iteration by incrementing index variable(`i`). Since the flower is planted at the current position, it won't be planted for next adjacent position.
33+
34+
4. After completion of iteration, return expression `count >=n` to indicate given number of flowers can be planted or not.
35+
36+
**Time and Space complexity:**
37+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
38+
39+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than counting variable.

0 commit comments

Comments
 (0)
Please sign in to comment.