{"id":"0bc91d89-cacd-4218-bb3e-ea32ae7fd92d","name":"Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed","description":"1. You are given a number n, representing the number of days.\r\n2. You are given n numbers, where ith number represents price of stock on ith day.\r\n3. You are required to print the maximum profit you can make if you are allowed infinite transactions, but have to cooldown for 1 day after 1 transaction\r\ni.e. you cannot buy on the next day after you sell, you have to cooldown for a day at-least before buying again.\r\nNote - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).","inputFormat":"A number n\r\n.. n more elements","outputFormat":"A number representing the maximum profit you can make if you are allowed infinite transactions with cooldown of 1 day.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n void transaction(vector<int> arr){\n // write your code here\n }\n \n int main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for (int i = 0; i < arr.size(); i++) {\n cin>> arr[i] ;\n }\n\n transaction(arr);\n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def cooldownInfiniteTransaction(arr,n):\n #write your code here\n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n\n cooldownInfiniteTransaction(array,n)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"12\r\n10\r\n15\r\n17\r\n20\r\n16\r\n18\r\n22\r\n20\r\n22\r\n20\r\n23\r\n25","sampleOutput":"19","questionVideo":"https://www.youtube.com/embed/GY0O57llkKQ?end=88","hints":[],"associated":[{"id":"01d1e1b7-96b3-4278-874d-9bf83865b5e4","name":"what will be the output of below input?","slug":"what-will-be-the-output-of-below-input","type":4},{"id":"170f1dbc-b964-4cf7-81d3-dceb51c10206","name":"Can we have Two consecutive CoolDowns?","slug":"can-we-have-two-consecutive-cooldowns","type":4},{"id":"531bdfca-71a2-47bc-9de9-f4be817a6511","name":"Is this Sequence Correct ?","slug":"is-this-sequence-correct","type":4},{"id":"81e83682-c9a9-4c78-891c-c478f68b602c","name":"what is space and time complexity for Buy and sell Stocks with cooldown -Infinite transaction allowed?","slug":"what-is-space-and-time-complexity-for-buy-and-sell-stocks-with-cooldown-infinite-transaction-allowed","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"f9539916-8e2a-4cd8-b6f2-53c683d9ba78","name":"Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed","slug":"buy-and-sell-stocks-with-cooldown-infinite-transaction-allowed","type":1}],"next":{"id":"046350af-067c-43d3-87d8-4a54d35307b8","name":"Buy & Sell Stock with Cooldown - Infinite Transaction Allowed","type":3,"slug":"buy-sell-stock-with-cooldown-infinite-transaction-allowed"},"prev":{"id":"26a59090-383b-4de5-a84e-bc6872b22a61","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed"}}}

Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed

1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are required to print the maximum profit you can make if you are allowed infinite transactions, but have to cooldown for 1 day after 1 transaction i.e. you cannot buy on the next day after you sell, you have to cooldown for a day at-least before buying again. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).

{"id":"0bc91d89-cacd-4218-bb3e-ea32ae7fd92d","name":"Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed","description":"1. You are given a number n, representing the number of days.\r\n2. You are given n numbers, where ith number represents price of stock on ith day.\r\n3. You are required to print the maximum profit you can make if you are allowed infinite transactions, but have to cooldown for 1 day after 1 transaction\r\ni.e. you cannot buy on the next day after you sell, you have to cooldown for a day at-least before buying again.\r\nNote - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).","inputFormat":"A number n\r\n.. n more elements","outputFormat":"A number representing the maximum profit you can make if you are allowed infinite transactions with cooldown of 1 day.","constraints":"0 &lt;= n &lt;= 20\r\n0 &lt;= n1, n2, .. &lt;= 10","sampleCode":{"cpp":{"code":"#include <iostream>\n#include <vector>\n\nusing namespace std;\n\n void transaction(vector<int> arr){\n // write your code here\n }\n \n int main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for (int i = 0; i < arr.size(); i++) {\n cin>> arr[i] ;\n }\n\n transaction(arr);\n return 0;\n }"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def cooldownInfiniteTransaction(arr,n):\n #write your code here\n\ndef main():\n n = int(input())\n array = []\n for i in range(n):\n array.append(int(input()))\n\n cooldownInfiniteTransaction(array,n)\n\nif __name__ == '__main__':\n main()"}},"points":10,"difficulty":"medium","sampleInput":"12\r\n10\r\n15\r\n17\r\n20\r\n16\r\n18\r\n22\r\n20\r\n22\r\n20\r\n23\r\n25","sampleOutput":"19","questionVideo":"https://www.youtube.com/embed/GY0O57llkKQ?end=88","hints":[],"associated":[{"id":"01d1e1b7-96b3-4278-874d-9bf83865b5e4","name":"what will be the output of below input?","slug":"what-will-be-the-output-of-below-input","type":4},{"id":"170f1dbc-b964-4cf7-81d3-dceb51c10206","name":"Can we have Two consecutive CoolDowns?","slug":"can-we-have-two-consecutive-cooldowns","type":4},{"id":"531bdfca-71a2-47bc-9de9-f4be817a6511","name":"Is this Sequence Correct ?","slug":"is-this-sequence-correct","type":4},{"id":"81e83682-c9a9-4c78-891c-c478f68b602c","name":"what is space and time complexity for Buy and sell Stocks with cooldown -Infinite transaction allowed?","slug":"what-is-space-and-time-complexity-for-buy-and-sell-stocks-with-cooldown-infinite-transaction-allowed","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"52d62581-1313-45fb-aaf0-1d72a45f6a50","name":"Dynamic Programming And Greedy For Beginners","slug":"dynamic-programming-and-greedy-for-beginners","type":0},{"id":"f9539916-8e2a-4cd8-b6f2-53c683d9ba78","name":"Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed","slug":"buy-and-sell-stocks-with-cooldown-infinite-transaction-allowed","type":1}],"next":{"id":"046350af-067c-43d3-87d8-4a54d35307b8","name":"Buy & Sell Stock with Cooldown - Infinite Transaction Allowed","type":3,"slug":"buy-sell-stock-with-cooldown-infinite-transaction-allowed"},"prev":{"id":"26a59090-383b-4de5-a84e-bc6872b22a61","name":"Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed","type":3,"slug":"buy-and-sell-stocks-with-transaction-fee-infinite-transactions-allowed"}}}
plane

Editor


Loading...

Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed

medium

1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are required to print the maximum profit you can make if you are allowed infinite transactions, but have to cooldown for 1 day after 1 transaction i.e. you cannot buy on the next day after you sell, you have to cooldown for a day at-least before buying again. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy).

Constraints

0 <= n <= 20 0 <= n1, n2, .. <= 10

Format

Input

A number n .. n more elements

Output

A number representing the maximum profit you can make if you are allowed infinite transactions with cooldown of 1 day.

Example

Sample Input

12 10 15 17 20 16 18 22 20 22 20 23 25

Sample Output

19

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode