From 441cebc138ba26334a2b4b16d06008046abe3edc Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 21:41:46 +0530 Subject: [PATCH 01/24] to add the trapped water program --- dynamic_programming/trapped_water.py | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 dynamic_programming/trapped_water.py diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py new file mode 100644 index 000000000000..a8304aad591a --- /dev/null +++ b/dynamic_programming/trapped_water.py @@ -0,0 +1,55 @@ +""" +Given an array of non-negative integers representing an elevation map where +the width of each bar is 1,this program calculates how much rainwater can be trapped. + +Example - height = [0,1,0,2,1,0,1,3,2,1,2,1] +Output: 6 +This problem can be solved using the concept of "DYNAMIC PROGRAMMING". + +We calculate the maximum height of bars on the left and right of every bar in array. +Then iterate over the width of structure and at each index. +The amount of water that will be stored is equal to minimum of maximum height of bars +on both sides minus height of bar at current position. +""" + +def trapped_rainwater(height : list = [0,1,0,2,1,0,1,3,2,1,2,1]) -> int: + """ + The trapped_rainwater function calculates the total amount of rainwater + that can be trapped given an array of bar heights. + It uses a dynamic programming approach, determining the maximum height of bars + on both sides for each bar, and then computing the trapped water above each bar. + The function returns the total trapped water. + + trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) + >>> 6 + trapped_rainwater([7,1,5,3,6,4]) + >>> 9 + trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) + >>> 0 + + """ + length = len(height) + + left_max = [0]*length + left_max[0] = height[0] + for i in range(1, length): + left_max[i] = max(height[i], left_max[i-1]) + + right_max = [0]*length + right_max[length-1] = height[length-1] + for i in range(length-2, -1, -1): + right_max[i] = max(height[i], right_max[i+1]) + + trapped_water : int = 0 + + for i in range(length): + water_level = min(left_max[i], right_max[i]) + trapped_water += water_level - height[i] + + return trapped_water + +if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{trapped_rainwater()}") \ No newline at end of file From 7ebd539e7d42d1f01e338c33033993fa47a65d7d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:14:02 +0000 Subject: [PATCH 02/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index a8304aad591a..0a26cb0f9eb9 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -2,7 +2,7 @@ Given an array of non-negative integers representing an elevation map where the width of each bar is 1,this program calculates how much rainwater can be trapped. -Example - height = [0,1,0,2,1,0,1,3,2,1,2,1] +Example - height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 This problem can be solved using the concept of "DYNAMIC PROGRAMMING". @@ -12,14 +12,15 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list = [0,1,0,2,1,0,1,3,2,1,2,1]) -> int: + +def trapped_rainwater(height: list = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) -> int: """ - The trapped_rainwater function calculates the total amount of rainwater - that can be trapped given an array of bar heights. - It uses a dynamic programming approach, determining the maximum height of bars - on both sides for each bar, and then computing the trapped water above each bar. + The trapped_rainwater function calculates the total amount of rainwater + that can be trapped given an array of bar heights. + It uses a dynamic programming approach, determining the maximum height of bars + on both sides for each bar, and then computing the trapped water above each bar. The function returns the total trapped water. - + trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) >>> 6 trapped_rainwater([7,1,5,3,6,4]) @@ -30,26 +31,27 @@ def trapped_rainwater(height : list = [0,1,0,2,1,0,1,3,2,1,2,1]) -> int: """ length = len(height) - left_max = [0]*length + left_max = [0] * length left_max[0] = height[0] for i in range(1, length): - left_max[i] = max(height[i], left_max[i-1]) + left_max[i] = max(height[i], left_max[i - 1]) - right_max = [0]*length - right_max[length-1] = height[length-1] - for i in range(length-2, -1, -1): - right_max[i] = max(height[i], right_max[i+1]) + right_max = [0] * length + right_max[length - 1] = height[length - 1] + for i in range(length - 2, -1, -1): + right_max[i] = max(height[i], right_max[i + 1]) - trapped_water : int = 0 + trapped_water: int = 0 for i in range(length): water_level = min(left_max[i], right_max[i]) trapped_water += water_level - height[i] - + return trapped_water + if __name__ == "__main__": import doctest doctest.testmod() - print(f"{trapped_rainwater()}") \ No newline at end of file + print(f"{trapped_rainwater()}") From aad0efbd5dd362baec0e2ccc6cdc401d3634fb5d Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 21:50:45 +0530 Subject: [PATCH 03/24] to make changes for error : B006 --- dynamic_programming/trapped_water.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index a8304aad591a..04c91a244242 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -12,7 +12,7 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list = [0,1,0,2,1,0,1,3,2,1,2,1]) -> int: +def trapped_rainwater(height : list = []) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -28,6 +28,8 @@ def trapped_rainwater(height : list = [0,1,0,2,1,0,1,3,2,1,2,1]) -> int: >>> 0 """ + if height == []: + return 0 length = len(height) left_max = [0]*length From 95ff91c794433100338c9fda687a1d3470d911fb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:22:06 +0000 Subject: [PATCH 04/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index e34b02c088c9..ba7937daf94e 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -12,7 +12,8 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list = []) -> int: + +def trapped_rainwater(height: list = []) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. From b18f44a83d0ac90bca030d1cc94abe4408139b23 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 21:57:09 +0530 Subject: [PATCH 05/24] to make changes for error : B006 --- dynamic_programming/trapped_water.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index ba7937daf94e..5324f19302cf 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height: list = []) -> int: +def trapped_rainwater(height: list = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -29,7 +29,8 @@ def trapped_rainwater(height: list = []) -> int: >>> 0 """ - if height == []: + if height is None: + height = [] return 0 length = len(height) From d514b2e8d1d85d3ae472f5f9576c10090b3685fd Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 21:59:02 +0530 Subject: [PATCH 06/24] to make changes for error : B006 --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 5324f19302cf..ba927e64e7ad 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height: list = None) -> int: +def trapped_rainwater(height = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. From ddf7b99b2149f9854099f5fad2a2d1d645ab7d4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:29:35 +0000 Subject: [PATCH 07/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index ba927e64e7ad..25b83cdfcbe4 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height = None) -> int: +def trapped_rainwater(height=None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. From 6962866739f2414a274f036d85cfaaa491cb5a26 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:01:37 +0530 Subject: [PATCH 08/24] to make changes in doctest --- dynamic_programming/trapped_water.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index ba927e64e7ad..81143f4522df 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -25,9 +25,10 @@ def trapped_rainwater(height = None) -> int: >>> 6 trapped_rainwater([7,1,5,3,6,4]) >>> 9 - trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) + trapped_rainwater([]) + >>> 0 + trapped_rainwater() >>> 0 - """ if height is None: height = [] From dadd1a978f27d29e1065b509e00817b1b6d4b0ff Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:06:08 +0530 Subject: [PATCH 09/24] to make changes in doctest --- dynamic_programming/trapped_water.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 13dce760f34d..3190d842a9fb 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height=None) -> int: +def trapped_rainwater(height : list[int] = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -24,11 +24,7 @@ def trapped_rainwater(height=None) -> int: trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) >>> 6 trapped_rainwater([7,1,5,3,6,4]) - >>> 9 - trapped_rainwater([]) - >>> 0 - trapped_rainwater() - >>> 0 + >>> 9 """ if height is None: height = [] @@ -58,4 +54,4 @@ def trapped_rainwater(height=None) -> int: import doctest doctest.testmod() - print(f"{trapped_rainwater()}") + print(f"{trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1])}") From 42021c4adf0e781ff71b339c375f0f313a724746 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:36:42 +0000 Subject: [PATCH 10/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 3190d842a9fb..3cb733084c4f 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height : list[int] = None) -> int: +def trapped_rainwater(height: list[int] = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -24,7 +24,7 @@ def trapped_rainwater(height : list[int] = None) -> int: trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) >>> 6 trapped_rainwater([7,1,5,3,6,4]) - >>> 9 + >>> 9 """ if height is None: height = [] From d61cbea372ba231c2024b02a2f074da684d0915e Mon Sep 17 00:00:00 2001 From: Kosuri L Indu <118645569+kosuri-indu@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:07:16 +0530 Subject: [PATCH 11/24] Update dynamic_programming/trapped_water.py Co-authored-by: Christian Clauss --- dynamic_programming/trapped_water.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 3cb733084c4f..c3585e582949 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -26,8 +26,7 @@ def trapped_rainwater(height: list[int] = None) -> int: trapped_rainwater([7,1,5,3,6,4]) >>> 9 """ - if height is None: - height = [] + if not height: return 0 length = len(height) From 3f9951d2cd44a390629713638444a08faec658d6 Mon Sep 17 00:00:00 2001 From: Kosuri L Indu <118645569+kosuri-indu@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:11:14 +0530 Subject: [PATCH 12/24] Update dynamic_programming/trapped_water.py Co-authored-by: Christian Clauss --- dynamic_programming/trapped_water.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index c3585e582949..7f4a80093101 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -32,8 +32,8 @@ def trapped_rainwater(height: list[int] = None) -> int: left_max = [0] * length left_max[0] = height[0] - for i in range(1, length): - left_max[i] = max(height[i], left_max[i - 1]) + for i, height in enumerate(heights, 1): + left_max[i] = max(height, left_max[i - 1]) right_max = [0] * length right_max[length - 1] = height[length - 1] From 03984afb4f42fb87cc03116541dda80e72787b80 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:16:56 +0530 Subject: [PATCH 13/24] to make changes in parameters --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 3190d842a9fb..1cf879d188d0 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height : list[int] = None) -> int: +def trapped_rainwater(height : list[int]) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. From eaa775e2d98438e7353b283b6ffd517cbc49744f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:48:58 +0000 Subject: [PATCH 14/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 0d56fd045010..02344c313339 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -12,7 +12,8 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list[int]) -> int: + +def trapped_rainwater(height: list[int]) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -31,7 +32,7 @@ def trapped_rainwater(height : list[int]) -> int: left_max = [0] * length left_max[0] = height[0] - for i in range(1,length): + for i in range(1, length): left_max[i] = max(height, left_max[i - 1]) right_max = [0] * length From bf94d38864e44f386de53c4f796904d4756294d9 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:28:08 +0530 Subject: [PATCH 15/24] to make changes in parameters --- dynamic_programming/trapped_water.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 0d56fd045010..36f5a694101b 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -12,7 +12,7 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list[int]) -> int: +def trapped_rainwater(height: list[int] = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -32,7 +32,7 @@ def trapped_rainwater(height : list[int]) -> int: left_max = [0] * length left_max[0] = height[0] for i in range(1,length): - left_max[i] = max(height, left_max[i - 1]) + left_max[i] = max(height[i], left_max[i - 1]) right_max = [0] * length right_max[length - 1] = height[length - 1] From 1ca67014022225dfdb3ea18e2b21aa9f546ccbee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 16:59:52 +0000 Subject: [PATCH 16/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 5c8694737ea3..c3585e582949 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -12,7 +12,8 @@ on both sides minus height of bar at current position. """ -def trapped_rainwater(height : list[int] = None) -> int: + +def trapped_rainwater(height: list[int] = None) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -31,7 +32,7 @@ def trapped_rainwater(height : list[int] = None) -> int: left_max = [0] * length left_max[0] = height[0] - for i in range(1,length): + for i in range(1, length): left_max[i] = max(height[i], left_max[i - 1]) right_max = [0] * length From 10609bcdf615f8dea75b7faa0222166976e7ed02 Mon Sep 17 00:00:00 2001 From: Kosuri L Indu <118645569+kosuri-indu@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:30:31 +0530 Subject: [PATCH 17/24] Update dynamic_programming/trapped_water.py Co-authored-by: Christian Clauss --- dynamic_programming/trapped_water.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index c3585e582949..fbe8eca69abc 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -28,6 +28,8 @@ def trapped_rainwater(height: list[int] = None) -> int: """ if not height: return 0 + if any(height < 0): + raise ValueError("No height can be negative") length = len(height) left_max = [0] * length From a0854dd0ad926503e083d75a608e12a8fe113e45 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:35:48 +0530 Subject: [PATCH 18/24] to make changes in parameters --- dynamic_programming/trapped_water.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index fbe8eca69abc..0d6d588f586f 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(height: list[int] = None) -> int: +def trapped_rainwater(height: list[int]) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights. @@ -26,7 +26,7 @@ def trapped_rainwater(height: list[int] = None) -> int: trapped_rainwater([7,1,5,3,6,4]) >>> 9 """ - if not height: + if not height or height==[]: return 0 if any(height < 0): raise ValueError("No height can be negative") From 7f9f1f11c0ae3bcee0fb7c6073bb638c273fd341 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:06:19 +0000 Subject: [PATCH 19/24] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 0d6d588f586f..12d7990e920e 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -26,7 +26,7 @@ def trapped_rainwater(height: list[int]) -> int: trapped_rainwater([7,1,5,3,6,4]) >>> 9 """ - if not height or height==[]: + if not height or height == []: return 0 if any(height < 0): raise ValueError("No height can be negative") From d6847710f9a8a3b088eccd0ca882d09c752de3b5 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:40:07 +0530 Subject: [PATCH 20/24] for negative heights --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index 12d7990e920e..ae309c28a452 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -28,7 +28,7 @@ def trapped_rainwater(height: list[int]) -> int: """ if not height or height == []: return 0 - if any(height < 0): + if any(h < 0 for h in height): raise ValueError("No height can be negative") length = len(height) From 923058949afdddeb20a75b232a071daac8c862e8 Mon Sep 17 00:00:00 2001 From: Kosuri L Indu <118645569+kosuri-indu@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:57:47 +0530 Subject: [PATCH 21/24] Update dynamic_programming/trapped_water.py Co-authored-by: Christian Clauss --- dynamic_programming/trapped_water.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index ae309c28a452..2a8c50a8c9ef 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -21,10 +21,10 @@ def trapped_rainwater(height: list[int]) -> int: on both sides for each bar, and then computing the trapped water above each bar. The function returns the total trapped water. - trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1]) - >>> 6 - trapped_rainwater([7,1,5,3,6,4]) - >>> 9 + >>> trapped_rainwater([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) + 6 + >>> trapped_rainwater([7, 1, 5, 3, 6, 4]) + 9 """ if not height or height == []: return 0 From 20b78d54bdd4346d0657fb65bb80af9a57b9b334 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sat, 7 Oct 2023 22:58:59 +0530 Subject: [PATCH 22/24] to remove falsy --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index ae309c28a452..8a1e091f5407 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -26,7 +26,7 @@ def trapped_rainwater(height: list[int]) -> int: trapped_rainwater([7,1,5,3,6,4]) >>> 9 """ - if not height or height == []: + if not height: return 0 if any(h < 0 for h in height): raise ValueError("No height can be negative") From f17ff7627c6ccef9e98cdf91d58fd8393f9ba2a1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 7 Oct 2023 20:11:55 +0200 Subject: [PATCH 23/24] Final edits --- dynamic_programming/trapped_water.py | 54 ++++++++++++++-------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index e703dbf32e92..e024e69bdddb 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -1,8 +1,8 @@ """ -Given an array of non-negative integers representing an elevation map where -the width of each bar is 1,this program calculates how much rainwater can be trapped. +Given an array of non-negative integers representing an elevation map where the width +of each bar is 1, this program calculates how much rainwater can be trapped. -Example - height = [0,1,0,2,1,0,1,3,2,1,2,1] +Example - height = (0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1) Output: 6 This problem can be solved using the concept of "DYNAMIC PROGRAMMING". @@ -13,46 +13,48 @@ """ -def trapped_rainwater(height: list[int]) -> int: +def trapped_rainwater(heights: tuple[int]) -> int: """ - The trapped_rainwater function calculates the total amount of rainwater - that can be trapped given an array of bar heights. - It uses a dynamic programming approach, determining the maximum height of bars - on both sides for each bar, and then computing the trapped water above each bar. + The trapped_rainwater function calculates the total amount of rainwater that can be + trapped given an array of bar heights. + It uses a dynamic programming approach, determining the maximum height of bars on + both sides for each bar, and then computing the trapped water above each bar. The function returns the total trapped water. - >>> trapped_rainwater([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) + >>> trapped_rainwater((0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)) 6 - >>> trapped_rainwater([7, 1, 5, 3, 6, 4]) + >>> trapped_rainwater((7, 1, 5, 3, 6, 4)) 9 + >>> trapped_rainwater((7, 1, 5, 3, 6, -1)) + Traceback (most recent call last): + ... + ValueError: No height can be negative """ - if not height: + if not heights: return 0 - if any(h < 0 for h in height): + if any(h < 0 for h in heights): raise ValueError("No height can be negative") - length = len(height) + length = len(heights) left_max = [0] * length - left_max[0] = height[0] - for i in range(1, length): - left_max[i] = max(height[i], left_max[i - 1]) + left_max[0] = heights[0] + for i, height in enumerate(heights[1:], start=1): + left_max[i] = max(height, left_max[i - 1]) right_max = [0] * length - right_max[length - 1] = height[length - 1] + right_max[-1] = heights[-1] for i in range(length - 2, -1, -1): - right_max[i] = max(height[i], right_max[i + 1]) + right_max[i] = max(heights[i], right_max[i + 1]) - trapped_water: int = 0 - - for i in range(length): - water_level = min(left_max[i], right_max[i]) - trapped_water += water_level - height[i] - - return trapped_water + return sum( + min(left, right) - height + for left, right, height in zip(left_max, right_max, heights) + ) if __name__ == "__main__": import doctest doctest.testmod() - print(f"{trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1])}") + print(f"{trapped_rainwater((0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)) = }") + print(f"{trapped_rainwater((7, 1, 5, 3, 6, 4)) = }") From 1bb35ee33daa3d8c5845b73deb2d885ddb3ef46c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 7 Oct 2023 20:13:51 +0200 Subject: [PATCH 24/24] tuple[int, ...] --- dynamic_programming/trapped_water.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/trapped_water.py b/dynamic_programming/trapped_water.py index e024e69bdddb..8bec9fac5fef 100644 --- a/dynamic_programming/trapped_water.py +++ b/dynamic_programming/trapped_water.py @@ -13,7 +13,7 @@ """ -def trapped_rainwater(heights: tuple[int]) -> int: +def trapped_rainwater(heights: tuple[int, ...]) -> int: """ The trapped_rainwater function calculates the total amount of rainwater that can be trapped given an array of bar heights.