Skip to content

Commit a14f16f

Browse files
anshumanvaviaryan
authored andcommitted
Added RodCuttingProblem [JavaScript] (#363)
* Added rodCuttingProblem [JavaScript] * Added rodCuttingProblem [JavaScript] * bot fix v1.0 * Monal changes :O * Take that inches off ! * Use GFG implementation * Declare tmax atleast !
1 parent d29c554 commit a14f16f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
3636
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | | |
3737
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | |
3838
| [Quicksort](https://en.wikipedia.org/wiki/Quicksort) | [:white_check_mark:](quicksort/quicksort.c) | | [:white_check_mark:](quicksort/QuickSort.java) | [:white_check_mark:](quicksort/quick_sort.py) | [:white_check_mark:](quicksort/quick_sort.go) | |
39-
| [Rod Cutting Problem](http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/) | [:white_check_mark:](rod_cutting_problem/rod_cutting.c) | | [:white_check_mark:](rod_cutting_problem/RodCutting.java) | [:white_check_mark:](rod_cutting_problem/rod_cutting.py) | [:white_check_mark:](rod_cutting_problem/rod_cutting.go) | |
39+
| [Rod Cutting Problem](http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/) | [:white_check_mark:](rod_cutting_problem/rod_cutting.c) | | [:white_check_mark:](rod_cutting_problem/RodCutting.java) | [:white_check_mark:](rod_cutting_problem/rod_cutting.py) | [:white_check_mark:](rod_cutting_problem/rod_cutting.go) | [:white_check_mark:](rod_cutting_problem/rodCuttingProblem.js) |
4040
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | | [:white_check_mark:](shell_sort/ShellSort.cpp) | | [:white_check_mark:](/shell_sort/shell_sort.py) | [:white_check_mark:](shell_sort/shell_sort.go) | [:white_check_mark:](shell_sort/shellSort.js) |
4141
| [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.c) | | [:white_check_mark:](sieve_of_eratosthenes/SieveOfEratosthenes.java) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.py) | [:white_check_mark:](sieve_of_eratosthenes/sieve_of_eratosthenes.go) | [:white_check_mark:](sieve_of_eratosthenes/sieveOfEratosthenes.js) |
4242
| [Sleep Sort](http://www.geeksforgeeks.org/sleep-sort-king-laziness-sorting-sleeping/) | | [:white_check_mark:](sleep_sort/sleep_sort.cpp) | | [:white_check_mark:](sleep_sort/sleep_sort.py) | | |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Problem Statement: Given a rod of length n and an array of prices that contains prices of all pieces of size smaller than n.
3+
* Determine the maximum value obtainable by cutting up the rod and selling the pieces.
4+
*/
5+
6+
function rodCuttingProblem (price) {
7+
/*
8+
Computes maximum money that can be earned by cutting a rod of length n (Bottom-Up Approach).
9+
Time Complexity : O(n ^ 2)
10+
Space Complexity : O(n)
11+
:param price: Array in which price[i] denotes price of rod of length i.
12+
:return: returns maximum value obtainable by cutting up the rod and selling the pieces.
13+
*/
14+
let n = price.length;
15+
let bestPrice = new Array(n + 1);
16+
bestPrice.fill(0);
17+
for (let i = 1; i < bestPrice.length; i++) {
18+
bestPrice[i] = price[i - 1];
19+
}
20+
for (let i = 1; i <= n; i++) {
21+
let tmax = Number.MIN_SAFE_INTEGER;
22+
for (let j = 0; j < i; j++) {
23+
tmax = Math.max(tmax, bestPrice[i - j - 1] + price[j]);
24+
}
25+
bestPrice[i] = tmax;
26+
}
27+
return bestPrice[n];
28+
}
29+
30+
function main () {
31+
let price = [10, 52, 84, 93, 101, 17, 117, 20];
32+
console.log('Maximum obtainable value is : ' + rodCuttingProblem(price));
33+
}
34+
35+
main();

0 commit comments

Comments
 (0)