1+ /*
2+ LeetCode Problem 3494: Minimum Total Time
3+ 🔗 https://leetcode.com/problems/minimum-total-time/
4+
5+ Problem Description:
6+ You are given two arrays:
7+ - skill[]: an array of 'n' integers where skill[i] denotes the skill value of the i-th warrior.
8+ - mana[]: an array of 'm' integers where mana[i] denotes the mana cost of the i-th spell.
9+
10+ You must apply **each mana spell exactly once**, and in order, to groups of warriors.
11+ At each stage, you apply the current spell to a **contiguous group of warriors** (starting from where the last spell left off).
12+ Each warrior must be covered by exactly one spell.
13+
14+ The cost for applying a spell at a particular stage is:
15+ total_time = sum of (skill[i] * mana[j]) for each applicable warrior
16+
17+ You want to determine the **minimum total time** required to apply all spells to all warriors in such a way that:
18+ - Each spell is applied in order.
19+ - The warriors are partitioned accordingly into m groups.
20+
21+ Return the **minimum total time** required.
22+
23+ Example:
24+ Input:
25+ skill = [1, 3, 5]
26+ mana = [2, 4]
27+ Output:
28+ 26
29+
30+ Explanation:
31+ Optimal partitioning:
32+ - Apply mana[0] = 2 to skill[0,1] => (1+3)*2 = 8
33+ - Apply mana[1] = 4 to skill[2] => 5*4 = 20
34+ Total = 8 + 20 = 28
35+ But better:
36+ - Apply mana[0] = 2 to skill[0] => 1*2 = 2
37+ - Apply mana[1] = 4 to skill[1,2] => (3+5)*4 = 32
38+ Total = 2 + 32 = 34
39+ Best:
40+ - Apply mana[0] = 2 to skill[0,1] => 8
41+ - Apply mana[1] = 4 to skill[2] => 20
42+ Total = 26 ✅
43+
44+ Approach:
45+ - Use dynamic programming (DP), where dp[i][j] represents the minimum total time required to assign the first i spells to the first j warriors.
46+ - Use space optimization by keeping only the previous and current DP rows.
47+ */
48+
49+ #include < bits/stdc++.h>
50+ using namespace std ;
51+
52+ class Solution {
53+ public:
54+ long long minTime (vector<int >& skill, vector<int >& mana) {
55+ int n = skill.size (); // Number of warriors
56+ int m = mana.size (); // Number of spells
57+
58+ // DP arrays: prev = previous row, curr = current row
59+ // dp[i][j] = minimum time to process first j warriors with first i spells
60+ vector<long long > curr (n + 1 ), prev (n + 1 );
61+
62+ // Base case: using only the first mana spell
63+ // For j warriors, time = sum of skill[0..j-1] * mana[0]
64+ prev[0 ] = 0 ;
65+ for (int i = 1 ; i <= n; i++)
66+ prev[i] = prev[i - 1 ] + (skill[i - 1 ] * 1LL * mana[0 ]);
67+
68+ // Loop over each mana spell starting from the second one
69+ for (int i = 1 ; i < m; i++) {
70+ long long x = prev[1 ]; // Best partition time starting from index 1
71+ long long cltr = 0 ; // Cumulative time cost for current partition
72+
73+ // Try to find the best partition point
74+ for (int j = 1 ; j < n; j++) {
75+ // Update best x:
76+ // x = max(previous dp - current partition cost so far)
77+ x = max (x, prev[j + 1 ] - ((skill[j - 1 ] * mana[i] * 1LL ) + cltr));
78+
79+ // Add current skill * mana to cumulative total
80+ cltr += skill[j - 1 ] * mana[i] * 1LL ;
81+ }
82+
83+ // Start filling current row of dp
84+ // curr[0] = base case: applying this spell to zero warriors (invalid)
85+ curr[0 ] = x;
86+
87+ // Build up cumulative cost applying mana[i] to skill[0..j-1]
88+ for (int j = 1 ; j <= n; j++)
89+ curr[j] = curr[j - 1 ] + (skill[j - 1 ] * mana[i] * 1LL );
90+
91+ // Move to the next stage: prev becomes curr
92+ prev = curr;
93+ }
94+
95+ // Final answer: min total time to apply all spells to all warriors
96+ return prev[n];
97+ }
98+ };
0 commit comments