diff --git a/Week1/Count Negative Nos/Shriya_Rai b/Week1/Count Negative Nos/Shriya_Rai new file mode 100644 index 0000000..6085ee4 --- /dev/null +++ b/Week1/Count Negative Nos/Shriya_Rai @@ -0,0 +1,41 @@ +class Solution { +public: + int check(vector &vec) + { + int n=vec.size(); + int l=0,r=n-1,mid; + while(l<=r) + { + mid=l+(r-l)/2; + if(vec[mid]<0) + { + r=mid-1; + } + else + { + l=mid+1; + } + } + if(vec[mid]>=0 && mid==n-1) + { + return 0; + } + else if(vec[mid]>=0) + { + return n-mid-1; + } + else + { + return n-mid; + } + } + + int countNegatives(vector>& grid) { + int c=0; + for(auto &v: grid) + { + c=c+check(v); + } + return c; + } +}; diff --git a/Week1/Count and Say/Shriya_Rai b/Week1/Count and Say/Shriya_Rai new file mode 100644 index 0000000..be28520 --- /dev/null +++ b/Week1/Count and Say/Shriya_Rai @@ -0,0 +1,20 @@ +class Solution { +public: + string countAndSay(int n) { + if (n == 0) return ""; + string res = "1"; + while (--n) { + string cur = ""; + for (int i = 0; i < res.size(); i++) { + int count = 1; + while ((i + 1 < res.size()) && (res[i] == res[i + 1])){ + count++; + i++; + } + cur += to_string(count) + res[i]; + } + res = cur; + } + return res; + } +}; diff --git a/Week1/Longest Substring wo repeating chars/Shriya_Rai b/Week1/Longest Substring wo repeating chars/Shriya_Rai new file mode 100644 index 0000000..c874d1e --- /dev/null +++ b/Week1/Longest Substring wo repeating chars/Shriya_Rai @@ -0,0 +1,14 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + vector dict(256, -1); + int maxLen = 0, start = -1; + for (int i = 0; i != s.length(); i++) { + if (dict[s[i]] > start) + start = dict[s[i]]; + dict[s[i]] = i; + maxLen = max(maxLen, i - start); + } + return maxLen; + } +}; diff --git a/Week1/Max Subarray/Shriya_Rai b/Week1/Max Subarray/Shriya_Rai new file mode 100644 index 0000000..fccf79f --- /dev/null +++ b/Week1/Max Subarray/Shriya_Rai @@ -0,0 +1,14 @@ +int sum=0; + int maxsum=INT_MIN; + for(int i=0;imaxsum) + { + maxsum=sum; + } + if (sum<0) + sum=0; + } + return maxsum; + } diff --git a/Week1/Missing no/Shriya_Rai b/Week1/Missing no/Shriya_Rai new file mode 100644 index 0000000..348a1ea --- /dev/null +++ b/Week1/Missing no/Shriya_Rai @@ -0,0 +1,12 @@ +class Solution { +public: + int missingNumber(vector& nums) { + int n=nums.size(); + int tsum=n*(n+1)/2; + int sum=0; + for(int i=0;i& A) { + bool isIncreasing = A[0] < A[A.size()-1]; //Checking array is increasing + + for(int i = 0; i < A.size() - 1 ; i++) + { + if(isIncreasing) + { + if(A[i] > A[i+1]) + return false; + } + else + { + if(A[i] < A[i+1]) + return false; + } + } + + return true; + } +}; diff --git a/Week1/Move Zeroes/Shriya_Rai b/Week1/Move Zeroes/Shriya_Rai new file mode 100644 index 0000000..919459e --- /dev/null +++ b/Week1/Move Zeroes/Shriya_Rai @@ -0,0 +1,12 @@ +class Solution { +public: + void moveZeroes(vector& nums) { + if(nums.size() == 0) return; + int p = -1; + int i = 0; + while(i < nums.size()){ + if(nums[i] != 0) swap(nums[++p], nums[i]); + i++; + } + } +}; diff --git a/Week1/Pascal Triangle/Shriya_Rai b/Week1/Pascal Triangle/Shriya_Rai new file mode 100644 index 0000000..eba8389 --- /dev/null +++ b/Week1/Pascal Triangle/Shriya_Rai @@ -0,0 +1,15 @@ +class Solution { +public: + vector> generate(int numRows) { + vector>ans; + for(int i=0;irow(i+1,1); + for(int j=1;j& nums) { + int leftSum=0, rightSum=0, sum=0; + for(int i=0; i productExceptSelf(vector& nums) { + int length = nums.size(); + + vector L(length); + vector R(length); + + + vector answer(length); + + + + L[0] = 1; + for (int i = 1; i < length; i++) { + + L[i] = nums[i - 1] * L[i - 1]; + } + + + R[length - 1] = 1; + for (int i = length - 2; i >= 0; i--) { + + + R[i] = nums[i + 1] * R[i + 1]; + } + + + for (int i = 0; i < length; i++) { + + answer[i] = L[i] * R[i]; + } + + return answer; + } +}; diff --git a/Week1/Remove Duplicates/Shriya_Rai b/Week1/Remove Duplicates/Shriya_Rai new file mode 100644 index 0000000..1aa0fd0 --- /dev/null +++ b/Week1/Remove Duplicates/Shriya_Rai @@ -0,0 +1,15 @@ +class Solution { +public: + int removeDuplicates(vector& nums) { + int n=nums.size(); + if (n==0) return 0; + int j=1; + for(int i=0;i& nums,int low,int high){ + while(low& nums, int k) { + int n=nums.size(); + k%=n; + reversearray(nums,0,n-1); + reversearray(nums,0,k-1); + reversearray(nums,k,n-1); + + } +}; diff --git a/Week1/Spiral Matrix/Shriya_Rai b/Week1/Spiral Matrix/Shriya_Rai new file mode 100644 index 0000000..e034cec --- /dev/null +++ b/Week1/Spiral Matrix/Shriya_Rai @@ -0,0 +1,34 @@ +class Solution { +public: + vector spiralOrder(vector>& matrix) { + vector curPos {0,0}; + vector result; + vector> dirs {{0,1},{1,0},{0,-1},{-1,0}}; + int curDir = 0; + + int RSize = matrix.size(); + int CSize = matrix[0].size(); + int numsSize = CSize * RSize; + + for(int i=0; i< numsSize; i++) { + //cout<= CSize || nextC < 0 || nextR >= RSize || nextR < 0 || matrix[nextR][nextC] == 101) { + nextR -= dirs[curDir][0]; + nextC -= dirs[curDir][1]; + curDir++; + if(curDir > 3) curDir = 0; + nextR += dirs[curDir][0]; + nextC += dirs[curDir][1]; + } + curPos[0] = nextR; + curPos[1] = nextC; + } + return result; + } +}; diff --git a/Week1/Target Array/Shriya_Rai b/Week1/Target Array/Shriya_Rai new file mode 100644 index 0000000..7fccc6e --- /dev/null +++ b/Week1/Target Array/Shriya_Rai @@ -0,0 +1,9 @@ +class Solution { +public: + vector createTargetArray(vector& nums, vector& index) { + vector ans; + for(int i=0;i<(int)nums.size();i++) + ans.insert(ans.begin()+index[i],nums[i]); + return ans; + } +}; diff --git a/Week1/Valid Palindrome/Shriya_Rai b/Week1/Valid Palindrome/Shriya_Rai new file mode 100644 index 0000000..2ef18b2 --- /dev/null +++ b/Week1/Valid Palindrome/Shriya_Rai @@ -0,0 +1,12 @@ +class Solution { +public: + bool isPalindrome(string s) { + for (int i = 0, j = s.size() - 1; i < j; i++, j--) { + while (isalnum(s[i]) == false && i < j) i++; + while (isalnum(s[j]) == false && i < j) j--; + if (toupper(s[i]) != toupper(s[j])) return false; + } + + return true; + } +}; diff --git a/Week1/Xor operation/Shriya_Rai b/Week1/Xor operation/Shriya_Rai new file mode 100644 index 0000000..41d2411 --- /dev/null +++ b/Week1/Xor operation/Shriya_Rai @@ -0,0 +1,10 @@ +class Solution { +public: + int xorOperation(int n, int start) { + int res = start; + for(int i = 1; i