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

Week1 AditiMohanty #140

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions Week1/Count Negative Nos/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int ans=0;

for (const vector<int>& row: grid)
for (const int& i: row)
if (i<0) ans++;
return ans;
}
};
20 changes: 20 additions & 0 deletions Week1/Count and Say/Aditi_Mohanty.cpp
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/Aditi_Mohanty.cpp
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/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = 0, smax = INT_MIN;
for (int num : nums) {
sum += num;
smax = max(smax, sum);
if (sum < 0) {
sum = 0;
}
}
return smax;
}
};
14 changes: 14 additions & 0 deletions Week1/Missing no/Aditi_Mohanty.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());
if(nums[nums.size()-1]!=nums.size())
return nums.size();
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=i)
return i;
}
return -1;
}
};
9 changes: 9 additions & 0 deletions Week1/Monotonic Array/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public:
bool isMonotonic(vector<int> A) {
bool inc = true, dec = true;
for (int i = 1; i < A.size(); ++i)
inc &= A[i - 1] <= A[i], dec &= A[i - 1] >= A[i];
return inc || dec;
}
};
16 changes: 16 additions & 0 deletions Week1/Move Zeroes/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int lastNonZeroFoundAt = 0;

for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
nums[lastNonZeroFoundAt++] = nums[i];
}
}

for (int i = lastNonZeroFoundAt; i < nums.size(); i++) {
nums[i] = 0;
}
}
};
16 changes: 16 additions & 0 deletions Week1/Pascal Triangle/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int>> r(numRows);

for (int i=0;i<numRows;i++) {
r[i].resize(i + 1);
r[i][0] = r[i][i] = 1;

for (int j=1;j<i;j++)
r[i][j] = r[i-1][j-1]+r[i-1][j];
}

return r;
}
};
29 changes: 29 additions & 0 deletions Week1/Pivot Index/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int pivotIndex(vector<int>& nums) {
if (nums.size() == 0)
return -1;

if (nums.size() == 1)
return 0;

int sum = 0;

for (auto n : nums) {
sum += n;
}

int left = 0;
int right = sum - nums[0];
for (int i = 0; i < nums.size(); i++) {
if (i > 0) {
left += nums[i-1];
right -= nums[i];
}
if (left == right)
return i;
}

return -1;
}
};
23 changes: 23 additions & 0 deletions Week1/Product Of Array/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n=nums.size();
vector<int> fromBegin(n);
fromBegin[0]=1;
vector<int> fromLast(n);
fromLast[n-1]=1;

for(int i=1;i<n;i++)
fromBegin[i]=fromBegin[i-1]*nums[i-1];

for(int i=n-2;i>=0;i--)
fromLast[i]=fromLast[i+1]*nums[i+1];

vector<int> res(n);
for(int i=0;i<n;i++)
res[i]=fromBegin[i]*fromLast[i];

return res;
}

};
17 changes: 17 additions & 0 deletions Week1/Remove Duplicates/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==0) return 0;
int j=1;
for(int i=0;i<nums.size()-1;i++)
{
if(nums[i+1]!=nums[i])
{
nums[j]=nums[i+1];
j++;
}
}
return j;

}
};
19 changes: 19 additions & 0 deletions Week1/Reverse Only Letters/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
string reverseOnlyLetters(string S) {
int l = 0;
int r = S.size();
while (l < r) {
if (!isalpha(S[l])) {
l++;
} else if (!isalpha(S[r-1])) {
r--;
} else {
swap(S[l], S[r-1]);
l++;
r--;
}
}
return S;
}
};
14 changes: 14 additions & 0 deletions Week1/Reverse vowels/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
string reverseVowels(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
i = s.find_first_of("aeiouAEIOU", i);
j = s.find_last_of("aeiouAEIOU", j);
if (i < j) {
swap(s[i++], s[j--]);
}
}
return s;
}
};
22 changes: 22 additions & 0 deletions Week1/Rotate Array/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
void reversearr(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=k%n;
reversearr(nums,0,n-1);
reversearr(nums,0,k-1);
reversearr(nums,k,n-1);
}
};
34 changes: 34 additions & 0 deletions Week1/Spiral Matrix/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int m = matrix.size(), n = m ? matrix[0].size() : 0, u = 0, d = m - 1, l = 0, r = n - 1, p = 0;
vector<int> order(m * n);
while (u <= d && l <= r) {
for (int col = l; col <= r; col++) {
order[p++] = matrix[u][col];
}
if (++u > d) {
break;
}
for (int row = u; row <= d; row++) {
order[p++] = matrix[row][r];
}
if (--r < l) {
break;
}
for (int col = r; col >= l; col--) {
order[p++] = matrix[d][col];
}
if (--d < u) {
break;
}
for (int row = d; row >= u; row--) {
order[p++] = matrix[row][l];
}
if (l++ > r) {
break;
}
}
return order;
}
};
10 changes: 10 additions & 0 deletions Week1/Target Array/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index)
{
vector<int> result;
for(int i=0;i<index.size();++i)
result.insert(result.begin()+index[i],nums[i]);
return result;
}
};
12 changes: 12 additions & 0 deletions Week1/Valid Palindrome/Aditi_Mohanty.cpp
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--) { // Move 2 pointers from each end until they collide
while (isalnum(s[i]) == false && i < j) i++; // Increment left pointer if not alphanumeric
while (isalnum(s[j]) == false && i < j) j--; // Decrement right pointer if no alphanumeric
if (toupper(s[i]) != toupper(s[j])) return false; // Exit and return error if not match
}

return true;
}
};
12 changes: 12 additions & 0 deletions Week1/Xor operation/Aditi_Mohanty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int xorOperation(int n, int start) {
int nums[n];
int ans=0;
for(int i=0;i<n;i++){
nums[i] = start + 2*i;
ans = ans^nums[i];
}
return ans;
}
};