-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 13d3453
Showing
100 changed files
with
6,171 additions
and
0 deletions.
There are no files selected for viewing
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,46 @@ | ||
// 1047. Remove All Adjacent Duplicates In String | ||
// Solved | ||
// Easy | ||
// Topics | ||
// Companies | ||
// Hint | ||
// You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. | ||
|
||
// We repeatedly make duplicate removals on s until we no longer can. | ||
|
||
// Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique. | ||
|
||
|
||
|
||
// Example 1: | ||
|
||
// Input: s = "abbaca" | ||
// Output: "ca" | ||
// Explanation: | ||
// For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca". | ||
// Example 2: | ||
|
||
// Input: s = "azxxzy" | ||
// Output: "ay" | ||
|
||
|
||
// Constraints: | ||
|
||
// 1 <= s.length <= 105 | ||
// s consists of lowercase English letters. | ||
|
||
|
||
class Solution { | ||
public: | ||
string removeDuplicates(std::string s) { | ||
string ans; | ||
for(char c:s){ | ||
if(!ans.empty() && ans.back()==c){ | ||
ans.pop_back(); | ||
}else{ | ||
ans.push_back(c); | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
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,42 @@ | ||
// 1071. Greatest Common Divisor of Strings | ||
// Easy | ||
// Topics | ||
// Companies | ||
// Hint | ||
// For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times). | ||
|
||
// Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2. | ||
|
||
|
||
|
||
// Example 1: | ||
|
||
// Input: str1 = "ABCABC", str2 = "ABC" | ||
// Output: "ABC" | ||
// Example 2: | ||
|
||
// Input: str1 = "ABABAB", str2 = "ABAB" | ||
// Output: "AB" | ||
// Example 3: | ||
|
||
// Input: str1 = "LEET", str2 = "CODE" | ||
// Output: "" | ||
|
||
|
||
// Constraints: | ||
|
||
// 1 <= str1.length, str2.length <= 1000 | ||
// str1 and str2 consist of English uppercase letters. | ||
|
||
|
||
class Solution { | ||
public: | ||
string gcdOfStrings(string str1, string str2) { | ||
int n1=str1.size(); | ||
int n2=str2.size(); | ||
if(str1+str2 != str2+str1){ | ||
return ""; | ||
} | ||
return str1.substr(0,gcd(n1,n2)); | ||
} | ||
}; |
96 changes: 96 additions & 0 deletions
96
1171. Remove Zero Sum Consecutive Nodes from Linked List.cpp
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,96 @@ | ||
// 1171. Remove Zero Sum Consecutive Nodes from Linked List | ||
// Medium | ||
// Topics | ||
// Companies | ||
// Hint | ||
// Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences. | ||
|
||
// After doing so, return the head of the final linked list. You may return any such answer. | ||
|
||
|
||
|
||
// (Note that in the examples below, all sequences are serializations of ListNode objects.) | ||
|
||
// Example 1: | ||
|
||
// Input: head = [1,2,-3,3,1] | ||
// Output: [3,1] | ||
// Note: The answer [1,2,1] would also be accepted. | ||
// Example 2: | ||
|
||
// Input: head = [1,2,3,-3,4] | ||
// Output: [1,2,4] | ||
// Example 3: | ||
|
||
// Input: head = [1,2,3,-3,-2] | ||
// Output: [1] | ||
|
||
|
||
// Constraints: | ||
|
||
// The given linked list will contain between 1 and 1000 nodes. | ||
// Each node in the linked list has -1000 <= node.val <= 1000. | ||
|
||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
// */ | ||
// class Solution { | ||
// public: | ||
// ListNode* removeZeroSumSublists(ListNode* head) { | ||
// ListNode* temp=new ListNode(0); | ||
// temp->next=head; | ||
// int prefix=0; | ||
// unordered_map<int, ListNode*>mp; | ||
// mp[0]=temp; | ||
// while(head!=NULL){ | ||
// prefix+=head->val; | ||
// mp[prefix]=head; | ||
// head=head->next; | ||
// } | ||
// head=temp; | ||
// prefix=0; | ||
// while(head!=NULL){ | ||
// prefix+=head->val; | ||
// head->next=mp[prefix]->next; | ||
// head=head->next; | ||
// } | ||
// return temp->next; | ||
// } | ||
// }; | ||
|
||
class Solution { | ||
public: | ||
ListNode* removeZeroSumSublists(ListNode* head) { | ||
unordered_map<int,ListNode*> mp; | ||
ListNode* dummy=new ListNode(0); | ||
dummy->next=head; | ||
int prefix=0; | ||
mp[0]=dummy; | ||
while(head!=NULL){ | ||
prefix+=head->val; | ||
if (mp.find(prefix)!=mp.end()){ | ||
ListNode* prev=mp[prefix]->next; | ||
int sum=prefix+prev->val; | ||
while(sum!=prefix) { | ||
mp.erase(sum); | ||
prev=prev->next; | ||
sum+=prev->val; | ||
} | ||
mp[prefix]->next=head->next; | ||
} else { | ||
mp[prefix]=head; | ||
} | ||
head=head->next; | ||
} | ||
|
||
return dummy->next; | ||
} | ||
}; |
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,141 @@ | ||
// 12. Integer to Roman | ||
// Medium | ||
// Topics | ||
// Companies | ||
// Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. | ||
|
||
// Symbol Value | ||
// I 1 | ||
// V 5 | ||
// X 10 | ||
// L 50 | ||
// C 100 | ||
// D 500 | ||
// M 1000 | ||
// For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. | ||
|
||
// Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: | ||
|
||
// I can be placed before V (5) and X (10) to make 4 and 9. | ||
// X can be placed before L (50) and C (100) to make 40 and 90. | ||
// C can be placed before D (500) and M (1000) to make 400 and 900. | ||
// Given an integer, convert it to a roman numeral. | ||
|
||
|
||
|
||
// Example 1: | ||
|
||
// Input: num = 3 | ||
// Output: "III" | ||
// Explanation: 3 is represented as 3 ones. | ||
// Example 2: | ||
|
||
// Input: num = 58 | ||
// Output: "LVIII" | ||
// Explanation: L = 50, V = 5, III = 3. | ||
// Example 3: | ||
|
||
// Input: num = 1994 | ||
// Output: "MCMXCIV" | ||
// Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. | ||
|
||
|
||
// Constraints: | ||
|
||
// 1 <= num <= 3999 | ||
|
||
// class Solution { | ||
// public: | ||
// string intToRoman(int num) { | ||
// string Roman=""; | ||
// vector<pair<int,string>> | ||
// storeIntRoman={ | ||
// {1000,"M"}, | ||
// {900,"CM"}, | ||
// {500,"D"}, | ||
// {400,"CD"}, | ||
// {100,"C"}, | ||
// {90,"XC"}, | ||
// {50,"L"}, | ||
// {40,"XL"}, | ||
// {10,"X"}, | ||
// {9,"IX"}, | ||
// {5,"V"}, | ||
// {4,"IV"}, | ||
// {1,"I"} | ||
// }; | ||
|
||
// for(int i=0;i<storeIntRoman.size();i++){ | ||
// while(num>=storeIntRoman[i].first){ | ||
// Roman+=storeIntRoman[i].second; | ||
// num-=storeIntRoman[i].first; | ||
// } | ||
// } | ||
// return Roman; | ||
// } | ||
// }; | ||
|
||
//Another Method -> thou[num/1000] + hund[(num%1000)/100] + tens[(num%100)/10] + ones[num%10]; | ||
|
||
class Solution { | ||
public: | ||
string intToRoman(int num) { | ||
string roman=""; | ||
while(num>=1000){ | ||
roman+="M"; | ||
num-=1000; | ||
} | ||
if(num>=900){ | ||
roman+="CM"; | ||
num-=900; | ||
} | ||
while(num>=500){ | ||
roman+="D"; | ||
num-=500; | ||
} | ||
if(num>=400){ | ||
roman+="CD"; | ||
num-=400; | ||
} | ||
while(num>=100){ | ||
roman+="C"; | ||
num-=100; | ||
} | ||
if(num>=90){ | ||
roman+="XC"; | ||
num-=90; | ||
} | ||
if(num>=50){ | ||
roman+="L"; | ||
num-=50; | ||
} | ||
if(num>=40){ | ||
roman+="XL"; | ||
num-=40; | ||
} | ||
|
||
while(num>=10){ | ||
roman+="X"; | ||
num-=10; | ||
} | ||
|
||
if(num==9){ | ||
roman+="IX"; | ||
num-=9; | ||
} | ||
|
||
if(num>=5){ | ||
roman+="V"; | ||
num-=5; | ||
} | ||
if(num==4){ | ||
roman+="IV"; | ||
num=num-4; | ||
} | ||
while(num>=1){ | ||
roman+="I"; | ||
num=num-1; | ||
} | ||
return roman; | ||
} | ||
}; |
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,45 @@ | ||
// 121. Best Time to Buy and Sell Stock | ||
// Solved | ||
// Easy | ||
// Topics | ||
// Companies | ||
// You are given an array prices where prices[i] is the price of a given stock on the ith day. | ||
|
||
// You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. | ||
|
||
// Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. | ||
|
||
|
||
|
||
// Example 1: | ||
|
||
// Input: prices = [7,1,5,3,6,4] | ||
// Output: 5 | ||
// Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. | ||
// Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. | ||
// Example 2: | ||
|
||
// Input: prices = [7,6,4,3,1] | ||
// Output: 0 | ||
// Explanation: In this case, no transactions are done and the max profit = 0. | ||
|
||
|
||
// Constraints: | ||
|
||
// 1 <= prices.length <= 105 | ||
// 0 <= prices[i] <= 104 | ||
|
||
|
||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
int profit=0; | ||
int minp=prices[0]; | ||
for(int i=0;i<prices.size();i++){ | ||
int cost=prices[i]-minp; | ||
profit=max(profit,cost); | ||
minp=min(minp,prices[i]); | ||
} | ||
return profit; | ||
} | ||
}; |
Oops, something went wrong.