-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
298 additions
and
137 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int maxProfit(vector<int>& prices) { | ||
int max_so_far=0; | ||
int max_ending_here=0; | ||
for(int i=1;i<prices.size();i++){ | ||
max_ending_here+=prices[i]-prices[i-1]; | ||
if(max_ending_here>max_so_far){ | ||
max_so_far=max_ending_here; | ||
int maxProfit(vector<int> &prices) | ||
{ | ||
int max_so_far = 0; | ||
int max_ending_here = 0; | ||
for (int i = 1; i < prices.size(); i++) | ||
{ | ||
max_ending_here += prices[i] - prices[i - 1]; | ||
if (max_ending_here > max_so_far) | ||
{ | ||
max_so_far = max_ending_here; | ||
} | ||
if(max_ending_here<0){ | ||
max_ending_here=0; | ||
if (max_ending_here < 0) | ||
{ | ||
max_ending_here = 0; | ||
} | ||
} | ||
return max_so_far; | ||
} | ||
|
||
int main(){ | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
vector <int> v; | ||
for(int i=0;i<n;i++){ | ||
cin >> n; | ||
vector<int> v; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
int x; | ||
cin>>x; | ||
cin >> x; | ||
v.push_back(x); | ||
} | ||
cout<<maxProfit(v)<<endl; | ||
cout << maxProfit(v) << endl; | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int findDup(vector<int> &arr) | ||
{ | ||
int slow = arr[0]; | ||
int fast = arr[0]; | ||
do | ||
{ | ||
slow = arr[slow]; | ||
fast = arr[arr[fast]]; | ||
} while (slow != fast); | ||
fast = arr[0]; | ||
while (slow != fast) | ||
{ | ||
slow = arr[slow]; | ||
fast = arr[fast]; | ||
} | ||
return slow; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<vector<int>> fourSum(vector<int> &nums, int target) | ||
{ | ||
vector<vector<int>> ans; | ||
if (nums.empty()) | ||
return ans; | ||
int n = nums.size(); | ||
sort(nums.begin(), nums.end()); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = i + 1; j < n; j++) | ||
{ | ||
int tar2 = target - nums[i] - nums[j]; | ||
int front = j + 1; | ||
int back = n - 1; | ||
while (front < back) | ||
{ | ||
int twosum = nums[front] + nums[back]; | ||
if (twosum < tar2) | ||
front++; | ||
else if (twosum > tar2) | ||
back--; | ||
else | ||
{ | ||
vector<int> quad; | ||
quad = {nums[i], nums[j], nums[front], nums[back]}; | ||
ans.push_back(quad); | ||
while (front < back && nums[front] == quad[2]) | ||
++front; | ||
while (front < back && nums[back] == quad[3]) | ||
--back; | ||
} | ||
} | ||
while (j + 1 < n && nums[j] == nums[j + 1]) | ||
++j; | ||
} | ||
while (i + 1 < n && nums[i] == nums[i + 1]) | ||
++i; | ||
} | ||
return ans; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int solve(int arr[],int n){ | ||
int max_so_far=INT_MIN; | ||
int solve(int arr[], int n) | ||
{ | ||
int max_so_far = INT_MIN; | ||
int max_ending_here = 0; | ||
for(int i=0;i<n;i++){ | ||
max_ending_here+=arr[i]; | ||
if(max_ending_here>max_so_far){ | ||
max_so_far=max_ending_here; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
max_ending_here += arr[i]; | ||
if (max_ending_here > max_so_far) | ||
{ | ||
max_so_far = max_ending_here; | ||
} | ||
if(max_ending_here<0){ | ||
max_ending_here=0; | ||
if (max_ending_here < 0) | ||
{ | ||
max_ending_here = 0; | ||
} | ||
} | ||
return max_so_far; | ||
} | ||
|
||
int main(){ | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
cin >> n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
cout<<solve(arr,n); | ||
cout<<endl; | ||
cout << solve(arr, n); | ||
cout << endl; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
#include<bits/stdc++.h> | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int kThMin(int arr[],int n,int k){ | ||
sort(arr,arr+n); | ||
return arr[k-1]; | ||
int kThMin(int arr[], int n, int k) | ||
{ | ||
sort(arr, arr + n); | ||
return arr[k - 1]; | ||
} | ||
|
||
int main() | ||
{ | ||
//code | ||
{ | ||
//code | ||
|
||
int n; | ||
cin>>n; | ||
cin >> n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
int k; | ||
cin>>k; | ||
cout<<kThMin(arr,n,k)<<endl; | ||
return 0; | ||
cin >> k; | ||
cout << kThMin(arr, n, k) << endl; | ||
return 0; | ||
} |
Empty file.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<vector<int>> merge(vector<vector<int>> &intervals) | ||
{ | ||
vector<vector<int>> ans; | ||
if (intervals.size() == 0) | ||
return ans; | ||
else | ||
{ | ||
sort(intervals.begin(), intervals.end()); | ||
vector<int> curr = intervals[0]; | ||
for (auto j : intervals) | ||
{ | ||
if (j[0] <= curr[1]) | ||
{ | ||
curr[1] = max(curr[1], j[1]); | ||
} | ||
else | ||
{ | ||
ans.push_back(curr); | ||
curr = j; | ||
} | ||
} | ||
ans.push_back(curr); | ||
return ans; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void solve(int arr[],int n){ | ||
void solve(int arr[], int n) | ||
{ | ||
int start = 0; | ||
int end = n-1; | ||
while(start<=end){ | ||
if(arr[start]<0&&arr[end]<0){ | ||
int end = n - 1; | ||
while (start <= end) | ||
{ | ||
if (arr[start] < 0 && arr[end] < 0) | ||
{ | ||
start++; | ||
} | ||
else if(arr[start]>0&&arr[end]>0){ | ||
else if (arr[start] > 0 && arr[end] > 0) | ||
{ | ||
end--; | ||
} else if(arr[start]>0&&arr[end]<0){ | ||
swap(arr[start],arr[end]); | ||
} | ||
else if (arr[start] > 0 && arr[end] < 0) | ||
{ | ||
swap(arr[start], arr[end]); | ||
start++; | ||
end--; | ||
} | ||
else{ | ||
else | ||
{ | ||
start++; | ||
end--; | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
cin >> n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
solve(arr,n); | ||
for(int i=0;i<n;i++){ | ||
cout<<arr[i]<<" "; | ||
solve(arr, n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout << arr[i] << " "; | ||
} | ||
cout<<endl; | ||
cout << endl; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
void solve(int arr[],int start,int end){ | ||
while(start<end){ | ||
void solve(int arr[], int start, int end) | ||
{ | ||
while (start < end) | ||
{ | ||
int temp = arr[start]; | ||
arr[start]= arr[end]; | ||
arr[end]=temp; | ||
arr[start] = arr[end]; | ||
arr[end] = temp; | ||
start++; | ||
end--; | ||
} | ||
} | ||
|
||
int main(){ | ||
int main() | ||
{ | ||
int n; | ||
cin>>n; | ||
cin >> n; | ||
int arr[n]; | ||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> arr[i]; | ||
} | ||
solve(arr,0,n-1); | ||
for(int i=0;i<n;i++){ | ||
cout<<arr[i]<<" "; | ||
solve(arr, 0, n - 1); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout << arr[i] << " "; | ||
} | ||
cout<<endl; | ||
cout << endl; | ||
} |
Binary file not shown.
Oops, something went wrong.