Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use golang to find the minimum score for merging n-pile stones into a pile

Shulou Source: shulou.com Published: 2022-06-03 06:11:45 09月23日 Update

This article focuses on "how to use golang to find the minimum score for merging n stones into a pile". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use golang to find the minimum score for merging n stones into a pile.

The code is written in golang. The code is as follows:

Package mainimport ("fmt"math") func main () {arr: = [] int {1,4,2,3} ret: = StoneMerge (arr) fmt.Println (ret)} func sum (arr [] int) [] int {N: = len (arr) s: = make ([] int, Noni1) s [0] = for I: = 0; I

< N; i++ { s[i+1] = s[i] + arr[i] } return s}func w(s []int, l int, r int) int { return s[r+1] - s[l]}func StoneMerge(arr []int) int { if len(arr) < 2 { return 0 } N := len(arr) s := sum(arr) dp := make([][]int, N) for i := 0; i < N; i++ { dp[i] = make([]int, N) } best := make([][]int, N) for i := 0; i < N; i++ { best[i] = make([]int, N) } for i := 0; i < N-1; i++ { best[i][i+1] = i dp[i][i+1] = w(s, i, i+1) } for L := N - 3; L >

= 0; for-{for R: = L + 2; R < N; R + {next: = math.MaxInt64 choose: =-1 for leftEnd: = best [L] [R Mel 1]; leftEnd

Tags: Minimum score pebble juxtaposition finding code content learning practical deeper interest practical practical easy to operate method more friends results websites channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Huawei Microsoft OPPO Reno