dengxinyi 6 éve
szülő
commit
40d8e5f328

+ 22 - 0
medium/672.bulb-switcher-ii.go

@@ -0,0 +1,22 @@
+func flipLights(n int, m int) int {
+	// 1. Flip all;
+	// 2. Flip odd;
+	// 3. Flip even;
+	// 4. Flip 3k + 1.
+	// So, 1 + 2 -> 3, we have 8 states at most:
+	// On; 1; 2; 3; 4; 1 + 4; 2 + 4; 3 + 4.
+	// When 2 < n and 3 <= m, we can have them all.
+	switch {
+	case m == 0:
+		return 1
+	case n == 1:
+		return 2
+	case n == 2 && m == 1:
+		return 3
+	case n == 2 || m == 1:
+		return 4
+	case m == 2:
+		return 7
+	}
+	return 8
+}

+ 32 - 0
medium/673.number-of-longest-increasing-subsequence.go

@@ -0,0 +1,32 @@
+func findNumberOfLIS(nums []int) int {
+	n, max := len(nums), 1
+	if n == 0 {
+		return 0
+	}
+	dp := make([]int, n)     // The number of LIS end with nums[i]
+	length := make([]int, n) // The length of LIS ...
+	dp[0], length[0] = 1, 1
+	for i := 1; i < n; i++ {
+		dp[i], length[i] = 1, 1
+		for j := 0; j < i; j++ {
+			if nums[j] < nums[i] {
+				if l := length[j] + 1; l == length[i] {
+					dp[i] += dp[j]
+				} else if length[i] < l {
+					if max < l {
+						max = l
+					}
+					dp[i] = dp[j]
+					length[i] = l
+				}
+			}
+		}
+	}
+	res := 0
+	for i := range dp {
+		if length[i] == max {
+			res += dp[i]
+		}
+	}
+	return res
+}