-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2735-CollectingChocolates.go
More file actions
95 lines (83 loc) · 3.82 KB
/
2735-CollectingChocolates.go
File metadata and controls
95 lines (83 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
// 2735. Collecting Chocolates
// You are given a 0-indexed integer array nums of size n representing the cost of collecting different chocolates.
// The cost of collecting the chocolate at the index i is nums[i].
// Each chocolate is of a different type, and initially, the chocolate at the index i is of ith type.
// In one operation, you can do the following with an incurred cost of x:
// Simultaneously change the chocolate of ith type to ((i + 1) mod n)th type for all chocolates.
// Return the minimum cost to collect chocolates of all types,
// given that you can perform as many operations as you would like.
// Example 1:
// Input: nums = [20,1,15], x = 5
// Output: 13
// Explanation: Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1.
// Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2nd type of chocolate at a cost of 1.
// Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1.
// Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
// Example 2:
// Input: nums = [1,2,3], x = 4
// Output: 6
// Explanation: We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
// Constraints:
// 1 <= nums.length <= 1000
// 1 <= nums[i] <= 10^9
// 1 <= x <= 10^9
import "fmt"
import "slices"
func minCost(nums []int, x int) int64 {
res, arr := 0, make([]int, len(nums))
copy(arr, nums)
for _, v := range nums {
res += v
}
for i := 1; i < len(nums); i++ {
cur := i * x
nums = append(nums, nums[0])
nums = nums[1:]
for i, v := range arr {
if nums[i] < v {
arr[i] = nums[i]
}
cur += arr[i]
}
if cur < res {
res = cur
}
}
return int64(res)
}
func minCost1(nums []int, x int) int64 {
n := len(nums)
s := make([]int64, n) // s[k] 统计操作 k 次的总成本
for i := range s {
s[i] = int64(i) * int64(x)
}
for i, mn := range nums { // 子数组左端点
for j := i; j < n + i; j++ { // 子数组右端点(把数组视作环形的)
mn = min(mn, nums[j % n]) // 维护从 nums[i] 到 nums[j] 的最小值
s[j-i] += int64(mn) // 累加操作 j-i 次的花费
}
}
return slices.Min(s)
}
func main() {
// Example 1:
// Input: nums = [20,1,15], x = 5
// Output: 13
// Explanation: Initially, the chocolate types are [0,1,2]. We will buy the 1st type of chocolate at a cost of 1.
// Now, we will perform the operation at a cost of 5, and the types of chocolates will become [1,2,0]. We will buy the 2nd type of chocolate at a cost of 1.
// Now, we will again perform the operation at a cost of 5, and the chocolate types will become [2,0,1]. We will buy the 0th type of chocolate at a cost of 1.
// Thus, the total cost will become (1 + 5 + 1 + 5 + 1) = 13. We can prove that this is optimal.
fmt.Println(minCost([]int{20,1,15}, 5)) // 13
// Example 2:
// Input: nums = [1,2,3], x = 4
// Output: 6
// Explanation: We will collect all three types of chocolates at their own price without performing any operations. Therefore, the total cost is 1 + 2 + 3 = 6.
fmt.Println(minCost([]int{1,2,3}, 4)) // 6
fmt.Println(minCost([]int{1,2,3,4,5,6,7,8,9}, 4)) // 35
fmt.Println(minCost([]int{9,8,7,6,5,4,3,2,1}, 4)) // 35
fmt.Println(minCost1([]int{20,1,15}, 5)) // 13
fmt.Println(minCost1([]int{1,2,3}, 4)) // 6
fmt.Println(minCost1([]int{1,2,3,4,5,6,7,8,9}, 4)) // 35
fmt.Println(minCost1([]int{9,8,7,6,5,4,3,2,1}, 4)) // 35
}