Skip to content
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
41 changes: 41 additions & 0 deletions Week1/Count Negative Nos/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
int check(vector<int> &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<vector<int>>& grid) {
int c=0;
for(auto &v: grid)
{
c=c+check(v);
}
return c;
}
};
20 changes: 20 additions & 0 deletions Week1/Count and Say/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -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;
}
};
14 changes: 14 additions & 0 deletions Week1/Longest Substring wo repeating chars/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> 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;
}
};
14 changes: 14 additions & 0 deletions Week1/Max Subarray/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
int sum=0;
int 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;
}
12 changes: 12 additions & 0 deletions Week1/Missing no/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n=nums.size();
int tsum=n*(n+1)/2;
int sum=0;
for(int i=0;i<n;i++)
{sum+=nums[i];}
return tsum-sum;

}
};
22 changes: 22 additions & 0 deletions Week1/Monotonic Array/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
bool isMonotonic(vector<int>& 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;
}
};
12 changes: 12 additions & 0 deletions Week1/Move Zeroes/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
void moveZeroes(vector<int>& 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++;
}
}
};
15 changes: 15 additions & 0 deletions Week1/Pascal Triangle/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>>ans;
for(int i=0;i<numRows;i++){
vector<int>row(i+1,1);
for(int j=1;j<i;j++){
row[j] = ans[i-1][j] + ans[i-1][j-1];

}
ans.push_back(row);
}
return ans;
}
};
26 changes: 26 additions & 0 deletions Week1/Pivot Index/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int leftSum=0, rightSum=0, sum=0;
for(int i=0; i<nums.size(); i++){
sum += nums[i];

}
if(nums.size()!=0){
rightSum = sum - nums[0];
if(leftSum==rightSum){
return 0;
}
}
rightSum = 0;
for(int i=1; i<nums.size(); i++){
leftSum = leftSum + nums[i-1];
rightSum = sum -nums[i]-leftSum;
cout<<rightSum;
if(leftSum==rightSum){
return i;
}
}
return -1;
}
};
36 changes: 36 additions & 0 deletions Week1/Product Of Array/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int length = nums.size();

vector<int> L(length);
vector<int> R(length);


vector<int> 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;
}
};
15 changes: 15 additions & 0 deletions Week1/Remove Duplicates/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n=nums.size();
if (n==0) return 0;
int j=1;
for(int i=0;i<n-1;i++)
{ if(nums[i+1]!=nums[i])
{ nums[j]=nums[i+1];
j++;
}
}
return j;
}
};
13 changes: 13 additions & 0 deletions Week1/Reverse Only Letters/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
string reverseOnlyLetters(string S) {
int first=-1, last=S.size();
while(true) {
while(first<last and !isalpha(S[++first]));
while(first<last and !isalpha(S[--last]));
if(first==last) break;
swap(S[first],S[last]);
}
return S;
}
};
22 changes: 22 additions & 0 deletions Week1/Reverse vowels/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
string reverseVowels(string s) {
int i=0;
int j=s.size()-1;
while(i<j){
if(isVowel(s[i])==true && isVowel(s[j])==true)
swap(s[i++],s[j--]);
if(isVowel(s[i])==false)
i++;
if(isVowel(s[j])==false)
j--;
}
return s;
}
bool isVowel(char s){
s=tolower(s);
if(s=='a'||s=='e'||s=='i'||s=='o'||s=='u')
return true;
return false;
}
};
22 changes: 22 additions & 0 deletions Week1/Rotate Array/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
void reversearray(vector<int>& nums,int low,int high){
while(low<high)
{
swap(nums[low],nums[high]);
low++;
high--;
}
}



void rotate(vector<int>& nums, int k) {
int n=nums.size();
k%=n;
reversearray(nums,0,n-1);
reversearray(nums,0,k-1);
reversearray(nums,k,n-1);

}
};
34 changes: 34 additions & 0 deletions Week1/Spiral Matrix/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> curPos {0,0};
vector<int> result;
vector<vector<int>> 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<<i<<" "<<curPos[0]<<" "<<curPos[1]<<endl;
result.push_back(matrix[curPos[0]][curPos[1]]);
matrix[curPos[0]][curPos[1]] = 101;

int nextC = curPos[1] + dirs[curDir][1];
int nextR = curPos[0] + dirs[curDir][0];

if(nextC >= 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;
}
};
9 changes: 9 additions & 0 deletions Week1/Target Array/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int> ans;
for(int i=0;i<(int)nums.size();i++)
ans.insert(ans.begin()+index[i],nums[i]);
return ans;
}
};
12 changes: 12 additions & 0 deletions Week1/Valid Palindrome/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -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;
}
};
10 changes: 10 additions & 0 deletions Week1/Xor operation/Shriya_Rai
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
int xorOperation(int n, int start) {
int res = start;
for(int i = 1; i<n; i++){
res ^= (start + i*2);
}
return res;
}
};