2735. Collecting Chocolates LeetCode Solution

In this guide, you will get 2735. Collecting Chocolates LeetCode Solution with the best time and space complexity. The solution to Collecting Chocolates problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Collecting Chocolates solution in C++
  4. Collecting Chocolates solution in Java
  5. Collecting Chocolates solution in Python
  6. Additional Resources
2735. Collecting Chocolates LeetCode Solution image

Problem Statement of 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] <= 109
1 <= x <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2735. Collecting Chocolates LeetCode Solution in C++

class Solution {
 public:
  long long minCost(vector<int>& nums, long long x) {
    const int n = nums.size();
    long ans = LONG_MAX;
    // minCost[i] := the minimum cost to collect the i-th type
    vector<int> minCost(n, INT_MAX);

    for (int rotate = 0; rotate < n; ++rotate) {
      for (int i = 0; i < n; ++i)
        minCost[i] = min(minCost[i], nums[(i - rotate + n) % n]);
      ans =
          min(ans, accumulate(minCost.begin(), minCost.end(), 0L) + rotate * x);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2735. Collecting Chocolates LeetCode Solution in Java

class Solution {
  public long minCost(int[] nums, long x) {
    int n = nums.length;
    long ans = Long.MAX_VALUE;
    // minCost[i] := the minimum cost to collect the i-th type
    int[] minCost = new int[n];
    Arrays.fill(minCost, Integer.MAX_VALUE);

    for (int rotate = 0; rotate < n; ++rotate) {
      for (int i = 0; i < n; ++i)
        minCost[i] = Math.min(minCost[i], nums[(i - rotate + n) % n]);
      ans = Math.min(ans, Arrays.stream(minCost).asLongStream().sum() + rotate * x);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2735. Collecting Chocolates LeetCode Solution in Python

class Solution:
  def minCost(self, nums: list[int], x: int) -> int:
    n = len(nums)
    ans = math.inf
    # minCost[i] := the minimum cost to collect the i-th type
    minCost = [math.inf] * n

    for rotate in range(n):
      for i in range(n):
        minCost[i] = min(minCost[i], nums[(i - rotate + n) % n])
      ans = min(ans, sum(minCost) + rotate * x)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.