From 9528b0def272170cf5db4319fdf44ca29b8749db Mon Sep 17 00:00:00 2001
From: Shail Patel <126716491+shail-patel-321@users.noreply.github.com>
Date: Sun, 27 Oct 2024 02:07:54 +0530
Subject: [PATCH] Create BestTimeToBuyAndSellStock2.js

---
 .../BestTimeToBuyAndSellStock2.js             | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 Dynamic-Programming/BestTimeToBuyAndSellStock2.js

diff --git a/Dynamic-Programming/BestTimeToBuyAndSellStock2.js b/Dynamic-Programming/BestTimeToBuyAndSellStock2.js
new file mode 100644
index 0000000000..011e009d60
--- /dev/null
+++ b/Dynamic-Programming/BestTimeToBuyAndSellStock2.js
@@ -0,0 +1,27 @@
+// Link :- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
+/*
+* You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
+* On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
+* Find and return the maximum profit you can achieve.
+*/
+
+/**
+ * @param {number[]} prices
+ * @return {number}
+ */
+var maxProfit = function(prices) {
+    const n = prices.length;
+    const dp = Array.from({ length: n }, () => Array(2).fill(0));
+
+    for (let i = n - 1; i >= 0; i--) {
+        if (i === n - 1) {
+            dp[i][0] = 0;
+            dp[i][1] = prices[i];
+            continue;
+        }
+        dp[i][0] = Math.max(dp[i + 1][1] - prices[i], dp[i + 1][0]);
+        dp[i][1] = Math.max(dp[i + 1][1], dp[i + 1][0] + prices[i]);
+    }
+
+    return dp[0][0];
+};