Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week_1_Anjali_Singh #142

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Miscellaneous/Binary_Search/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int BinarySearch(vector<int>nums, int l, int r, int target){
if(r>=l){
int mid = l + (r-l)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target);
else return BinarySearch(nums, mid+1, r, target);
}
return -1;
}
int search(vector<int>& nums, int target) {
int x = BinarySearch(nums, 0, nums.size()-1, target);
return x;
}
};
12 changes: 12 additions & 0 deletions Miscellaneous/Duplicate_Number/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int findDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
for(int i=0; i<nums.size(); i++){
if(nums[i]==nums[i+1])
return nums[i];

}
return NULL;
}
};
16 changes: 16 additions & 0 deletions Miscellaneous/Peek_Index/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int BinarySearch(vector<int>nums, int l, int r, int target){
if(r>=l){
int mid = l + (r-l)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]>target) return BinarySearch(nums, l, mid-1, target);
else return BinarySearch(nums, mid+1, r, target);
}
return -1;
}
int search(vector<int>& nums, int target) {
int x = BinarySearch(nums, 0, nums.size()-1, target);
return x;
}
};
50 changes: 50 additions & 0 deletions Stacks/BackSpace_String_Compare/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Solution {
public:
bool isEqual(stack<char> &stack1, stack<char> &stack2) {
if (stack1.empty() && stack2.empty())
return true;
else if (stack1.empty() || stack2.empty())
return false;
while (!stack1.empty() && !stack2.empty())
{
if (stack1.top() != stack2.top())
return false;
stack1.pop();
stack2.pop();
}
if (stack1.empty() && stack2.empty())
return true;
else
return false;
}

bool backspaceCompare(string S, string T) {
stack<char> stack1;
stack<char> stack2;
for(char s:S){
if(s=='#' && !stack1.empty()){
// cout<<s<<" ";
// cout<<stack1.top();
stack1.pop();
//cout<<stack1.top();
}
else if(s!='#'){
//cout<<s<<endl;
stack1.push(s);
}
}
for(char t:T){
if(t=='#' && !stack2.empty()){
stack2.pop();
}
else if(t!='#'){
//cout<<t<<" ";
stack2.push(t);
}
}
cout<<stack1.size()<<" "<<stack2.size()<<endl;
// cout<<isEqual(stack1, stack2);
return isEqual(stack1, stack2);

}
};
21 changes: 21 additions & 0 deletions Stacks/Make_string_great/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
string makeGood(string s) {
stack<char> stk;
string ans;
for(char i: s){
if(!stk.empty() && (i==stk.top()-32||i==stk.top()+32)){
cout<<stk.top()+32;
cout<<i<<" "<<stk.top();
stk.pop();
ans.pop_back();
}
else{
stk.push(i);
ans.push_back(i);
}
}
return ans;
}

};
24 changes: 24 additions & 0 deletions Stacks/Next_Greater_Element/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {

stack<int> st;
unordered_map<int,int> hashmap;
for(int i=nums2.size()-1;i>=0;i--)
{
while(!st.empty()&&st.top()<nums2[i])
st.pop();
if(st.empty())
hashmap[nums2[i]]=-1;
else
hashmap[nums2[i]]=st.top();
st.push(nums2[i]);
}
for(int i=0;i<nums1.size();i++)
{
nums1[i]=hashmap[nums1[i]];
}
return nums1;

}
};
24 changes: 24 additions & 0 deletions Stacks/Remove_Adjacent_Duplicates_In_String/Anjali_SIngh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
string removeDuplicates(string S) {
stack<char> stk;
string ans;
for(char s: S){
if(!stk.empty() && s==stk.top())
{
stk.pop();
}
else{
stk.push(s);
}
}
while(!stk.empty()){
ans.push_back(stk.top());
cout<<stk.top()<<" ";
stk.pop();
}
reverse(ans.begin(), ans.end());
return ans;

}
};
49 changes: 49 additions & 0 deletions Stacks/Remove_Outermost_Parentheses/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Solution {
public:
string removeOuterParentheses(string S) {

stack <char> st;
string ans;



for(char& c : S)
{
//for opening brackets
// if it is not the outer most bracket then we will take it into ans string and push it to stack for later comparison.
if (c == '(' && st.size()>=1)
{
ans.push_back('(');
st.push('(');
}
// if c is outer most bracket then push it into stack for later comparison
else
{
if (c == '(' && st.size() == 0)
{
st.push('(');
}
}


//for closing brackets

if (c == ')' && st.size()>1)
{
ans.push_back(')');
st.pop();
}
else
{
if (c == ')' && st.size() == 1)
{
st.pop();
}
}
}


return ans;

}
};
32 changes: 32 additions & 0 deletions Stacks/Reverse_Polish_Notation/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int evalRPN(vector<string>& tokens) {
unordered_set<string> operands = {"+", "-", "*", "/"};
stack<int> s;
for (string token : tokens) {
// We have a number
if (operands.find(token) == operands.end()) {
s.push(stoi(token));
}
else { // We have an operand
int b = s.top();
s.pop();
int a = s.top();
s.pop();
if (token == "+") {
s.push(a + b);
}
else if (token == "-") {
s.push(a - b);
}
else if (token == "*") {
s.push(a * b);
}
else if (token == "/") {
s.push(a / b);
}
}
}
return s.top();
}
};
20 changes: 20 additions & 0 deletions Stacks/Valid_Parentheses/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (char c : s) {
if (c == '(' || c == '{' || c == '[') {
stk.push(c);
continue;
}
// now we've all closing brackets
if (stk.empty()) return false;

if (c == ')' && stk.top() != '(') return false;
if (c == '}' && stk.top() != '{') return false;
if (c == ']' && stk.top() != '[') return false;
stk.pop();
}
return stk.empty();
}
};
15 changes: 15 additions & 0 deletions Week1/Count Negative Nos/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int r=grid.size(), c=grid[0].size();
int count=0;
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(grid[i][j]<0){
count++;
}
}
}
return count;
}
};
18 changes: 18 additions & 0 deletions Week1/Count and Say/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
string countAndSay(int n) {
if(n==1)
return "1";

string res;
string pres = countAndSay(n-1);
int start = 0, i = 1;
for( ; i<pres.size(); i++ ){
if( pres[i]!=pres[i-1] ){
res += to_string(i-start) + pres[i-1];
start = i;
}
}
return res + to_string(i-start) + pres[i-1];
}
};
24 changes: 24 additions & 0 deletions Week1/Longest Substring wo repeating chars/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res=0,n=s.size(),left=0;
unordered_map<char,int>mymap;
for(int i=0; i<n ;++i){
if(mymap.find(s[i])==mymap.end()){
mymap[s[i]]=i;
}
else{
if(mymap[s[i]]>=left){
left=mymap[s[i]]+1;
mymap[s[i]]=i;

}
else{
mymap[s[i]]=i;
}
}
res=max(res,i-left+1);
}
return res;
}
};
17 changes: 17 additions & 0 deletions Week1/Max Subarray/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum=0, maxSum=INT_MIN;
for(int i=0; i<nums.size(); i++){
sum += nums[i];
if(sum>maxSum){
maxSum=sum;
}
if(sum<0){
sum=0;
}
}
return maxSum;

}
};
14 changes: 14 additions & 0 deletions Week1/Missing no/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
cout<<nums.back();
if(nums[0]!=0) return 0;
for(int i=1;i<nums.size();i++){
if(nums[i]-nums[i-1]!=1){
return i;
}
}
return nums.back()+1;
}
};
20 changes: 20 additions & 0 deletions Week1/Monotonic Array/Anjali_Singh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isMonotonic(vector<int>& A) {
int len = A.size();
int count1 = 1;
int count2 = 1;
for(int i = 1; i < len; i++){
if(A[i] >= A[i-1]){
count1++;
}
if(A[i] <= A[i-1]){
count2++;
}
}
if(count1 == len or count2 == len){
return true;
}
return false;
}
};
Loading