This file contains hidden or 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
class Solution { | |
public: | |
vector<int> twoSum(vector<int>& nums, int target) { | |
int n=nums.size(); | |
vector<pair<int,int>> a; | |
for(int i=0;i<n;i++) | |
a.push_back({nums[i],i}); | |
sort(a.begin(),a.end()); | |
int l=0,r=n-1; | |
while(l<r) |
This file contains hidden or 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
class Solution { | |
public: | |
bool isValid(string s) { | |
stack<char> st; | |
int n=s.size(); | |
for(int i=0;i<n;i++) | |
{ | |
if(!st.empty() and ((s[i]==')' and st.top()=='(') or (s[i]=='}' and st.top()=='{') or (s[i]==']' and st.top()=='['))) | |
st.pop(); | |
else |
This file contains hidden or 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
class Solution { | |
public: | |
int maxProfit(vector<int>& prices) { | |
int n=prices.size(); | |
int profit=0; | |
int cur_min=prices[0]; | |
for(int i=1;i<n;i++) | |
{ | |
profit=max(profit,prices[i]-cur_min); | |
cur_min=min(cur_min,prices[i]); |
This file contains hidden or 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
class Solution { | |
public: | |
vector<int> runningSum(vector<int>& nums) { | |
int n=nums.size(); | |
vector<int> runningSum(n); | |
runningSum[0]=nums[0]; | |
for(int i=1;i<n;i++) | |
{ | |
runningSum[i]+=nums[i]+runningSum[i-1]; | |
} |
This file contains hidden or 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
class Solution { | |
public: | |
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) { | |
int n=candies.size(); | |
int max_candies=*max_element(candies.begin(),candies.end()); | |
vector<bool> result(n,false); | |
for(int i=0;i<n;i++) | |
{ | |
if(candies[i]+extraCandies >= max_candies) | |
result[i]=true; |
This file contains hidden or 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
class Solution { | |
public: | |
vector<int> shuffle(vector<int>& nums, int n) { | |
// swap(i,i+(n-1)) | |
int t_n=n+n; | |
for(int i = n;i < t_n ; i++) | |
{ | |
int ind=i; | |
int total_swaps=t_n-i-1; |
This file contains hidden or 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
class Solution { | |
public: | |
int maximumWealth(vector<vector<int>>& accounts) { | |
int ans=0; | |
for(int i=0;i<accounts.size();i++) | |
{ | |
int temp=0; | |
for(int j=0;j<accounts[i].size();j++) | |
{ | |
temp+=accounts[i][j]; |
This file contains hidden or 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
class Solution { | |
public: | |
int numIdenticalPairs(vector<int>& nums) { | |
map<int,int> frequency; | |
int n=nums.size(); | |
int ans=0; | |
for(int i=0;i<n;i++) | |
{ | |
ans+= frequency[nums[i]]; | |
frequency[nums[i]]++; |
This file contains hidden or 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
class Solution { | |
public: | |
int countMatches(vector<vector<string>>& items, string ruleKey, string ruleValue) { | |
int n=items.size(); | |
int matches=0; | |
for(int i=0;i<n;i++) | |
{ | |
if(ruleKey=="type" and items[i][0]==ruleValue) | |
matches++; | |
else if(ruleKey=="color" and items[i][1]==ruleValue) |
This file contains hidden or 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
class Solution { | |
public: | |
int numJewelsInStones(string jewels, string stones) { | |
vector<int> jwls(128); | |
int ans=0; | |
for(char c:jewels) | |
jwls[c]++; | |
for(char c: stones) | |
{ |
OlderNewer