Wednesday, July 5, 2023

Maximum profit from the stock sale.

Maximum profit from a single buy and sale of stock data
def maxProfit(arr):
max_profit = 0
diff = 0
mlen = len(arr)
for i in range(mlen):
for j in range( i+1 ,mlen):
if arr[i] < arr[j]:
diff = arr[j] - arr[i]
if max_profit < diff:
max_profit = diff
return max_profit
arr = [0, 6, 10, 3, 2, 80, -1, 10, 1]
print(maxProfit(arr))

Merge two sorted arrays

Problem Merge One Sorted Array Into Another First array has n positive numbers, and they are sorted in the non-descending order. Second array has 2n numbers: first n are also positive and sorted in the same way but the last n are all zeroes. Merge the first array into the second and return the latter. You should get 2n positive integers sorted in the non-descending order. Example { "first": [1, 3, 5], "second": [2, 4, 6, 0, 0, 0] } Output: [1, 2, 3, 4, 5, 6]
def merge_one_into_another(first, second):
"""
Args:
first(list_int32)
second(list_int32)
Returns:
list_int32
"""
# Write your code here.
sindex = findex = len(first) - 1
s_end_index = len(second) - 1
while( sindex >= 0 and findex >=0 ):
if (first[findex] < second[sindex]):
second[s_end_index] = second[sindex]
sindex -= 1
else:
second[s_end_index] = first[findex]
findex -= 1
s_end_index -= 1
print(findex)
print(sindex)
print(s_end_index)
while(sindex >= 0):
second[s_end_index] = second[sindex]
sindex -= 1
s_end_index -= 1
while(findex >= 0):
second[s_end_index] = first[findex]
findex -= 1
s_end_index -= 1
return second
view raw mergearr.py hosted with ❤ by GitHub