File tree Expand file tree Collapse file tree 4 files changed +28
-0
lines changed
javascript/dynamic_programming
python/dynamic_programming
sections/DynamicProgramming Expand file tree Collapse file tree 4 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ const kadane = ( arr ) => {
2+ let currSub = arr [ 0 ]
3+ let maxSub = arr [ 0 ]
4+
5+ for ( let i = 1 ; i < arr . length ; i ++ ) {
6+ currSub = Math . max ( currSub + arr [ i ] , arr [ i ] )
7+ maxSub = Math . max ( maxSub , currSub )
8+ }
9+
10+ return maxSub
11+ }
Original file line number Diff line number Diff line change 1+ def kadane (arr : list [int ]) -> int :
2+ curr_sub = max_sub = arr [0 ]
3+
4+ for num in arr [1 :]:
5+ curr_sub = max (curr_sub + num , num )
6+ max_sub = max (max_sub , curr_sub )
7+
8+ return max_sub
Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ export default function SidebarLinks() {
5858 < Accordion title = "Dynamic Programming" >
5959 < LinkWithTooltip href = "#dp-top-down" description = "top-down DP" />
6060 < LinkWithTooltip href = "#dp-bottom-up" description = "bottom-up DP" />
61+ < LinkWithTooltip href = "#dp-kadane" description = "Kadane (max-sum subarray)" />
6162 </ Accordion >
6263 < Accordion title = "Bit Manipulation" >
6364 < LinkWithTooltip href = "#bitmanipulation-test-kth-bit" description = "test kth bit" />
Original file line number Diff line number Diff line change @@ -4,9 +4,11 @@ import Tabs from '@components/Tabs'
44
55import TopDownPY from '@code/python/dynamic_programming/top_down.py?raw'
66import BottomUpPY from '@code/python/dynamic_programming/bottom_up.py?raw'
7+ import KadanePY from '@code/python/dynamic_programming/kadane.py?raw'
78
89import TopDownJS from '@code/javascript/dynamic_programming/top_down.js?raw'
910import BottomUpJS from '@code/javascript/dynamic_programming/bottom_up.js?raw'
11+ import KadaneJS from '@code/javascript/dynamic_programming/kadane.js?raw'
1012
1113
1214export default function DynamicProgramming ( ) {
@@ -25,6 +27,12 @@ export default function DynamicProgramming() {
2527 < Tabs . Tab code = { BottomUpJS } language = "javascript" />
2628 </ Tabs >
2729 </ section >
30+ < section id = "dp-kadane" >
31+ < Tabs title = "Kadane (max-sum subarray)" >
32+ < Tabs . Tab code = { KadanePY } language = "python" />
33+ < Tabs . Tab code = { KadaneJS } language = "javascript" />
34+ </ Tabs >
35+ </ section >
2836 </ div >
2937 )
3038}
You can’t perform that action at this time.
0 commit comments