The Stock Span Problem | Fills array S [ ] with span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; print the calculated span values ; Driver Code ; Fill the span values in array S [ ]
< ? php function calculateSpan ( $ price , $ n , $ S ) { $ S [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ S [ $ i ] = 1 ; for ( $ j = $ i - 1 ; ( $ j >= 0 ) && ( $ price [ $ i ] >= $ price [ $ j ] ) ; $ j -- ) $ S [ $ i ] ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ S [ $ i ] . " ▁ " ; ; } $ price = array ( 10 , 4 , 5 , 90 , 120 , 80 ) ; $ n = count ( $ price ) ; $ S = array ( $ n ) ; calculateSpan ( $ price , $ n , $ S ) ; ? >