Skip to content

Commit 7a7ea25

Browse files
committed
fib, gridT, canSum - simple tabulation
1 parent 29b5cff commit 7a7ea25

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed

Diff for: Dynamic Programming/C++/canSum_tab.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// given a target an an array of non-negative integers, find out if the target can be constructed using the number of the array.
2+
// the numbers can be used repeatedly.
3+
// m - target and n = arr.size()
4+
// Time = O(nm)
5+
// Space = O(m)
6+
7+
#include<iostream>
8+
#include<vector>
9+
using namespace std;
10+
11+
bool canSum_tab(int target, vector<int> arr) {
12+
vector<bool> table (target+1, false);
13+
14+
// it is possible to generate 0 from arr (without picking any element)
15+
table[0] = true;
16+
17+
// single pass of the table
18+
for(int i=0; i<table.size(); i++) {
19+
if(table[i] == true) {
20+
// for a given true element, change curr_pos + arr[j] to true as well
21+
for(int j=0; j<arr.size(); j++) {
22+
// check for out of bounds
23+
if(i + arr[j] < table.size()) {
24+
table[i + arr[j]] = true;
25+
}
26+
}
27+
}
28+
}
29+
30+
return table[target];
31+
}
32+
33+
int main() {
34+
int n, iter = 1, temp, target;
35+
cout<<"Enter number of elements in array: ";
36+
cin>>n;
37+
38+
vector<int> arr;
39+
while(iter <= n) {
40+
cout<<"Enter element "<<iter<<": ";
41+
cin>>temp;
42+
arr.push_back(temp);
43+
++iter;
44+
}
45+
46+
cout<<"Enter target: ";
47+
cin>>target;
48+
49+
if(canSum_tab(target, arr))
50+
cout<<target<<" can be generated from array"<<endl;
51+
else
52+
cout<<target<<" cannot be generated from array"<<endl;
53+
54+
return 0;
55+
}

Diff for: Dynamic Programming/C++/fibonacci_tab.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given an integer n, find the nth fibonacci number using tabulation method.
2+
// Time complexity: O(n)
3+
// Space complexity: O(n)
4+
5+
#include<iostream>
6+
#include<vector>
7+
using namespace std;
8+
9+
int64_t fib_tab(int n){
10+
vector<int64_t> table (n+1, 0);
11+
table[0] = 0;
12+
table[1] = 1;
13+
14+
for(int i=2; i<=n; i++){
15+
table[i] = table[i-1] + table[i-2];
16+
}
17+
return table[n];
18+
}
19+
20+
int main() {
21+
int n;
22+
cout<<"Enter n: ";
23+
cin>>n;
24+
25+
cout<<"The "<<n<<"th fibonacci number is: "<<fib_tab(n);
26+
27+
return 0;
28+
}

Diff for: Dynamic Programming/C++/gridTraveller_tab.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Given a mxn grid, how many ways can I go from the start (0,0) to the end (m,n) by just moving RIGHT or DOWN
2+
// Time complexity = O(m*n)
3+
// Space complexity = O(m*n)
4+
5+
#include<iostream>
6+
#include<vector>
7+
#include<map>
8+
using namespace std;
9+
10+
int64_t gridTraveller_tab(int rows, int cols){
11+
vector<vector<int64_t>> table (rows+1, vector<int64_t> (cols + 1, 0));
12+
13+
// 0th row and 0th col will be populated with zeros
14+
// table[1][1] will be 1 as there is 1 way to reach destination on a 1x1 grid
15+
table[1][1] = 1;
16+
17+
for(int i=0; i<=rows; i++){
18+
for(int j=0; j<=cols; j++){
19+
if(j+1 <= cols) table[i][j+1] += table[i][j];
20+
if(i+1 <= rows) table[i+1][j] += table[i][j];
21+
}
22+
}
23+
return table[rows][cols];
24+
}
25+
26+
int main() {
27+
28+
int m, n;
29+
cout<<"Enter number of rows: ";
30+
cin>>m;
31+
cout<<"Enter number of cols: ";
32+
cin>>n;
33+
34+
cout<<"Number of ways to travel from (1,1) to ("<<m<<","<<n<<") is: "<<gridTraveller_tab(m,n)<<endl;
35+
36+
return 0;
37+
}

Diff for: Dynamic Programming/Python/canSum_tab.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# given a target an an array of non-negative integers, find out if the target can be constructed using the number of the array.
2+
# the numbers can be used repeatedly.
3+
4+
def canSum_tab(target, arr):
5+
table = [False for _ in range(target+1)]
6+
table[0] = True
7+
8+
for i in range(0, target+1):
9+
if table[i]:
10+
for shift in arr:
11+
if i + shift < len(table):
12+
table[i+shift] = True
13+
14+
return table[target]
15+
16+
if __name__ == "__main__":
17+
n = int(input("Enter number of elements: "))
18+
i = 0
19+
arr = []
20+
while(i<n):
21+
t = int(input(f"Enter element {i}: "))
22+
arr.append(t)
23+
i += 1
24+
25+
target = int(input("Enter target: "))
26+
27+
if(canSum_tab(target, arr)):
28+
print(f"{target} can be generated from array")
29+
else:
30+
print(f"{target} cannot be generated from array")

Diff for: Dynamic Programming/Python/fibonacci_tab.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Given an integer n, find the nth fibonacci number using tabulation method
2+
# Time complexity: O(n)
3+
# Space complexity: O(n)
4+
5+
def fib_tab(n):
6+
7+
table = [0] * (n+1)
8+
9+
table[1] = 1
10+
11+
for i in range(2, n+1):
12+
table[i] = table[i-1] + table[i-2]
13+
14+
return table[n]
15+
16+
if __name__ == "__main__":
17+
n = int(input("Enter n: "))
18+
19+
print(f"{n}th fibonacci number is: {fib_tab(n)}")

Diff for: Dynamic Programming/Python/gridTraveller_tab.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Given a mxn grid, how many ways can I go from the start (0,0) to the end (m,n) by just moving RIGHT or DOWN
2+
# Time complexity = O(m*n)
3+
# Space complexity = O(m*n)
4+
5+
def gridTraveller_tab(rows, cols):
6+
table = [[0] * (cols+1) for i in range(rows+1)]
7+
8+
table[1][1] = 1
9+
10+
for i in range(rows+1):
11+
for j in range(cols+1):
12+
if(j+1 <= cols):
13+
table[i][j+1] += table[i][j]
14+
if(i+1 <= rows):
15+
table[i+1][j] += table[i][j]
16+
17+
return table[rows][cols]
18+
19+
if __name__ == "__main__":
20+
m = int(input("Enter number of rows: "))
21+
n = int(input("Enter number of cols: "))
22+
23+
print(f"Number of ways to travel from (1,1) to ({m},{n}): {gridTraveller_tab(m,n)}")

0 commit comments

Comments
 (0)