diff --git a/problem.CPP b/problem.CPP new file mode 100644 index 0000000..55f1989 --- /dev/null +++ b/problem.CPP @@ -0,0 +1,26 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + int ans = 0; + int n = s.size(); + int temp = 0; + int h[128] = {0}; + for(int i = 0;i 0) + { + if(h[int(s[i])] >= 2) + for(int j = h[int(s[i])] - 2 ;j >= (i - temp);j--){ + h[int(s[j])] = 0; + } + ans = max(ans,temp); + temp = i - h[int(s[i])] + 1; + h[int(s[i])] = i + 1; + } + else{ + temp++; + h[int(s[i])] = i + 1; + } + } + return max(ans,temp); + } +}; diff --git a/trappingRainwater.py b/trappingRainwater.py new file mode 100644 index 0000000..20086ea --- /dev/null +++ b/trappingRainwater.py @@ -0,0 +1,21 @@ +class Solution(object): + def trap(self, height): + """ + :type height: List[int] + :rtype: int + """ + n=len(height) + left=[0]*n + right=[0]*n + maxi=-876543 + for i in range(0,n): + left[i]=max(maxi,height[i]) + maxi=left[i] + maxi=-876543 + for i in range(n-1,-1,-1): + right[i]=max(maxi,height[i]) + maxi=right[i] + ans=0 + for i in range(0,n): + ans+=min(left[i],right[i])-height[i] + return ans