From 9c9fe1b060f4ecdb1edb4cd70a6c42815207d8c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Khoa=20Hu=E1=BB=B3nh=20=28Daniel=29?= Date: Sat, 13 Apr 2024 23:15:19 +0700 Subject: [PATCH] Create main.go --- solution/85/main.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 solution/85/main.go diff --git a/solution/85/main.go b/solution/85/main.go new file mode 100644 index 0000000..351169e --- /dev/null +++ b/solution/85/main.go @@ -0,0 +1,32 @@ +package main + +func maximalRectangle(matrix [][]byte) int { + heights := make([]int, len(matrix[0]) + 1) + heights[len(heights)-1] = -1 + mx := 0 + for _, row := range matrix { + for i := range row { + if row[i] == '1' { + heights[i]++ + } else { + heights[i] = 0 + } + } + + stack := []int{} + for i, currentHeight := range heights { + for len(stack) > 0 && heights[stack[len(stack)-1]] > currentHeight { + prev := heights[stack[len(stack)-1]] + stack = stack[:len(stack)-1] + + width := i + if len(stack) > 0 { + width = i - stack[len(stack)-1] - 1 + } + mx = max(mx, prev * width) + } + stack = append(stack, i) + } + } + return mx +}