Skip to content

Commit

Permalink
Merge pull request #585 from 0xff-dev/241
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 241
  • Loading branch information
6boris authored Aug 22, 2023
2 parents 234359b + 48de499 commit 7739639
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,96 @@ func ways(input string, cache map[string][]int) []int {

return ans
}

// Solution2
type operation struct {
val int
op byte
}

func calResult(a, b int, op byte) int {
if op == '*' {
return a * b
}
if op == '+' {
return a + b
}
return a - b
}

func diffWaysToCompute2(expression string) []int {
opts := make([]operation, 0)
base := 1
c := 0
// 245
for idx := len(expression) - 1; idx >= 0; idx-- {
if expression[idx] >= '0' && expression[idx] <= '9' {
c = base*int(expression[idx]-'0') + c
base *= 10
continue
}
opts = append(opts, operation{val: c}, operation{op: expression[idx]})
base, c = 1, 0
}
opts = append(opts, operation{val: c})

length := len(opts)
if length == 1 {
return []int{opts[0].val}
}
for s, e := 0, length-1; s < e; s, e = s+1, e-1 {
opts[s], opts[e] = opts[e], opts[s]
}

ans := make([]int, 0)
dp := make([][]map[int]int, length)
// 需要明确dp[i][j] 能计算出多少种结果
for i := 0; i < length; i++ {
dp[i] = make([]map[int]int, length)
for j := 0; j < length; j++ {
dp[i][j] = make(map[int]int)
}
if i&1 == 0 {
dp[i][i][opts[i].val] = 1
}
}

// 2 - 1 - 1-1
// n=2 0-2, 2-4, 4-6
// n=3,0-4, 2-6
// n=4,0-6
// n = 4 end=6
// length=7
// 2-1
// 2 有一个操作数,3有两个,4有3个
// 2 = 1+0
// 3 = 2+1
// 4 = 3 + 2
// length - 1 - n+1 -n+2

for n := 2; n <= length/2+1; n++ {
for start := 0; start < length+2-2*n; start += 2 {
end := start + 2*(n-1)
for k := start; k < end; k += 2 {
for v1, c1 := range dp[start][k] {
for v2, c2 := range dp[k+2][end] {
r := calResult(v1, v2, opts[k+1].op)
dp[start][end][r] += c1 * c2
}
}
}
}
}
for idx := length - 1; idx > 0; idx -= 2 {
op := opts[idx-1].op
for v1, c1 := range dp[idx][length-1] {
for v2, c2 := range dp[0][idx-2] {
r := calResult(v2, v1, op)
for loop := c1 * c2; loop > 0; loop-- {
ans = append(ans, r)
}
}
}
}
return ans
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Solution

import (
"sort"
"strconv"
"testing"
)
Expand All @@ -12,8 +13,9 @@ func TestSolution(t *testing.T) {
inputs string
expect []int
}{
{"TestCase", "2-1-1", []int{0, 2}},
{"TestCase", "2*3-4*5", []int{-34, -14, -10, -10, 10}},
{"TestCase1", "2-1-1", []int{0, 2}},
{"TestCase2", "2*3-4*5", []int{-34, -14, -10, -10, 10}},
{"TestCase3", "10+5", []int{15}},
}

// 开始测试
Expand All @@ -32,14 +34,29 @@ func TestSolution(t *testing.T) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
}
}

got = diffWaysToCompute2(c.inputs)
sort.Ints(got)
if len(got) != len(c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
}
m = make(map[int]int)
for v := range got {
m[v]++
}
for v := range c.expect {
if _, ok := m[v]; !ok {
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
}
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 7739639

Please sign in to comment.