From 0312ee731ebbd4236eafcd33fbd83444643df4fb Mon Sep 17 00:00:00 2001 From: raghhavtaneja <55586663+raghhavtaneja@users.noreply.github.com> Date: Mon, 11 Oct 2021 21:54:41 +0530 Subject: [PATCH 1/3] Create RodCutting.js --- Dynamic-Programming/RodCutting.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Dynamic-Programming/RodCutting.js diff --git a/Dynamic-Programming/RodCutting.js b/Dynamic-Programming/RodCutting.js new file mode 100644 index 000000000..1f90dac0e --- /dev/null +++ b/Dynamic-Programming/RodCutting.js @@ -0,0 +1,27 @@ +/* + * You are given a rod of 'n' length and an array of prices associated with all the lengths less than 'n'. + * Find the maximum profit possible by cutting the rod and selling the pieces. +*/ + + function rodCut(prices, n){ + let memo = new Array(n + 1); + memo[0] = 0; + + for (let i = 1; i<=n; i++) + { + let max_val = Number.MIN_VALUE; + for (let j = 0; j < i; j++) + max_val = Math.max(max_val, prices[j] + memo[i - j - 1]); + memo[i] = max_val; + } + + return memo[n]; + } + + function main(){ + let arr = [1, 5, 4, 2, 1, 11, 19, 12]; + let n = arr.length; + console.log('Maximum Possible Profit is : '+ rodCut(arr,n)); + } + + main(); From 0ae960c4ee0a4374575a8f2f0b091a7989734033 Mon Sep 17 00:00:00 2001 From: raghhavtaneja Date: Tue, 12 Oct 2021 11:06:21 +0530 Subject: [PATCH 2/3] added tests for rodCutting and made some changes --- Dynamic-Programming/RodCutting.js | 10 ++------- Dynamic-Programming/tests/RodCutting.test.js | 22 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 Dynamic-Programming/tests/RodCutting.test.js diff --git a/Dynamic-Programming/RodCutting.js b/Dynamic-Programming/RodCutting.js index 1f90dac0e..6afd1e2b3 100644 --- a/Dynamic-Programming/RodCutting.js +++ b/Dynamic-Programming/RodCutting.js @@ -3,7 +3,7 @@ * Find the maximum profit possible by cutting the rod and selling the pieces. */ - function rodCut(prices, n){ + export function rodCut(prices, n){ let memo = new Array(n + 1); memo[0] = 0; @@ -18,10 +18,4 @@ return memo[n]; } - function main(){ - let arr = [1, 5, 4, 2, 1, 11, 19, 12]; - let n = arr.length; - console.log('Maximum Possible Profit is : '+ rodCut(arr,n)); - } - - main(); + diff --git a/Dynamic-Programming/tests/RodCutting.test.js b/Dynamic-Programming/tests/RodCutting.test.js new file mode 100644 index 000000000..049ac8bb8 --- /dev/null +++ b/Dynamic-Programming/tests/RodCutting.test.js @@ -0,0 +1,22 @@ +import { rodCut } from '../RodCutting' + +test('Test Case 1', () => { + expect(rodCut([1,5,8,9,10,17,17,20],8)).toBe(22); +}) + +test('Test Case 2', () => { + expect(rodCut([1,5,4,2,1,11,19,12],8)).toBe(20); +}) + +test('Test Case 3', () => { + expect(rodCut([1,2,1],3)).toBe(3); +}) + +test('Test Case 4', () => { + expect(rodCut([5,4,3,2,1],5)).toBe(25); +}) + +test('Test Case 5', () => { + expect(rodCut([3,5,8,8,10,16,14,19],8)).toBe(24); +}) + From 521027310483f548b69c3958cd099f16317105b7 Mon Sep 17 00:00:00 2001 From: raghhavtaneja Date: Tue, 12 Oct 2021 16:55:56 +0530 Subject: [PATCH 3/3] resolved style issues in the code --- Dynamic-Programming/RodCutting.js | 28 +++++++++----------- Dynamic-Programming/tests/RodCutting.test.js | 11 ++++---- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Dynamic-Programming/RodCutting.js b/Dynamic-Programming/RodCutting.js index 6afd1e2b3..1e8bac82e 100644 --- a/Dynamic-Programming/RodCutting.js +++ b/Dynamic-Programming/RodCutting.js @@ -1,21 +1,17 @@ -/* +/* * You are given a rod of 'n' length and an array of prices associated with all the lengths less than 'n'. * Find the maximum profit possible by cutting the rod and selling the pieces. */ - export function rodCut(prices, n){ - let memo = new Array(n + 1); - memo[0] = 0; - - for (let i = 1; i<=n; i++) - { - let max_val = Number.MIN_VALUE; - for (let j = 0; j < i; j++) - max_val = Math.max(max_val, prices[j] + memo[i - j - 1]); - memo[i] = max_val; - } - - return memo[n]; - } - +export function rodCut (prices, n) { + const memo = new Array(n + 1) + memo[0] = 0 + for (let i = 1; i <= n; i++) { + let maxVal = Number.MIN_VALUE + for (let j = 0; j < i; j++) { maxVal = Math.max(maxVal, prices[j] + memo[i - j - 1]) } + memo[i] = maxVal + } + + return memo[n] +} diff --git a/Dynamic-Programming/tests/RodCutting.test.js b/Dynamic-Programming/tests/RodCutting.test.js index 049ac8bb8..349ea2997 100644 --- a/Dynamic-Programming/tests/RodCutting.test.js +++ b/Dynamic-Programming/tests/RodCutting.test.js @@ -1,22 +1,21 @@ import { rodCut } from '../RodCutting' test('Test Case 1', () => { - expect(rodCut([1,5,8,9,10,17,17,20],8)).toBe(22); + expect(rodCut([1, 5, 8, 9, 10, 17, 17, 20], 8)).toBe(22) }) test('Test Case 2', () => { - expect(rodCut([1,5,4,2,1,11,19,12],8)).toBe(20); + expect(rodCut([1, 5, 4, 2, 1, 11, 19, 12], 8)).toBe(20) }) test('Test Case 3', () => { - expect(rodCut([1,2,1],3)).toBe(3); + expect(rodCut([1, 2, 1], 3)).toBe(3) }) test('Test Case 4', () => { - expect(rodCut([5,4,3,2,1],5)).toBe(25); + expect(rodCut([5, 4, 3, 2, 1], 5)).toBe(25) }) test('Test Case 5', () => { - expect(rodCut([3,5,8,8,10,16,14,19],8)).toBe(24); + expect(rodCut([3, 5, 8, 8, 10, 16, 14, 19], 8)).toBe(24) }) -