Skip to content

Cpp problems #47

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

Open
wants to merge 2 commits into
base: master
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
46 changes: 46 additions & 0 deletions C++/Solution#3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
3. Longest Substring Without Repeating Characters
Medium


Given a string s, find the length of the longest substring without repeating characters.



Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

*/

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int beg=0;
int end=0;
unsigned long int len=0;
set<char> st;
while(end<s.length())
{
if(!(st.count(s[end])))
{
st.insert(s[end]);
len=max(st.size(),len);
end++;
}
else
{
st.erase(s[beg]);
beg++;
}
}
return len;
}
};
57 changes: 57 additions & 0 deletions C++/Solution#42.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
42. Trapping Rain Water
Hard

Example 1:


Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9

*/

class Solution {
public:
int trap(vector<int>& height) {
if(height.size()==0)
return 0;
int i,pi=0,wt=0,s=(int)(height.size()-1),tp=0;
int p=height[0];
for(i=1;i<=s;i++)
{
if(height[i]>p)
{
pi=i;
p=height[i];
tp=0;
}
else
{
wt+=p-height[i];
tp+=p-height[i];
}
}
if(pi<s)
{
wt-=tp;
p=height[s];
for(i=s-1;i>=pi;i--)
{
if(height[i] >= p)
{
p = height[i];
}
else
{
wt += p - height[i];
}
}
}
return wt;
}
};
2 changes: 2 additions & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@

**Caitlin Genna** --> "CS Computer Science Graduate, Fordham University." --> [caitlingenna](https://github.com/caitlingenna)

**Utkarsh Nigam** --> "Learner | C.S undergrad" --> [utkarsh-7](https://github.com/utkarsh-7)