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

Create Count Sorted Vowel Strings.cpp #24

Open
wants to merge 3 commits into
base: master
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
52 changes: 52 additions & 0 deletions Leetcode/DP/01 Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.


*/

class Solution {
public:


vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int r = matrix.size();
if (r == 0)
return matrix;
int c = matrix[0].size();
vector<vector<int> > dp(r, vector<int>(c, INT_MAX - 100000));

for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (matrix[i][j] == 0)
dp[i][j] = 0;
else {
if (i > 0)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
if (j > 0)
dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
}
}
}

for (int i = r - 1; i >= 0; i--) {
for (int j = c - 1; j >= 0; j--) {
if (i < r - 1)
dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
if (j < c - 1)
dp[i][j] = min(dp[i][j], dp[i][j + 1] + 1);
}
}

return dp;
}
};


/*
0 0 0
0 1 0
1 1 1

*/
26 changes: 26 additions & 0 deletions Leetcode/DP/Best Time to Buy and Sell Stock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.


*/

class Solution {
public:
int maxProfit(vector<int>& prices) {
int profit = 0;
int nn = prices.size();
vector<int>dp(nn , 0);
dp[0] = 0;
for(int i = 1 ; i < prices.size() ; i++)
{
dp[i] = max(prices[i] - prices[i - 1] + dp[i - 1] , dp[i]);
}

return *max_element(dp.begin() , dp.end());
}
};
32 changes: 32 additions & 0 deletions Leetcode/DP/Count Sorted Vowel Strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Given an integer n, return the number of strings of length n that consist only of vowels (a, e, i, o, u) and are lexicographically sorted.

A string s is lexicographically sorted if for all valid i, s[i] is the same as or comes before s[i+1] in the alphabet.

*/

class Solution {
public:

int countVowelStrings(int n) {

int table[52][52] = { 0 };

table[0][0] = 1;
for (int i = 1; i < 5; i++)
table[0][i] = table[0][i - 1] + 1;

for (int i = 0; i < n; i++)
table[i][0] = 1;



for (int i = 1; i < n; i++)
for (int j = 1; j < 5; j++)
table[i][j] = table[i - 1][j] + table[i][j - 1];

return table[n - 1][4];


}
};