28
28
*/
29
29
30
30
/**
31
+ * Get the largest rectangle area
31
32
* @param {number[] } heights
32
33
* @return {number }
34
+ * @time complexity: O(n)
35
+ * @space complexity: O(n)
33
36
*/
34
37
const largestRectangleArea = heights => {
35
38
@@ -54,6 +57,15 @@ const largestRectangleArea = heights => {
54
57
return maxArea ;
55
58
} ;
56
59
60
+ /**
61
+ * Calcualte the area
62
+ * @param {number } i index element in heights
63
+ * @param {number[] } heights
64
+ * @param {number[] } stack
65
+ * @return {number }
66
+ * @time complexity: O(1)
67
+ * @space complexity: O(1)
68
+ */
57
69
const getArea = ( i , heights , stack ) => {
58
70
const h = heights [ stack . pop ( ) ] ,
59
71
w = stack . length ? i - stack [ stack . length - 1 ] - 1 : i ;
@@ -62,22 +74,20 @@ const getArea = (i, heights, stack) => {
62
74
} ;
63
75
64
76
65
- const assert = require ( 'chai' ) . assert ;
77
+ import { assert } from 'chai' ;
66
78
67
79
describe ( 'Largest Rectangle in Histogram' , ( ) => {
68
80
69
81
it ( 'should find the longest path' , ( ) => {
70
- const input = [ 2 , 1 , 5 , 6 , 2 , 3 ] ,
71
- output = 10 ;
72
-
73
- assert . strictEqual ( largestRectangleArea ( input ) , output ) ;
82
+ assert . strictEqual ( largestRectangleArea ( [ 2 , 1 , 5 , 6 , 2 , 3 ] ) , 10 ) ;
74
83
} ) ;
75
84
76
85
it ( 'should return 2 for [1, 1]' , ( ) => {
77
- const input = [ 1 , 1 ] ,
78
- output = 2 ;
86
+ assert . strictEqual ( largestRectangleArea ( [ 1 , 1 ] ) , 2 ) ;
87
+ } ) ;
79
88
80
- assert . strictEqual ( largestRectangleArea ( input ) , output ) ;
81
- } )
89
+ it ( 'should return 1 for [1]' , ( ) => {
90
+ assert . strictEqual ( largestRectangleArea ( [ 1 ] ) , 1 ) ;
91
+ } ) ;
82
92
83
93
} ) ;
0 commit comments