Skip to content

Commit 1001036

Browse files
committed
add: previously solved DP questions
1 parent 58fd6fe commit 1001036

6 files changed

+86
-10
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int countSquares(vector<vector<int>>& matrix) {
8+
int col = matrix[0].size(), row = matrix.size();
9+
int result = 0;
10+
for(int i = 0; i < row; i++) {
11+
for(int j = 0; j < col; j++) {
12+
if(matrix[i][j] == 1) {
13+
if(i == 0 || j == 0) {
14+
result += 1;
15+
}
16+
else {
17+
int res = min(
18+
{ matrix[i - 1][j - 1], matrix[i][j - 1], matrix[i - 1][j] }
19+
) + matrix[i][j];
20+
result += res;
21+
matrix[i][j] = res;
22+
}
23+
}
24+
}
25+
}
26+
return result;
27+
}
28+
};
29+
30+
int main () {
31+
Solution s;
32+
vector<vector<int>> matrix = {{1,0,1,0,0},
33+
{1,0,1,1,1},
34+
{1,1,1,1,1},
35+
{1,0,0,1,0}};
36+
cout << s.countSquares(matrix) << endl;
37+
return 0;
38+
}

Diff for: DynamicProgramming/CountSortedVowelStrings.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function countVowelStrings(n) {
2+
const chars = ["a", "e", "i", "o", "u"];
3+
let res = [];
4+
function dfs(word) {
5+
if (word.length === n) {
6+
res.push(word);
7+
return;
8+
}
9+
10+
for (let i = 0; i < chars.length; i++) {
11+
if (chars[i] >= word.charAt(word.length - 1)) {
12+
dfs(word + chars[i]);
13+
}
14+
}
15+
}
16+
dfs("");
17+
return res.length;
18+
}

Diff for: DynamicProgramming/MinCostClimbingStairs.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minCostClimbingStairs(costs) {
2+
let first = costs[0];
3+
let second = costs[1];
4+
let i = 0;
5+
if (costs.length <= 2) return Math.min(first, second);
6+
else i = 2;
7+
for (; i < costs.length; i++) {
8+
let current = costs[i] + Math.min(first, second);
9+
first = second;
10+
second = current;
11+
}
12+
return Math.min(first, second);
13+
}

Diff for: DynamicProgramming/PascalsTriangle.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
const generate = function (numRows) {
6+
let pascalTriangle = [];
7+
for (let i = 0; i < numRows; i++) {
8+
const col = new Array(i + 1).fill(1);
9+
for (let j = 1; j < i; j++) {
10+
col[j] = pascalTriangle[i - 1][j - 1] + pascalTriangle[i - 1][j];
11+
}
12+
pascalTriangle.push(col);
13+
}
14+
return pascalTriangle;
15+
};

Diff for: array/BestTimeToBuyandSell.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@ class Solution {
1414
}
1515
return maxSoFar;
1616
}
17-
18-
int maxProfitSecondApproach(vector<int>& prices) {
19-
int max = 0;
20-
for(int i = 1; i < prices.size(); i++) {
21-
if(prices[i] > prices[i - 1]) max += prices[i] - prices[i - 1];
22-
}
23-
return max;
24-
}
2517
};
2618

2719
int main() {
2820
Solution sol;
2921
vector<int> test1 = {7,1,5,3,6,4};
3022
vector<int> test2 = {7,6,4,3,1};
31-
cout << sol.maxProfitSecondApproach(test1) << endl;
32-
cout << sol.maxProfitSecondApproach(test2) << endl;
23+
cout << sol.maxProfit(test1) << endl;
24+
cout << sol.maxProfit(test2) << endl;
3325
return 0;
3426
}

Diff for: readme.md

2.61 KB

Problem solving practice

Topic Problem Solution
Array Best Time to Buy and Sell Stock BestTimeToBuyandSell.cpp
Array Reverse a string ReverseAString.cpp
Array Move Negative Numbers MoveNegative.cpp
Array Cyclically rotate an array by one RotateByOne.cpp
Array Find the Duplicate Number DuplicateNumber.cpp
Array Merge Intervals MergeIntervals.cpp
Array Next Permutation NextPermutation.cpp
Array Count Pair Of Given Sum PairsOfGivenSum.cpp
Array Merge Sorted Array MergeSortedArray.cpp
Array Longest consecutive subsequence LongestConsecutiveSubsequence.cpp
DynamicProgramming Longest Palindromic Subsequence LongestPalindrome.cpp
Topic Problem Solution
Array Best Time to Buy and Sell Stock BestTimeToBuyandSell.cpp
Array Reverse a string ReverseAString.cpp
Array Move Negative Numbers MoveNegative.cpp
Array Cyclically rotate an array by one RotateByOne.cpp
Array Find the Duplicate Number DuplicateNumber.cpp
Array Merge Intervals MergeIntervals.cpp
Array Next Permutation NextPermutation.cpp
Array Count Pair Of Given Sum PairsOfGivenSum.cpp
Array Merge Sorted Array MergeSortedArray.cpp
Array Longest consecutive subsequence LongestConsecutiveSubsequence.cpp
DynamicProgramming Longest Palindromic Subsequence LongestPalindrome.cpp
DynamicProgramming Pascal's Triangle PascalsTriangle.js
DynamicProgramming Min Cost Climbing Stairs MinCostClimbingStairs.js
DynamicProgramming Count Sorted Vowel Strings CountSortedVowelStrings.js
DynamicProgramming Coun Square Submatrices with All Ones CounSquareSubmatricesWithAllOnes.cpp

0 commit comments

Comments
 (0)