|
1 |
| -# Minimum Cost to Move Chips to The Same Position |
2 |
| -We have n chips, where the position of the ith chip is position[i]. |
| 1 | +# Can Place Flowers |
3 | 2 |
|
4 |
| -We need to move all the chips to the same position. In one step, we can change the position of the ith chip from position[i] to: |
| 3 | +Solution |
| 4 | +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. |
5 | 5 |
|
6 |
| -position[i] + 2 or position[i] - 2 with cost = 0. |
7 |
| -position[i] + 1 or position[i] - 1 with cost = 1. |
8 |
| -Return the minimum cost needed to move all the chips to the same position. |
| 6 | +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. |
9 | 7 |
|
10 | 8 |
|
11 | 9 |
|
12 | 10 | Example 1:
|
13 | 11 |
|
14 |
| - |
15 |
| -Input: position = [1,2,3] |
16 |
| -Output: 1 |
17 |
| -Explanation: First step: Move the chip at position 3 to position 1 with cost = 0. |
18 |
| -Second step: Move the chip at position 2 to position 1 with cost = 1. |
19 |
| -Total cost is 1. |
| 12 | +Input: flowerbed = [1,0,0,0,1], n = 1 |
| 13 | +Output: true |
20 | 14 | Example 2:
|
21 | 15 |
|
22 |
| - |
23 |
| -Input: position = [2,2,2,3,3] |
24 |
| -Output: 2 |
25 |
| -Explanation: We can move the two chips at poistion 3 to position 2. Each move has cost = 1. The total cost = 2. |
26 |
| -Example 3: |
27 |
| - |
28 |
| -Input: position = [1,1000000000] |
29 |
| -Output: 1 |
| 16 | +Input: flowerbed = [1,0,0,0,1], n = 2 |
| 17 | +Output: false |
30 | 18 |
|
31 | 19 |
|
32 | 20 | Constraints:
|
33 | 21 |
|
34 |
| -1 <= position.length <= 100 |
35 |
| -1 <= position[i] <= 10^9<br> |
| 22 | +1 <= flowerbed.length <= 2 * 104 |
| 23 | +flowerbed[i] is 0 or 1. |
| 24 | +There are no two adjacent flowers in flowerbed. |
| 25 | +0 <= n <= flowerbed.length<br> |
36 | 26 | ## Idea
|
37 | 27 | It is really intestring, it is not a good question. THere is a very very simple solution but u need to be really abstract
|
38 | 28 | ## Code
|
39 | 29 | ```python
|
40 | 30 | class Solution:
|
41 |
| - def minCostToMoveChips(self, position: List[int]) -> int: |
42 |
| - eve = 0 |
43 |
| - odd = 0 |
44 |
| - for i in position: |
45 |
| - if i % 2 == 0: |
46 |
| - eve += 1 |
47 |
| - else: |
48 |
| - odd += 1 |
49 |
| - |
50 |
| - return min(eve, odd) |
| 31 | + def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: |
| 32 | + i = 0 |
| 33 | + lenz = len(flowerbed) |
| 34 | + count = 0 |
| 35 | + |
| 36 | + while i < lenz and count < n: |
| 37 | + if flowerbed[i] == 0: |
| 38 | + if i == lenz - 1: |
| 39 | + nex = 0 |
| 40 | + else: nex = flowerbed[i+1] |
| 41 | + if i == 0: |
| 42 | + prev = 0 |
| 43 | + else: |
| 44 | + prev = flowerbed[i-1] |
| 45 | + if nex == 0 and prev == 0: |
| 46 | + flowerbed[i] = 1 |
| 47 | + count += 1 |
| 48 | + |
| 49 | + i += 1 |
| 50 | + return count == n |
51 | 51 | ```
|
0 commit comments