From 12230854377255cdcbd429ecfed5e02b4de0eaa1 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 19 Dec 2025 21:41:40 +0800 Subject: [PATCH] Add solution and test-cases for problem 3652 --- .../README.md | 57 ++++++++++++++----- .../Solution.go | 18 +++++- .../Solution_test.go | 22 +++---- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/README.md b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/README.md index e38705154..74f2d2b11 100755 --- a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/README.md +++ b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/README.md @@ -1,28 +1,59 @@ # [3652.Best Time to Buy and Sell Stock using Strategy][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two integer arrays `prices` and `strategy`, where: + +- `prices[i]` is the price of a given stock on the `ith` day. +- `strategy[i]` represents a trading action on the `ith` day, where: + + - `-1` indicates buying one unit of the stock. + - `0` indicates holding the stock. + - `1` indicates selling one unit of the stock. + +You are also given an **even** integer `k`, and may perform **at most one** modification to `strategy`. A modification consists of: + +- Selecting exactly `k` **consecutive** elements in `strategy`. +- Set the **first** `k / 2` elements to `0` (hold). +- Set the **last** `k / 2` elements to `1` (sell). + +The **profit** is defined as the **sum** of `strategy[i] * prices[i]` across all days. + +Return the **maximum** possible profit you can achieve. + +**Note**: There are no constraints on budget or stock ownership, so all buy and sell operations are feasible regardless of past actions. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: prices = [4,2,8], strategy = [-1,0,1], k = 2 + +Output: 10 + +Explanation: -## 题意 -> ... +Modification Strategy Profit Calculation Profit +Original [-1, 0, 1] (-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8 4 +Modify [0, 1] [0, 1, 1] (0 × 4) + (1 × 2) + (1 × 8) = 0 + 2 + 8 10 +Modify [1, 2] [-1, 0, 1] (-1 × 4) + (0 × 2) + (1 × 8) = -4 + 0 + 8 4 +Thus, the maximum possible profit is 10, which is achieved by modifying the subarray [0, 1]​​​​​​​. +``` -## 题解 +**Example 2:** -### 思路1 -> ... -Best Time to Buy and Sell Stock using Strategy -```go ``` +Input: prices = [5,4,3], strategy = [1,1,0], k = 2 + +Output: 9 +Explanation: + +Modification Strategy Profit Calculation Profit +Original [1, 1, 0] (1 × 5) + (1 × 4) + (0 × 3) = 5 + 4 + 0 9 +Modify [0, 1] [0, 1, 0] (0 × 5) + (1 × 4) + (0 × 3) = 0 + 4 + 0 4 +Modify [1, 2] [1, 0, 1] (1 × 5) + (0 × 4) + (1 × 3) = 5 + 0 + 3 8 +Thus, the maximum possible profit is 9, which is achieved without any modification. +``` ## 结语 diff --git a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution.go b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution.go index d115ccf5e..52e3bf104 100644 --- a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution.go +++ b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(prices []int, strategy []int, k int) int64 { + n := len(prices) + profitSum := make([]int64, n+1) + priceSum := make([]int64, n+1) + for i := 0; i < n; i++ { + profitSum[i+1] = profitSum[i] + int64(prices[i])*int64(strategy[i]) + priceSum[i+1] = priceSum[i] + int64(prices[i]) + } + res := profitSum[n] + for i := k - 1; i < n; i++ { + leftProfit := profitSum[i-k+1] + rightProfit := profitSum[n] - profitSum[i+1] + changeProfit := priceSum[i+1] - priceSum[i-k/2+1] + res = max(res, leftProfit+changeProfit+rightProfit) + } + return res } diff --git a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution_test.go b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution_test.go index 14ff50eb4..fd0800dc0 100644 --- a/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution_test.go +++ b/leetcode/3601-3700/3652.Best-Time-to-Buy-and-Sell-Stock-using-Strategy/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + prices, strategy []int + k int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 2, 8}, []int{-1, 0, 1}, 2, 10}, + {"TestCase2", []int{5, 4, 3}, []int{1, 1, 0}, 2, 9}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.prices, c.strategy, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.prices, c.strategy, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }