From 78eea6f86902823c27d5a9324a5e407891429304 Mon Sep 17 00:00:00 2001 From: darrenltj <142252698+darrenltj@users.noreply.github.com> Date: Fri, 1 Sep 2023 23:23:44 +0800 Subject: [PATCH 1/3] Delete max_ones_index.py --- algorithms/arrays/max_ones_index.py | 42 ----------------------------- 1 file changed, 42 deletions(-) diff --git a/algorithms/arrays/max_ones_index.py b/algorithms/arrays/max_ones_index.py index 40abdf505..8b1378917 100644 --- a/algorithms/arrays/max_ones_index.py +++ b/algorithms/arrays/max_ones_index.py @@ -1,43 +1 @@ -""" -Find the index of 0 to be replaced with 1 to get -longest continuous sequence -of 1s in a binary array. -Returns index of 0 to be -replaced with 1 to get longest -continuous sequence of 1s. -If there is no 0 in array, then -it returns -1. -e.g. -let input array = [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1] -If we replace 0 at index 3 with 1, we get the longest continuous -sequence of 1s in the array. -So the function return => 3 -""" - - -def max_ones_index(arr): - - n = len(arr) - max_count = 0 - max_index = 0 - prev_zero = -1 - prev_prev_zero = -1 - - for curr in range(n): - - # If current element is 0, - # then calculate the difference - # between curr and prev_prev_zero - if arr[curr] == 0: - if curr - prev_prev_zero > max_count: - max_count = curr - prev_prev_zero - max_index = prev_zero - - prev_prev_zero = prev_zero - prev_zero = curr - - if n - prev_prev_zero > max_count: - max_index = prev_zero - - return max_index From a93fabb8929dfffbebd7f723c9df2b7616f52a3a Mon Sep 17 00:00:00 2001 From: darrenltj Date: Sat, 2 Sep 2023 00:05:12 +0800 Subject: [PATCH 2/3] Added split.py file --- algorithms/strings/split.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 algorithms/strings/split.py diff --git a/algorithms/strings/split.py b/algorithms/strings/split.py new file mode 100644 index 000000000..06889d218 --- /dev/null +++ b/algorithms/strings/split.py @@ -0,0 +1,4 @@ +myString = "Hello world" +myList = myString.split() + +print(myList) \ No newline at end of file From 5717d1bc0d28ac717c91d522864123547ed15a35 Mon Sep 17 00:00:00 2001 From: darrenltj Date: Sat, 2 Sep 2023 00:18:31 +0800 Subject: [PATCH 3/3] Restore max_ones_index file --- algorithms/arrays/max_ones_index.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/algorithms/arrays/max_ones_index.py b/algorithms/arrays/max_ones_index.py index 8b1378917..40abdf505 100644 --- a/algorithms/arrays/max_ones_index.py +++ b/algorithms/arrays/max_ones_index.py @@ -1 +1,43 @@ +""" +Find the index of 0 to be replaced with 1 to get +longest continuous sequence +of 1s in a binary array. +Returns index of 0 to be +replaced with 1 to get longest +continuous sequence of 1s. +If there is no 0 in array, then +it returns -1. +e.g. +let input array = [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1] +If we replace 0 at index 3 with 1, we get the longest continuous +sequence of 1s in the array. +So the function return => 3 +""" + + +def max_ones_index(arr): + + n = len(arr) + max_count = 0 + max_index = 0 + prev_zero = -1 + prev_prev_zero = -1 + + for curr in range(n): + + # If current element is 0, + # then calculate the difference + # between curr and prev_prev_zero + if arr[curr] == 0: + if curr - prev_prev_zero > max_count: + max_count = curr - prev_prev_zero + max_index = prev_zero + + prev_prev_zero = prev_zero + prev_zero = curr + + if n - prev_prev_zero > max_count: + max_index = prev_zero + + return max_index