07. Maximize dot product
The problem can be found at the following link: Question Link
My Approach
To solve this problem, we use dynamic programming. We initialize a 2D array dp
of size (n+1) x (m+1)
to store the maximum dot product for subarrays of a
and b
. - We initialize dp[0][j]
to INT_MIN
for handling cases where one of the arrays is empty.
Then, we iterate over the arrays
a
andb
, and for each pair of elements, we updatedp[i][j]
to be the maximum of either the dot product of the subarrays ending at indexi
andj
or the dot product of the subarrays ending at indexi-1
andj
.Finally, we return
dp[n][m]
which represents the maximum dot product.
Time and Auxiliary Space Complexity
Time Complexity :
O(n * m)
, wheren
is the size of arraya
andm
is the size of arrayb
.Auxiliary Space Complexity :
O(n * m)
, for thedp
array.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated