id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
1
{ "code": "class Solution {\npublic:\n// time: O(n * log(max_diff) + n * log(n))\n// space: O(1)\n\n/* Find the min value for pair difference so that the pairs whose difference ls less than the value\nis less than p.\n*/\n int minimizeMax(vector<int>& nums, int p) {\n sort(nums.begin(), nums.end()); \n ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
2
{ "code": "class Solution {\npublic:\n\n int isValid(vector<int>& nums, int mid, int p){\n int cnt = 0;\n int i = 0;\n while(i < nums.size()-1){\n if(abs(nums[i]-nums[i+1]) <= mid){\n cnt++;\n i+=2;\n }else i++;\n if(cnt==p)return ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
2
{ "code": "class Solution {\npublic:\n int minimizeMax(vector<int>& nums, int p) {\n if (p == 0) {\n return 0;\n }\n\n sort(nums.begin(), nums.end());\n\n int l = 0, r = nums[nums.size() - 1];\n\n while (l < r) {\n int mid = l + (r - l) / 2;\n\n i...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int check(int diff[], int n, int mid){\n int i = 0; \n int counter = 0;\n while(i < n-1){\n if(diff[i] <= mid){\n counter++;\n i += 2;\n }\n else i++;\n }\n return counter;\n ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int check(int diff[], int n, int mid){\n int i = 0; \n int counter = 0;\n while(i < n-1){\n if(diff[i] <= mid){\n counter++;\n i += 2;\n }\n else i++;\n }\n return counter;\n ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int minimizeMax(vector<int>& nums, int p) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n// print(nums);\n int low = 0, high = nums[n - 1] - nums[0], ans = 0;\n while (low <= high) {\n int mid = low + (high - low) / 2, cnt = 0;\n // print(low,...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n bool check(vector<int>& dis, int p, int mid){\n for(int i = 0; i < dis.size(); i++){\n if(dis[i] <= mid){\n p--;\n i++;\n }\n if(p==0) return true;\n }\n return (p<=0);\n }\n\n int minim...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int p;\n int n;\n vector<int>arr;\n bool check(int mid){\n int count=0;\n for(int i=1;i<n;){\n if((arr[i]-arr[i-1])<=mid){\n count++;i+=2;\n }\n else i+=1;\n }\n if(count>=p)return true;\n ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "\nclass Solution {\npublic:\n int minimizeMax(vector<int>& nums, int p) {\n int n=nums.size();\n if(p==0)return 0;\n sort(nums.begin(),nums.end());\n int low=0;\n int high=1e9;int ans;\n while(high>=low){\n int mid=(low+high)/2;\n vector<bo...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n bool isOk(vector<int>& nums, int hi, int p) {\n\n int ptr = 0;\n int cnt = 0;\n while (ptr < nums.size()) {\n if (nums[ptr] <= hi) {\n cnt++;\n ptr += 2;\n } else {\n ptr++;\n }...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int minimizeMax(vector<int>& nums, int p) {\n sort(nums.begin(),nums.end());\n int n = nums.size();\n vector<int>diff;\n for(int i=1;i<n;i++){\n diff.push_back(nums[i]-nums[i-1]);\n }\n int m = diff.size();\n int low...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "// BY BINARY SEARCH APPROACH:\n\nclass Solution {\npublic:\n int minimizeMax(vector<int>& nums, int p) {\n if(p==0) return 0;\n int n = nums.size();\n sort(nums.begin(), nums.end());\n int l = INT_MAX;\n int r = INT_MIN;\n for(int i=1;i<n;i++){\n int ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\n bool isPoss(int diff,vector<int> &nums,int &p){\n int cnt = 0;\n // if(diff ==0){\n // for(auto it:mp) cout<<it<<\" \";\n // cout<<\"-*\\n\";\n // }\n // for(auto it= mp.begin();it != mp.end();it++){\n // int val = *it;\n ...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int helper(vector<int> nums, int diff){\n int i = 1;int cnt = 0;\n while(i < nums.size()){\n if(nums[i] - nums[i-1] <= diff){\n i+=2;\n cnt++;\n }\n else{i++;}\n }\n return cnt;\n }\...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n int find(vector<int> arr,int val)\n {\n int i=0;\n int n=arr.size();\n int count=0;\n while(i<n-1)\n {\n if(arr[i+1]-arr[i]<=val)\n {\n count++;\n i=i+2;\n }\n else...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n bool check(vector<int>& nums, int p, int tgt){\n int i = 2, n = nums.size();\n vector<int> gd(n);\n gd[0] = 0;\n if(nums[1] - nums[0] <= tgt){\n if(p == 1){\n return true;\n }\n gd[1] = 1;\n }e...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n bool check(vector<int>& nums, int p, int tgt){\n int i = 2, n = nums.size();\n vector<int> gd(n, 0);\n if(nums[1] - nums[0] <= tgt){\n gd[1] = 1;\n }\n while(i < n){\n if(nums[i] - nums[i-1] <= tgt){\n gd...
2,720
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>p</code>. Find <code>p</code> pairs of indices of <code>nums</code> such that the <strong>maximum</strong> difference amongst all the pairs is <strong>minimized</strong>. Also, ensure no index appears more than once among...
3
{ "code": "class Solution {\npublic:\n bool check(vector<int>& nums, int p, int tgt){\n int i = 2, n = nums.size();\n vector<int> gd(n, 0);\n if(nums[1] - nums[0] <= tgt){\n gd[1] = 1;\n if(p == 1){\n return true;\n }\n }\n while(i ...
2,678
<p>There is a <strong>directed weighted</strong> graph that consists of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The edges of the graph are initially represented by the given array <code>edges</code> where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, edgeCost<sub>i</sub>]</code> m...
3
{ "code": "/// @author Huy\n/// @date 19:44:30 20/07/2024\n#include <iostream>\n\n#ifdef HUY_DEBUG\n#include \"E:/code/library/debug.h\"\nusing namespace std;\n\n#else\n#define dbg(...) 42\n#endif\n\nstruct Edge {\n int to;\n long long weight;\n\n Edge() : to(0), weight(0) {}\n Edge(int a, long long b) ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (int _i = 1, _n = s.length(); _i < _n; ++_i) {\n int price = s[_i++] & 15;\n wh...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nint init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (in...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nint init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (in...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nint init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (in...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nint init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (in...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nint init = [] {\n\tcin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, minPrice = INT_MAX;\n for (in...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\n public:\n int maxProfit(vector<int>& prices) {\n int cur = INT_MAX, profit = 0;\n for (int x : prices) {\n if (x < cur) {\n cur = x;\n } else {\n profit = max(profit, x - cur);\n }\n }\n return profit;\n }\n};\n\nint speedUp = [] {\n std::ios...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "int init = [] {\n cin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int solution = 0, maxi = INT_MIN;\n int num_to_int;\n for (int i = s.length() - 1; i >= 0; --i) {\n string num;\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nstatic const bool __boost = []() {\n\tcin.tie(nullptr);\n\tcout.tie(nullptr);\n\treturn std::ios_base::sync_with_stdio(false);\n\t}();\n\n\nconst size_t BUFFER_SIZE = 0x6fafffff;\nalignas(std::max_align_t) char b...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& nums) {\n int mx=0;\n int b = nums[0];\n for(int i=1;i<nums.size();i++){\n if(nums[i]<b){\n b=nums[i];\n }else if(nums[i]-b>mx){\n mx = nums[i]-b;\n }\n }\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\nprivate:\n \npublic:\n int maxProfit(vector<int>& prices) {\n int minStock = prices[0];\n int ans = 0;\n for(int i = 1; i < prices.size(); i++){\n int profit = prices[i] - minStock;\n minStock = min(minStock, prices[i]);\n ans = ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int length = prices.size();\n \n int B_Value = prices[0];\n int S_Value = B_Value;\n \n \n int prev = 0;\n \n \n int i =0;\n while( i < length)\n {\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int minPrice=INT_MAX, profit=0;\n for(int i=0;i<prices.size();i++){\n if(prices[i]<minPrice)\n minPrice=prices[i];\n if(prices[i]-minPrice>profit)\n profit=prices[i]-minPr...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n \n int k = INT_MAX;\n\n int profitToday = 0;\n int ans = 0;\n\n for(int j = 0; j < prices.size(); ++j){\n if(prices[j] < k){\n k = prices[j];\n }\n profitToday = prices[j] - k;\n if(a...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n int mini=INT_MAX;\n int maxp=0;\n for(int i=0;i<n;i++){\n if(mini>prices[i]){\n mini=prices[i];\n }\n int profit=prices[i]-mini;\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int cost = 0;\n int profit = 0;\n int minim = prices[0];\n\n for(int i = 1; i < prices.size(); i++){\n cost = prices[i] - minim;\n profit = max(profit, cost);\n minim = min(min...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int buy=prices[0];\n int profit=0;\n for(int i=1;i<prices.size();i++)\n {\n if(prices[i]<buy)\n buy=prices[i];\n else if(prices[i]-buy>profit)\n profit=prices[i]-buy...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n \n int ans = 0;\n\n int mn = prices[0];\n\n for(int i=1; i< prices.size(); i++)\n {\n ans = max(ans , prices[i] - mn);\n\n mn = min(mn , prices[i]);\n }\n\n return an...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int profit =0;\n int buyAtPrice = INT_MAX;\n for(int i=0; i<prices.size(); i++){\n buyAtPrice = min(prices[i], buyAtPrice);\n profit = max(prices[i]- buyAtPrice, profit);\n }\n ret...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int difference = 0 ;\n int maxProfit = 0 ;\n for(int i=1 ; i<prices.size() ; i++) {\n difference += prices[i] - prices[i-1] ;\n maxProfit = max(maxProfit , difference) ;\n\n if(differ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n int mini = prices[0];\n int profit = 0;\n for(int i = 1; i < n; i++){\n profit = max(profit, prices[i]-mini);\n mini = min(mini, prices[i]);\n }\n r...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) { \n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n int maximum = 0;\n int least = INT_MAX;\n for (int i = 0; i < prices.size();i++) {\n least = min(least,prices[i]);\n maximum = m...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "int speedUp = [] {\n std::ios::sync_with_stdio(0);\n std::cin.tie(0);\n return 0;\n}();\nclass Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int profit = 0;\n int mini = prices[0];\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n stack<int> values;\n int ans=0;\n int max=0;\n for(int i =prices.size()-1; i>=0;i--)\n {\n if(values.empty())\n {\n values.push(prices[i]);\n }\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n stack<int> values;\n int ans=0;\n int max=0;\n for(int i =prices.size()-1; i>=0;i--)\n {\n if(values.empty())\n {\n values.push(prices[i]);\n }\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n \n stack<int>st;\n st.push(prices[0]);\n int profit = 0;\n\n for(int i = 1 ; i<prices.size() ; i++){\n if(prices[i] < st.top()){\n st.pop();\n st.push(prices[i]);\n...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n //Dynamic programing.\n //Find function that computes every state:\n //state is the day you are currently on\n // dp(i) = profit if you sold assumig you bought at the min from previous days.\n //dp(i) =...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n if(n==0)return 0;\n\n\n\n\n int bestbuy[n];\n bestbuy[0]=prices[0];//remember this initialisation\n for(int i=1;i<n;i++){\n bestbuy[i]=min(bestbuy[i-1],prices[i-1]);\n\n }...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n\n int arr[prices.size()];\n int max=0;\n\n for(int i=prices.size()-1;i>=0;i--){\n\n arr[i]=max;\n if(max<prices[i]) max=prices[i];\n\n }\n max=0;\n\n for(int i=0; i<prices.s...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n int premin[n];\n premin[0]=prices[0];\n int suffmax[n];\n suffmax[n-1]=prices[n-1];\n for(int i=1;i<n;i++){\n premin[i]=min(prices[i],premin[i-1]);\n }\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n int dp[n];\n int mini[n];\n mini[0]=prices[0];\n dp[0]=0;\n for(int i=1;i<n;i++){\n dp[i]=max(prices[i]-mini[i-1],dp[i-1]);\n mini[i]=min(mini[i-1],...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n int mn[n];\n int mx[n];\n for (int i = 0; i < n; ++i) {\n if (i == 0) mn[i] = prices[i];\n else mn[i] = min(mn[i - 1], prices[i]);\n }\n for (int i ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int sum=0,j=0;\n int n=prices.size();\n int arr[n],arr1[n];\n for(int i=0;i<n;i++)\n {if(i==0)\n {\n arr[i]=0;\n }\n else\n {\n arr[i]=prices[i]-prices[...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n \n void maxToItsRight(int arr[], int n, vector<int>& prices){\n arr[n-1] = prices[n-1] ;\n for(int i = n-2 ; i >=0 ; i--){\n arr[i] = max(arr[i+1], prices[i]);\n }\n }\n \n void minToItsLeft(int arr[], int n, vector<int>& prices){\n...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int buyday = prices[0];\n int profit = 0, max = -1;\n vector<int> profits;\n profit = 0;\n for(int i = 1; i<prices.size();i++){\n if (prices[i]<buyday){\n buyday = prices[i];\n...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n\nint findMaxProfitCrossing(const std::vector<int>& prices, int low, int mid, int high) {\n int minPrice = INT_MAX;\n for (int i = low; i <= mid; ++i) {\n minPrice = std::min(minPrice, prices[i]);\n }\n \n int maxProfit = 0;\n for (int i = mid + 1; i <= h...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n\nint findMaxProfitCrossing(const std::vector<int>& prices, int low, int mid, int high) {\n int minPrice = INT_MAX;\n for (int i = low; i <= mid; ++i) {\n minPrice = std::min(minPrice, prices[i]);\n }\n \n int maxProfit = 0;\n for (int i = mid + 1; i <= h...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n\nint findMaxProfitCrossing(const std::vector<int>& prices, int low, int mid, int high) {\n int minPrice = INT_MAX;\n for (int i = low; i <= mid; ++i) {\n minPrice = std::min(minPrice, prices[i]);\n }\n \n int maxProfit = 0;\n for (int i = mid + 1; i <= h...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int i=0,ans=0,smallest;\n stack<int>st;\n\n while(i<prices.size())\n {\n while(!st.empty() && prices[i]<st.top())\n st.pop();\n\n st.push(prices[i]);\n if(st.siz...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\nint dp[100001][2][2];\n int maxProfit(vector<int>& prices) {\n\n int buy=prices[0];\n int profit=0;\n for(int i=1;i<prices.size();i++){\n buy=min(buy,prices[i-1]);\n profit=max(profit,prices[i]-buy);\n }\n return profit;...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int maxProfit = 0;\n std::deque<int> maximums;\n maximums.push_back(1);\n for(int i = 2; i < prices.size(); i++){\n while(maximums.size()>0 && prices[i] > prices[maximums.back()]){\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int maxProfit = 0;\n std::deque<int> maximums;\n maximums.push_back(1);\n for(int i = 2; i < prices.size(); i++){\n while(maximums.size()>0 && prices[i] > prices[maximums.back()]){\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int maxProfit = 0;\n std::deque<int> maximums;\n maximums.push_back(1);\n for(int i = 2; i < prices.size(); i++){\n while(maximums.size()>0 && prices[i] > prices[maximums.back()]){\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(std::vector<int>& prices) {\n int max_diff{};\n std::set<int> days;\n days.insert(prices[0]);\n for (int i = 1; i < prices.size(); ++i) {\n if (prices[i] - *days.begin() > max_diff) {\n max_diff = prices[i] -...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& p) {\n set<int>st;\n int n=p.size();\n st.insert(p[n-1]);\n int ans=0;\n for(int i=n-2;i>=0;i--){\n auto it=st.end();\n it--;\n ans=max(ans,*(it)-p[i]);\n st.insert(p[i]...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n vector<int> v;\n int mxProfit = 0;\n for(auto &e:prices){\n while(v.size()>0 && v.back()>e)v.pop_back();\n v.push_back(e);\n int currProfit = v.back()-v.front();\n mxProfit...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n vector<int>v;\n v.push_back(prices[0]);\n int ans=0;\n int n=prices.size();\n for(int i=1;i<n;i++){\n while(!v.empty() && prices[i]<v.back())v.pop_back();\n v.push_back(prices[i]);...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& a) {\n unordered_map <int,int> m;\n int n = a.size();\n int minimum = a[0];\n int maximum = 0;\n for(int i =0;i<n;i++)\n {\n minimum = min(minimum,a[i]);\n m[a[i]] = minimum;\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& a) {\n unordered_map <int,int> m;\n int n = a.size();\n int minimum = a[0];\n int maximum = 0;\n for(int i =0;i<n;i++)\n {\n minimum = min(minimum,a[i]);\n m[a[i]] = minimum;\n ...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n if (prices.size() == 1)\n return 0;\n \n unordered_map<int, int> m;\n m[prices.back()] = 0;\n \n for(int i = prices.size() - 2; i >= 0; --i) {\n if (prices[i] < prices[i + 1...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n if (prices.size() == 1)\n return 0;\n \n unordered_map<int, int> m;\n m[prices.back()] = 0;\n \n for(int i = prices.size() - 2; i >= 0; --i) {\n if (prices[i] < prices[i + 1...
121
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
3
{ "code": "class Solution {\npublic:\n int maxProfitRec(int i, int mini, vector<int>& prices) {\n // Base case: If we have checked all days, return 0\n if (i == prices.size()) return 0;\n \n // Calculate current day's profit if we sell today\n int profitToday = prices[i] - mini;\...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int init = [] {\n cin.tie(nullptr)->sync_with_stdio(false);\n freopen(\"user.out\", \"w\", stdout);\n\n for (string s; getline(cin, s);) {\n int pr = INT_MAX, solution = 0;\n for (int i = 1, n = s.length(); i < n; ++i) {\n int price = s[i++] & 15;\n while ((s[i]...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n int prev, curr;\n char ch;\n\n ss >> ch >> prev >> ch;\n\n while (...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "int main() {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream cout(\"user.out\");\n\n string tmp;\n\n while (cin >> tmp) {\n int res = 0;\n stringstream ss(tmp);\n\n char ch;\n ss >> ch;\n\n int prev = INT_MAX, curr;\n\n while (ss >...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int result = 0;\n for (int i = 1; i < prices.size(); i++)\n {\n if (prices[i] > prices[i-1])\n result += prices[i]-prices[i-1];\n }\n \n return result;\n }\n};", "m...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& nums) {\n int t=nums.size();\n int maxans=0,buy=1000;\n for(int i=0;i<t;i++){\n if(nums[i]<buy){\n buy=nums[i];\n while(i+1<t && nums[i+1]>nums[i]){\n i++;\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int i = 0;\n int profit = 0;\n int min = prices[0];\n int size = prices.size();\n for (int i = 0; i < size - 1; i++)\n {\n if (prices[i] < prices[i + 1])\n {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\n\npublic:\n\n int maxProfit(vector<int>& nums) {\n\n int n = nums.size();\n\n int ans=0;\n\n int mx=nums[0];\n\n int mn=nums[0];\n\n for(int i=1;i<n;){\n\n while(i<n && nums[i]>=nums[i-1]){\n\n mx=nums[i];\n\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int profit=0;\n int n=prices.size();\n for(int i=1;i<n;i++){\n if(prices[i]>prices[i-1]) profit+=prices[i]-prices[i-1];\n }\n return profit;\n \n }\n};", "memory": "15700" }
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int ans = 0;\n\n for (int i = 1; i < prices.size(); ++i) {\n if (prices[i] > prices[i - 1]) {\n ans += prices[i] - prices[i - 1];\n }\n }\n\n return ans;\n }\n};", "me...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int max =0;\n int start = prices[0];\n int len = prices.size();\n for(int i=1; i<len; i++){\n if(start < prices[i]){\n max += prices[i] -start;\n\n }\n start = p...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size(); int mini = prices[0];\n int maxP = 0;\n for (int i = 1; i < n; i++) {\n if (mini < prices[i]) {\n maxP += prices[i] - mini;\n }\n mini = prices[i...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
0
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int profit = 0;\n for (int i = 0; i < prices.size()-1; ++i){\n int n = prices[i+1] - prices[i];\n if (n > 0){\n profit += n;\n }\n }\n return profit;\n }\n};"...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int profit = 0;\n int minp=0;\n int mini=prices[0];\n\n for(int i : prices){\n minp = max(minp,i-mini);\n mini =min(mini,i);\n if(minp>0){\n profit+=minp;\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n\n int maxProfit(vector<int>& prices) {\n int n=prices.size();\n if(n==0){\n return 0;\n }\n vector<int> next(2,0),curr(2,-1);\n for(int i=n-1;i>=0;i--){\n curr[1] = max(0+next[1],next[0]-prices[i]);\n curr[0]...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int n = prices.size();\n vector<int> postProfit(n);\n postProfit[n-1] = 0;\n for (int i=n-2; i>=0; i--) {\n int buy = prices[i];\n int max = 0;\n for (int j=i+1; j<n-1; j++) {\...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n \n int maxProfit(vector<int>& prices) {\n vector <int> dp (prices.size(), 0); // init\n int diff = 0;\n int size = prices.size();\n int sum = 0;\n for(int i = 0; i < size; i ++) {\n for(int j = i + 1; j < size; j ++) {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "bool isMinima(vector<int> &prices,int k){\n int n=prices.size();\n if(k==n-1)return false;\n if(k==0&&prices[k]<prices[k+1])return true;\n if(k==0&&prices[k]>prices[k+1])return false;\n if(prices[k]<prices[k-1]&&prices[k]<prices[k+1])return true;\n return false;\n}\n\nbool isMaxima(vector...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n //dp[i].first - max при hold stock\n //dp[i].second - max при отсутсвующем hold stock\n vector<pair<int, int> > dp(prices.size());\n int result = 0;\n dp[0].first = -prices[0];\n\n for (int i = 1...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int solve(int index,vector<int>& prices, int buy){\n if(index==prices.size()){\n return 0;\n }\n int profit = 0;\n if(buy){\n profit = max((-prices[index])+solve(index+1,prices,0),(0+solve(index+1,prices,1)));\n }\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n // profit(1,prices);\n stack<int> st;\n int n=prices.size(),i=0;\n vector<int> v(n,-1);\n while(i<n)\n {\n if(st.empty())st.push(i);\n else\n {\n w...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution \n{\npublic:\n // int dp(vector<int>& prices, int i, vector<vector<int>>& memo, bool holding)\n // {\n // if (i >= prices.size())\n // {\n // return 0;\n // }\n\n // if (memo[holding][i] != -1)\n // {\n // return memo[holding][i]...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int maxProfit(vector<int>& prices) {\n int size=prices.size();\n vector<int>ans(size-1);\n int sum=0;\n for(int a=1;a<size;a++){\n ans.push_back(prices[a]-prices[a-1]);\n }\n for(int a=0;a<ans.size();a++){\n if(a...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int pro(vector<int>& prices, int n, int index) {\n if (index >= n) {\n return 0;\n }\n \n int buy = prices[index];\n int sell = 0;\n \n int profit = 0;\n\n if (index + 1 < n && prices[index + 1] > buy) {\n ...
122
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
2
{ "code": "class Solution {\npublic:\n int func(int index,int canbuy,int bought,vector<int> prices){\n if(index==prices.size()) return 0;\n if(canbuy){\n return max(func(index+1,true,bought,prices),func(index+1,false,prices[index],prices));\n }\n else{\n return max...