Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic-programming #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably add .vscode to your .gitignore file.

"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"*test.py"
],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestArgs": [
"tests"
],
"cSpell.words": [
"curr"
]
}
24 changes: 23 additions & 1 deletion lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@ def max_sub_array(nums):
Returns 0 if nums is None or an empty list.
Time Complexity: ?
Space Complexity: ?
Does this have to be contiguous?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

"""
if nums == None:
return 0
if len(nums) == 0:
return 0
pass
# # Arrange
# input = [50, 3, -50, 50, 3]
# output = 56
# input1 = [-2, -1]
# output = [-1]

max_so_far = 0
max_ending = 0

for i in nums:
max_ending += i

if max_ending > max_so_far:
max_so_far = max_ending

# when max sum is less than 0/negative num
if max_so_far <= 0:
return max(nums)
Comment on lines +29 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note putting this if statement in the loop means that if the 1st number is negative the function will find the largest number in the array and return it instead of finding the max contiguous subarray.

return max_so_far

# Run Time: 0(n)
# Space: O(1)
20 changes: 17 additions & 3 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
# Space Complexity: ?
def newman_conway(num):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: ?
Space Complexity: ?
Time Complexity: o(n)
Space Complexity: 0(n)
"""
Comment on lines 5 to 9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Well done

pass
# count of num and the num itself
# input = 13
# output = 1113 -> 3113 -> 132113-> 1113122113 -> 311311222113 -> 13211321322113 ->
if num == 0:
raise ValueError
if num == 1:
return "1"

sequence = [0, 1, 1]
for val in range(3, num+1):
nc = sequence[sequence[val - 1]] + sequence[val - sequence[val - 1]]
sequence.append(nc)

return ' '.join([str(s) for s in sequence[1:]])