From 3c322daff12ce82409697e43663a1c538467351e Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Wed, 7 Nov 2018 04:32:34 +0000 Subject: [PATCH 1/4] Added Solution.py --- solution/0605.Can Place Flowers/Solution.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 solution/0605.Can Place Flowers/Solution.py diff --git a/solution/0605.Can Place Flowers/Solution.py b/solution/0605.Can Place Flowers/Solution.py new file mode 100644 index 0000000000000..e4ac7e0325115 --- /dev/null +++ b/solution/0605.Can Place Flowers/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def canPlaceFlowers(self, flowerBed, n): + """ + type flowerBed : List[int], n : int + rtype : bool + """ + + for i in range(len(flowerBed)): + if i == 0 and flowerBed[0] == 0: # for 1st Element + if len(flowerBed) == 1 or (len(flowerBed) > 1 and flowerBed[1] == 0): + n -= 1 + elif i == len(flowerBed)-1 and flowerBed[i] == 0 and flowerBed[i-1] == 0: # for last element + n -= 1 + elif flowerBed[i] == 0 and flowerBed[i-1] == 0 and flowerBed[i+1] == 0: + n -= 1 + + return n==0 \ No newline at end of file From 851bd720a8a12b84bab1fb4a4ff3c64eee46586f Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Wed, 7 Nov 2018 05:11:25 +0000 Subject: [PATCH 2/4] Update Solution.py --- solution/0605.Can Place Flowers/Solution.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/solution/0605.Can Place Flowers/Solution.py b/solution/0605.Can Place Flowers/Solution.py index e4ac7e0325115..ef213ab288f59 100644 --- a/solution/0605.Can Place Flowers/Solution.py +++ b/solution/0605.Can Place Flowers/Solution.py @@ -9,9 +9,12 @@ def canPlaceFlowers(self, flowerBed, n): if i == 0 and flowerBed[0] == 0: # for 1st Element if len(flowerBed) == 1 or (len(flowerBed) > 1 and flowerBed[1] == 0): n -= 1 + flowerBed[0] = 1 elif i == len(flowerBed)-1 and flowerBed[i] == 0 and flowerBed[i-1] == 0: # for last element n -= 1 + flowerBed[i] = 1 elif flowerBed[i] == 0 and flowerBed[i-1] == 0 and flowerBed[i+1] == 0: n -= 1 + flowerBed[i] = 1 - return n==0 \ No newline at end of file + return n==0 From 133aac75508d8b90142538626a74951a7b311eed Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Wed, 7 Nov 2018 06:12:27 +0000 Subject: [PATCH 3/4] Update Solution.py --- solution/0605.Can Place Flowers/Solution.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solution/0605.Can Place Flowers/Solution.py b/solution/0605.Can Place Flowers/Solution.py index ef213ab288f59..45f962d6e78dd 100644 --- a/solution/0605.Can Place Flowers/Solution.py +++ b/solution/0605.Can Place Flowers/Solution.py @@ -16,5 +16,7 @@ def canPlaceFlowers(self, flowerBed, n): elif flowerBed[i] == 0 and flowerBed[i-1] == 0 and flowerBed[i+1] == 0: n -= 1 flowerBed[i] = 1 - + if n == 0: + break + return n==0 From 5cb92a1a61252dc37ef050a8c447c96a9f37dcfa Mon Sep 17 00:00:00 2001 From: Ashwek Swamy <39827514+ashwek@users.noreply.github.com> Date: Wed, 7 Nov 2018 06:30:40 +0000 Subject: [PATCH 4/4] Update Solution.py --- solution/0605.Can Place Flowers/Solution.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solution/0605.Can Place Flowers/Solution.py b/solution/0605.Can Place Flowers/Solution.py index 45f962d6e78dd..44462eb86495d 100644 --- a/solution/0605.Can Place Flowers/Solution.py +++ b/solution/0605.Can Place Flowers/Solution.py @@ -5,7 +5,8 @@ def canPlaceFlowers(self, flowerBed, n): rtype : bool """ - for i in range(len(flowerBed)): + i = 0 + while n > 0 and i < len(flowerBed): if i == 0 and flowerBed[0] == 0: # for 1st Element if len(flowerBed) == 1 or (len(flowerBed) > 1 and flowerBed[1] == 0): n -= 1 @@ -16,7 +17,6 @@ def canPlaceFlowers(self, flowerBed, n): elif flowerBed[i] == 0 and flowerBed[i-1] == 0 and flowerBed[i+1] == 0: n -= 1 flowerBed[i] = 1 - if n == 0: - break + i += 1 return n==0