FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Dynamic-Programming/RodCutting.js at master · MMABSOUT/JavaScript · GitHub
MMABSOUT
/
JavaScript
Public
forked from
TheAlgorithms/JavaScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Dynamic-Programming
/
RodCutting.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
19 lines (16 loc) · 490 Bytes
Breadcrumbs
JavaScript
/
Dynamic-Programming
/
RodCutting.js
Copy path
File metadata and controls
19 lines (16 loc) · 490 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* 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
)
{
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
]
}
Back
|
FazBrowse Home
|
New Git URL