From ad46d17a80f94e1fcf5d562eb489e0e34aedacb0 Mon Sep 17 00:00:00 2001 From: MANUL <113660278+Manul28@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:32:42 +0530 Subject: [PATCH] max_dots.cpp --- Leetcode/max_dots.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Leetcode/max_dots.cpp diff --git a/Leetcode/max_dots.cpp b/Leetcode/max_dots.cpp new file mode 100644 index 00000000..e4b8ca7b --- /dev/null +++ b/Leetcode/max_dots.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int maxDotProduct(vector& nums1, vector& nums2) { + int n = nums1.size(); + int m = nums2.size(); + vector> memo(n, vector(m, INT_MIN)); + + function dp = [&](int i, int j) { + if (i == n || j == m) { + return INT_MIN; + } + + if (memo[i][j] != INT_MIN) { + return memo[i][j]; + } + + memo[i][j] = max( + nums1[i] * nums2[j] + max(dp(i + 1, j + 1), 0), + max(dp(i + 1, j), dp(i, j + 1)) + ); + + return memo[i][j]; + }; + + return dp(0, 0); + } +};