邓心一 6 years ago
parent
commit
305755c523
4 changed files with 105 additions and 2 deletions
  1. 47 2
      hard/68.go
  2. 39 0
      hard/72.go
  3. 9 0
      hard/76.go
  4. 10 0
      hard/helper.go

+ 47 - 2
hard/68.go

@@ -1,9 +1,54 @@
 package main
 
+import (
+	"strings"
+)
+
 func fullJustify(words []string, maxWidth int) (ans []string) {
+	beg, width := 0, -1
+	for i := range words {
+		if width+len(words[i]) >= maxWidth {
+			// Arrange words in [beg, i)
+			ans = append(ans, arrange(words, beg, i, width, maxWidth))
+			// Reset status
+			beg, width = i, -1
+		}
+		width += len(words[i]) + 1
+	}
+	// Arrange the last group of words,
+	// the last line must be left-justified instead of fully-justified.
+	prefix := strings.Join(words[beg:], " ")
+	trail := strings.Repeat(" ", maxWidth-len(prefix))
+	ans = append(ans, prefix+trail)
 	return
 }
 
-func main() {
-
+func arrange(words []string, beg, end, width, maxWidth int) string {
+	n := end - beg
+	space := maxWidth - (width - n + 1)
+	if n == 1 {
+		return words[beg] + strings.Repeat(" ", space)
+	}
+	gap, remain := space/(n-1), space%(n-1)
+	var sb strings.Builder
+	for i := 0; i < n; i++ {
+		sb.WriteString(words[beg+i])
+		if space > 0 {
+			sb.WriteString(strings.Repeat(" ", gap))
+			space -= gap
+			if i < remain {
+				sb.WriteByte(' ')
+				space--
+			}
+		}
+	}
+	return sb.String()
 }
+
+// func main() {
+// 	w := []string{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain",
+// 		"to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}
+// 	fmt.Println(fullJustify(w, 20))
+// 	w = []string{"What", "must", "be", "acknowledgment", "shall", "be"}
+// 	fmt.Println(fullJustify(w, 16))
+// }

+ 39 - 0
hard/72.go

@@ -0,0 +1,39 @@
+package main
+
+func minDistance(word1 string, word2 string) int {
+	// A classic dp problem. Assume that dp[i][j] is the steps
+	// that needs to be taken to change w1[0...i-1] into w2[0...j-1],
+	// then we have dp[i][0] = i (i steps to change len i str into ""),
+	// and dp[0][j] = j. If we known dp[i-1][j-1], then:
+	// 1) if w1[i-1] == w2[j-1], dp[i][j] = dp[i-1][j-1];
+	// 2) if not equal, w1[0...i-1]->w2[0...j-2]->(insert w2[j-1])
+	//    dp[i][j] = dp[i][j-1] + 1
+	// 3) or (delete w1[i-1])->w1[0...i-2]->w2[0...j-1]
+	//    dp[i][j] = dp[i-1][j] + 1
+	// 4) or (replace w1[i-1] with w2[j-1])->w1[0...i-2]+w2[j-1]->w2[0...j-2]+w2[j-1]
+	//    dp[i][j] = dp[i-1][j-1] + 1
+	m, n := len(word1), len(word2)
+	dp := make([][]int, m+1)
+	for i := range dp {
+		dp[i] = make([]int, n+1)
+		dp[i][0] = i
+	}
+	for i := 1; i <= n; i++ {
+		dp[0][i] = i
+	}
+	for i := 1; i <= m; i++ {
+		for j := 1; j <= n; j++ {
+			if word1[i-1] == word2[j-1] {
+				dp[i][j] = dp[i-1][j-1]
+			} else {
+				dp[i][j] = minInts(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
+			}
+		}
+	}
+	return dp[m][n]
+}
+
+// func main() {
+// 	println(minDistance("", ""))
+// 	println(minDistance("horse", "ros"))
+// }

+ 9 - 0
hard/76.go

@@ -0,0 +1,9 @@
+package main
+
+func minWindow(s string, t string) string {
+	return ""
+}
+
+func main() {
+
+}

+ 10 - 0
hard/helper.go

@@ -44,6 +44,16 @@ func minInt(x, y int) int {
 	return y
 }
 
+func minInts(vals ...int) (min int) {
+	min = vals[0]
+	for i := 1; i < len(vals); i++ {
+		if vals[i] < min {
+			min = vals[i]
+		}
+	}
+	return
+}
+
 func toLinkedList(num []int) *ListNode {
 	length := len(num)
 	if length == 0 {