邓心一 6 năm trước cách đây
mục cha
commit
32a72afe17
3 tập tin đã thay đổi với 48 bổ sung9 xóa
  1. 7 7
      hard/10.go
  2. 32 2
      hard/42.go
  3. 9 0
      hard/44.go

+ 7 - 7
hard/10.go

@@ -1,6 +1,6 @@
 package main
 
-func isMatch(s string, p string) bool {
+func isMatchRegex(s string, p string) bool {
 	// If former x chars of string matches former y chars
 	// of pattern, match[x][y] == true, else == false
 	var match [][]bool
@@ -44,10 +44,10 @@ func isMatch(s string, p string) bool {
 }
 
 // func main() {
-// 	fmt.Println(isMatch("", ""))
-// 	fmt.Println(isMatch("ssssss", "p"))
-// 	fmt.Println(isMatch("ssssss", "s*"))
-// 	fmt.Println(isMatch("ssssss", "s.*s"))
-// 	fmt.Println(isMatch("ss", "s.*ss"))
-// 	fmt.Println(isMatch("ss", "s*"))
+// 	fmt.Println(isMatchRegex("", ""))
+// 	fmt.Println(isMatchRegex("ssssss", "p"))
+// 	fmt.Println(isMatchRegex("ssssss", "s*"))
+// 	fmt.Println(isMatchRegex("ssssss", "s.*s"))
+// 	fmt.Println(isMatchRegex("ss", "s.*ss"))
+// 	fmt.Println(isMatchRegex("ss", "s*"))
 // }

+ 32 - 2
hard/42.go

@@ -1,5 +1,35 @@
 package main
 
-func main() {
-
+func trap(height []int) (units int) {
+	n := len(height)
+	if n <= 2 {
+		return 0
+	}
+	left, right := 0, n-1 // Bidirection scan is the key!!!
+	maxLeft, maxRight := height[left], height[right]
+	for left <= right {
+		if height[left] <= height[right] { // So left bar would meet right bar
+			if height[left] > maxLeft {
+				maxLeft = height[left]
+			} else {
+				units += maxLeft - height[left]
+			}
+			left++
+		} else { // Else, right bar would meet left bar
+			if height[right] > maxRight {
+				maxRight = height[right]
+			} else {
+				units += maxRight - height[right]
+			}
+			right--
+		}
+	}
+	return
 }
+
+// func main() {
+// 	h := []int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}
+// 	println(trap(h))
+// 	h = []int{}
+// 	println(trap(h))
+// }

+ 9 - 0
hard/44.go

@@ -0,0 +1,9 @@
+package main
+
+func isMatch(s string, p string) (ans bool) {
+	return
+}
+
+func main() {
+
+}