From 95ff34687a86f11253453d6b04af0a9a9b2a8783 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 11 Mar 2025 09:04:29 +0800 Subject: [PATCH] Add solution and test-cases for problem 2640 --- .../README.md | 45 +++++++++++++++++++ .../Solution.go | 11 ++++- .../Solution_test.go | 13 +++--- 3 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/README.md diff --git a/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/README.md b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/README.md new file mode 100644 index 000000000..7c2393a0b --- /dev/null +++ b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/README.md @@ -0,0 +1,45 @@ +# [2640.Find the Score of All Prefixes of an Array][title] + +## Description + +We define the **conversion array** `conver` of an array `arr` as follows: + +- `conver[i] = arr[i] + max(arr[0..i])` where `max(arr[0..i])` is the maximum value of `arr[j]` over `0 <= j <= i`. + +We also define the **score** of an array `arr` as the sum of the values of the conversion array of `arr`. + +Given a **0-indexed** integer array `nums` of length n, return an array `ans` of length `n` where `ans[i]` is the score of the prefix `nums[0..i]`. + +**Example 1:** + +``` +Input: nums = [2,3,7,5,10] +Output: [4,10,24,36,56] +Explanation: +For the prefix [2], the conversion array is [4] hence the score is 4 +For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10 +For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24 +For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36 +For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56 +``` + +**EXample 2:** + +``` +Input: nums = [1,1,2,4,8,16] +Output: [2,4,8,16,32,64] +Explanation: +For the prefix [1], the conversion array is [2] hence the score is 2 +For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4 +For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8 +For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16 +For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32 +For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64 +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution.go b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution.go index d115ccf5e..0d83d4a80 100755 --- a/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution.go +++ b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution.go @@ -1,5 +1,12 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) []int64 { + ans := make([]int64, len(nums)) + m := int64(nums[0]) + ans[0] = int64(nums[0]) + m + for i := 1; i < len(nums); i++ { + m = max(m, int64(nums[i])) + ans[i] = int64(nums[i]) + m + ans[i-1] + } + return ans } diff --git a/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution_test.go b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution_test.go index 14ff50eb4..0d4a4263c 100755 --- a/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution_test.go +++ b/leetcode/2601-2700/2640.Find-the-Score-of-All-Prefixes-of-an-Array/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 3, 7, 5, 10}, []int64{4, 10, 24, 36, 56}}, + {"TestCase2", []int{1, 1, 2, 4, 8, 16}, []int64{2, 4, 8, 16, 32, 64}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }