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_AnkitaPrasad #147

Open
wants to merge 7 commits 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
14 changes: 14 additions & 0 deletions Week1/Count Negative Nos/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int count=0;
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid[i].size(); j++){
if(grid[i][j]<0){
count++;
}
}
}
return count;
}
};
15 changes: 15 additions & 0 deletions Week1/Count and Say/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
string countAndSay(int n) {
if(n==1) {
return "1";
}
auto s = countAndSay(n-1) + ' ';
string out;
for(auto i{0}, j{0}; i<size(s); ++i)
if(s[i] != s[j]) {
out= out+ to_string(i-j)+s[i-1], j=i;
}
return out;
}
};
17 changes: 17 additions & 0 deletions Week1/Longest Substring wo repeating chars/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int i=0,j=0,ans=0;
set<char> a;
while(i<s.size()&&j<s.size()){
if(a.find(s[j])==a.end()){
a.insert(s[j++]);
ans=max(ans,j-i);
}
else{
a.erase(s[i++]);
}
}
return ans;
}
};
12 changes: 12 additions & 0 deletions Week1/Max Subarray/week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum = nums[0];
int max_sum = nums[0];
for (int i=1; i<nums.size(); i++){
sum = max(nums[i], sum + nums[i]);
max_sum = max(max_sum, sum);
}
return max_sum;
}
};
23 changes: 23 additions & 0 deletions Week1/Missing no/Week1_AnkitaPrasad.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int missingNumber(vector<int>& nums) {


if(nums.size()==0)
{
return 0;
}

sort(nums.begin(),nums.end());

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

};
21 changes: 21 additions & 0 deletions Week1/Monotonic Array/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool isMonotonic(vector<int>& A) {
int count1=1,count2=1;

for(int i=0;i<A.size()-1;i++)
{
if(A[i]<=A[i+1])
{
count1++;
}
if(A[i]>=A[i+1])
{
count2++;
}
}

return count1==A.size() || count2==A.size();

}
};
14 changes: 14 additions & 0 deletions Week1/Move Zeroes/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int j=0;
for(int i=0; i<nums.size(); i++){
if(nums[i]!=0){
nums[j++]= nums[i];
}
}
for(int i=j; i<nums.size(); i++){
nums[i]=0;
}
}
};
18 changes: 18 additions & 0 deletions Week1/Pascal Triangle/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> number;
if (!numRows)
return number;

number.push_back({1});

for (int i = 1; i < numRows; i++) {
vector<int> t(i+1,1);
for (int j = 1; j < i; j++)
t[j] = number[i-1][j-1] + number[i-1][j];
number.push_back(t);
}
return number;
}
};
17 changes: 17 additions & 0 deletions Week1/Pivot Index/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int tsum=0, l_sum=0;
for(int i=0; i<nums.size();i++){
tsum=tsum+nums[i]; //getting the whole sum of array
}
for(int i=0; i<nums.size(); i++){
if(i!=0){
l_sum=l_sum+nums[i-1];
}
if(tsum-l_sum-nums[i]==l_sum)
return i;
}
return -1;
}
};
23 changes: 23 additions & 0 deletions Week1/Product Of Array/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> ans;
if(nums.size()<1){
return ans;
}

int product=1;
for(int i=0; i<nums.size(); i++){
product = product *nums[i];
ans.push_back(product);
}

product=1;
for(int i=nums.size()-1; i>0; --i){
ans[i]= ans[i-1]*product;
product=product*nums[i];
}
ans[0]= product;
return ans;
}
};
6 changes: 6 additions & 0 deletions Week1/Remove Duplicates/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return unique(nums.begin(), nums.end()) - nums.begin();
}
};
18 changes: 18 additions & 0 deletions Week1/Reverse Only Letters/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
string reverseOnlyLetters(string s) {
vector<char> result;
map<int,char> m;
for(int i=0;i<s.size();i++){
if(!isalpha(s[i])) m[i]=s[i];
else result.push_back(s[i]);
}
reverse(result.begin(),result.end());
for(auto i:m){
result.insert(result.begin() +i.first, i.second);
}
string str(result.begin(), result.end());
return str;

}
};
29 changes: 29 additions & 0 deletions Week1/Reverse vowels/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
inline bool isVowel(char ch)
{
return (
ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}

string reverseVowels(string s) {
int i = 0;
int j = s.size()-1;
while(i<j){
if(isVowel(s[i])){
if(isVowel(s[j])){
swap(s[i], s[j]);
i++;
j--;
}else{
j--;
}
}else{
i++;
}
}

return s;
}
};
9 changes: 9 additions & 0 deletions Week1/Rotate Array/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k=k%nums.size();
reverse(nums.end()-k, nums.end());
reverse(nums.begin(), nums.end()-k);
reverse(nums.begin(), nums.end());
}
};
43 changes: 43 additions & 0 deletions Week1/Spiral Matrix/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int right =matrix[0].size()-1;
int bottom = matrix.size()-1;
int left = 0;
int top=0;
int direction = 0;
vector <int> ans;
while(top<= bottom && left <= right){
if (direction==0){
for (int i =left; i<=right;i++){
ans.push_back(matrix[top][i]);
}
top +=1;
direction +=1;
}
else if (direction ==1){
for(int i=top; i<=bottom; i++){
ans.push_back(matrix[i][right]) ;
}
direction +=1;
right -=1;
}
else if (direction ==2){
for(int i =right; i>=left; i--){
ans.push_back(matrix[bottom][i]);
}
direction +=1;
bottom -=1;
}
else if (direction ==3){
for(int i=bottom; i>=top; i--){
ans.push_back(matrix[i][left]);
}
direction =0;
left +=1;
}
}

return ans;
}
};
12 changes: 12 additions & 0 deletions Week1/Target Array/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int>ans;
for(int i=0; i<nums.size(); i++){
ans.insert(ans.begin()+index[i], nums[i]);
}

return ans;

}
};
21 changes: 21 additions & 0 deletions Week1/Valid Palindrome/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool isPalindrome(string s) {
int i=0,j=s.size()-1;
while(i<j){
if(!isalnum(s[i])){
i++;
}
if(!isalnum(s[j])){
j--;
}
if(isalnum(s[i])&&isalnum(s[j])){
if(tolower(s[i])!=tolower(s[j]))
return false;
i++;
j--;
}
}
return true;
}
};
14 changes: 14 additions & 0 deletions Week1/Week1/Count Negative Nos/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int count=0;
for(int i=0; i<grid.size(); i++){
for(int j=0; j<grid[i].size(); j++){
if(grid[i][j]<0){
count++;
}
}
}
return count;
}
};
1 change: 1 addition & 0 deletions Week1/Week1/Count Negative Nos/week1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enter Week1 codes
15 changes: 15 additions & 0 deletions Week1/Week1/Count and Say/Week1_AnkitaPrasad.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
string countAndSay(int n) {
if(n==1) {
return "1";
}
auto s = countAndSay(n-1) + ' ';
string out;
for(auto i{0}, j{0}; i<size(s); ++i)
if(s[i] != s[j]) {
out= out+ to_string(i-j)+s[i-1], j=i;
}
return out;
}
};
1 change: 1 addition & 0 deletions Week1/Week1/Count and Say/week1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enter Week1 codes
Loading