Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5210273

Browse files
resolved style issues in the code
1 parent 0ae960c commit 5210273

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

‎Dynamic-Programming/RodCutting.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
/*
1+
/*
22
* You are given a rod of 'n' length and an array of prices associated with all the lengths less than 'n'.
33
* Find the maximum profit possible by cutting the rod and selling the pieces.
44
*/
55

6-
export function rodCut(prices, n){
7-
let memo = new Array(n + 1);
8-
memo[0] = 0;
9-
10-
for (let i = 1; i<=n; i++)
11-
{
12-
let max_val = Number.MIN_VALUE;
13-
for (let j = 0; j < i; j++)
14-
max_val = Math.max(max_val, prices[j] + memo[i - j - 1]);
15-
memo[i] = max_val;
16-
}
17-
18-
return memo[n];
19-
}
20-
6+
export function rodCut (prices, n) {
7+
const memo = new Array(n + 1)
8+
memo[0] = 0
219

10+
for (let i = 1; i <= n; i++) {
11+
let maxVal = Number.MIN_VALUE
12+
for (let j = 0; j < i; j++) { maxVal = Math.max(maxVal, prices[j] + memo[i - j - 1]) }
13+
memo[i] = maxVal
14+
}
15+
16+
return memo[n]
17+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { rodCut } from '../RodCutting'
22

33
test('Test Case 1', () => {
4-
expect(rodCut([1,5,8,9,10,17,17,20],8)).toBe(22);
4+
expect(rodCut([1,5,8,9,10,17,17,20],8)).toBe(22)
55
})
66

77
test('Test Case 2', () => {
8-
expect(rodCut([1,5,4,2,1,11,19,12],8)).toBe(20);
8+
expect(rodCut([1,5,4,2,1,11,19,12],8)).toBe(20)
99
})
1010

1111
test('Test Case 3', () => {
12-
expect(rodCut([1,2,1],3)).toBe(3);
12+
expect(rodCut([1,2,1],3)).toBe(3)
1313
})
1414

1515
test('Test Case 4', () => {
16-
expect(rodCut([5,4,3,2,1],5)).toBe(25);
16+
expect(rodCut([5,4,3,2,1],5)).toBe(25)
1717
})
1818

1919
test('Test Case 5', () => {
20-
expect(rodCut([3,5,8,8,10,16,14,19],8)).toBe(24);
20+
expect(rodCut([3,5,8,8,10,16,14,19],8)).toBe(24)
2121
})
22-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /