Skip to content

Commit b505ef4

Browse files
committed
05/12/2020
1 parent e36e63f commit b505ef4

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

Daily-challenge/Dec/05/README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
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
32

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.
55

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.
97

108

119

1210
Example 1:
1311

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
2014
Example 2:
2115

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
3018

3119

3220
Constraints:
3321

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>
3626
## Idea
3727
It is really intestring, it is not a good question. THere is a very very simple solution but u need to be really abstract
3828
## Code
3929
```python
4030
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
5151
```

Daily-challenge/Dec/05/sol.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
class Solution:
2-
def minCostToMoveChips(self, position: List[int]) -> int:
3-
eve = 0
4-
odd = 0
5-
for i in position:
6-
if i % 2 == 0:
7-
eve += 1
8-
else:
9-
odd += 1
10-
11-
return min(eve, odd)
2+
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
3+
i = 0
4+
lenz = len(flowerbed)
5+
count = 0
6+
7+
while i < lenz and count < n:
8+
if flowerbed[i] == 0:
9+
if i == lenz - 1:
10+
nex = 0
11+
else: nex = flowerbed[i+1]
12+
if i == 0:
13+
prev = 0
14+
else:
15+
prev = flowerbed[i-1]
16+
if nex == 0 and prev == 0:
17+
flowerbed[i] = 1
18+
count += 1
19+
20+
i += 1
21+
return count == n

0 commit comments

Comments
 (0)