mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
resolved style issues in the code
This commit is contained in:
@ -3,19 +3,15 @@
|
|||||||
* Find the maximum profit possible by cutting the rod and selling the pieces.
|
* Find the maximum profit possible by cutting the rod and selling the pieces.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function rodCut(prices, n){
|
export function rodCut (prices, n) {
|
||||||
let memo = new Array(n + 1);
|
const memo = new Array(n + 1)
|
||||||
memo[0] = 0;
|
memo[0] = 0
|
||||||
|
|
||||||
for (let i = 1; i<=n; i++)
|
for (let i = 1; i <= n; i++) {
|
||||||
{
|
let maxVal = Number.MIN_VALUE
|
||||||
let max_val = Number.MIN_VALUE;
|
for (let j = 0; j < i; j++) { maxVal = Math.max(maxVal, prices[j] + memo[i - j - 1]) }
|
||||||
for (let j = 0; j < i; j++)
|
memo[i] = maxVal
|
||||||
max_val = Math.max(max_val, prices[j] + memo[i - j - 1]);
|
|
||||||
memo[i] = max_val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return memo[n];
|
return memo[n]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,22 +1,21 @@
|
|||||||
import { rodCut } from '../RodCutting'
|
import { rodCut } from '../RodCutting'
|
||||||
|
|
||||||
test('Test Case 1', () => {
|
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', () => {
|
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', () => {
|
test('Test Case 3', () => {
|
||||||
expect(rodCut([1,2,1],3)).toBe(3);
|
expect(rodCut([1, 2, 1], 3)).toBe(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Test Case 4', () => {
|
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', () => {
|
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)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user