Skip to content

Commit ae76b71

Browse files
authored
Improved task 84
1 parent 8bb2a08 commit ae76b71

File tree

1 file changed

+11
-5
lines changed
  • src/main/scala/g0001_0100/s0084_largest_rectangle_in_histogram

1 file changed

+11
-5
lines changed

src/main/scala/g0001_0100/s0084_largest_rectangle_in_histogram/Solution.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package g0001_0100.s0084_largest_rectangle_in_histogram
22

33
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Stack #Monotonic_Stack
4-
// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2023_11_02_Time_904_ms_(71.43%)_Space_77.5_MB_(35.71%)
4+
// #Big_O_Time_O(n_log_n)_Space_O(log_n) #2024_06_02_Time_1092_ms_(71.43%)_Space_76.1_MB_(64.29%)
5+
6+
import scala.util.control.Breaks.{break, breakable}
57

68
object Solution {
79
def largestRectangleArea(heights: Array[Int]): Int = {
@@ -54,12 +56,16 @@ object Solution {
5456
}
5557

5658
private def checkIfSorted(a: Array[Int], start: Int, limit: Int): Boolean = {
57-
for (i <- start + 1 until limit) {
58-
if (a(i) < a(i - 1)) {
59-
return false
59+
var sorted = true
60+
breakable {
61+
for (i <- start + 1 until limit) {
62+
if (a(i) < a(i - 1)) {
63+
sorted = false
64+
break
65+
}
6066
}
6167
}
62-
true
68+
sorted
6369
}
6470

6571
private def maxOfThreeNums(a: Int, b: Int, c: Int): Int = {

0 commit comments

Comments
 (0)