id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<double,int>>pq;\n int n=health.size();\n long long sum=0;\n for(int i=0;i<n;i++){\n int time=(health[i]/power)+(health[i]%power==0?0:1);\n cout<<time<<\" \";\n double take=(double)damage[i]/(time);\n pq.push({take,i});\n sum+=damage[i];\n }\n long long ans=0;\n while(!pq.empty()){\n auto t=pq.top();\n pq.pop();\n int time=(health[t.second]/power)+(health[t.second]%power==0?0:1);\n ans+=(time*sum);\n sum-=damage[t.second];\n }\n return ans;\n }\n};", "memory": "156795" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool static cmp(pair<double,int>& p, pair<double,int>& p1){\n return p.first > p1.first;\n }\n\n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n vector<pair<double,int>> vp;\n int n=d.size();\n for(int i=0;i<n;i++){\n int t = ceil(double(h[i])/p);\n double D = double(d[i])/t;\n vp.push_back({D,i});\n }\n sort(vp.begin(),vp.end(),cmp);\n long long ans=0,t=0;\n for(auto P:vp){\n // cout<<P.first<<\" \";\n t += ceil((1.0*h[P.second])/p);\n ans += (1LL*t*d[P.second]);\n }//cout<<endl;\n return ans;\n }\n};", "memory": "156795" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int,int>> vp;\n long long sum=0;\n int n=damage.size();\n for(int i=0;i<n;i++){\n int time=(health[i]/power);\n if(health[i]%power){\n time++;\n }\n vp.push_back({damage[i],time});\n sum+=damage[i];\n }\n vector<pair<float,int>> vf;\n for(int i=0;i<n;i++){\n float f=((vp[i].first*1.0)/vp[i].second);\n vf.push_back({f,i});\n }\n sort(vf.rbegin(),vf.rend());\n\n long long ans=0;\n for(int i=0;i<n;i++){\n int ind=vf[i].second;\n int time=(health[ind]/power);\n if(health[ind]%power){\n time++;\n }\n ans+=(sum*time);\n // cout<<ans<<endl;\n sum-=damage[ind];\n }\n return ans;\n }\n};", "memory": "157484" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<double,int>>en;\n int n=damage.size();\n for(int i=0;i<n;i++){\n health[i]=(health[i]+power-1)/power;\n double ratio=((1.00)*damage[i]/(1.00*health[i])); \n en.push_back({ratio,i});\n }\n for(auto it:en){\n cout<<it.first<<\" \";\n }\n long long tot=0;\n long long sum=accumulate(damage.begin(),damage.end(),0);\n sort(en.rbegin(),en.rend());\n for (const auto& it : en) {\n int ix=it.second;\n tot+=health[ix]*sum;\n sum-=damage[ix];\n }\n \n return tot;\n }\n};", "memory": "157484" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n static bool comparator(tuple<int, double, int> a, tuple<int, double, int>b) {\n if(get<1>(a) == get<1>(b)) return get<2>(a) < get<2>(b);\n\n return get<1>(a) > get<1>(b);\n }\n\n long long find_total(vector<int>& arr) {\n long long res = 0;\n\n for(int i = 0; i < arr.size(); i++) {\n res += (1ll * arr[i]);\n }\n\n return res;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long total_damage = find_total(damage);\n long long total = 0;\n vector<int> time(health.size());\n vector<double> efficiency(health.size());\n vector<tuple<int, double, int>> order(health.size());\n\n for(int i = 0; i < health.size(); i++) {\n time[i] = ceil((1.0 * health[i])/power);\n efficiency[i] = (1.0 * damage[i])/time[i];\n\n order[i] = make_tuple(i, efficiency[i], time[i]);\n }\n\n sort(order.begin(), order.end(), this->comparator);\n\n for(auto i: order) {\n total += (total_damage) * time[get<0>(i)];\n total_damage -= damage[get<0>(i)];\n }\n\n return total;\n }\n};", "memory": "158173" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "#include<bits/stdc++.h>\nbool customComparison(pair<long long, long long> a, pair<long long, long long> b){\n return (((double) (a.second) / (double) (a.first)) > ((double) (b.second) / (double) (b.first))); \n } \nclass Solution {\npublic:\n \n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<long long , long long>> damagex;\n \n long long sum = 0;\n for(int i = 0; i < damage.size(); i++){\n long long x = 0;\n if(health[i] % power == 0){\n x = health[i] / power;\n }\n else{\n x = health[i] / power;\n x++;\n }\n pair<long long, long long> p = {x, damage[i]}; \n damagex.push_back(p);\n sum += damage[i];\n }\n\n sort(damagex.begin(), damagex.end(), customComparison);\n\n\n long long ans = 0;\n\n for(int i = 0; i < damagex.size(); i++){\n ans += (sum * damagex[i].first);\n sum -= damagex[i].second;\n }\n \n return ans;\n \n \n \n \n \n }\n};", "memory": "158173" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long res = 0, sum = 0;\n priority_queue<pair<double, int>> pq;\n for(int i = 0; i < damage.size(); ++i){\n int time = health[i]/power + ((health[i]%power)?1:0);\n pq.push({(1.0 * damage[i])/ (1.0*time), i });\n sum += damage[i];\n }\n while(pq.size()){\n auto [d, i] = pq.top(); pq.pop();\n int time = health[i]/power + ((health[i]%power)?1:0);\n res += time * sum;\n sum -= damage[i];\n }\n return res;\n}\n\n};", "memory": "158861" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long minDamage(int k, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n\n ll sum = accumulate(damage.begin(), damage.end(), 0LL);\n\n vector<pair<ll,ll>> v;\n for(int i = 0; i<n;i++){\n health[i] = (health[i]+k-1)/k;\n }\n\n for(int i=0; i<n;i++) v.push_back({damage[i], health[i]});\n \n\n sort(v.begin(), v.end(), [](const pair<ll,ll>& a, const pair<ll,ll>& b){\n ll n = a.first*b.second;\n ll m = b.first*a.second;\n return (n>m);\n });\n\n ll ans = 0;\n\n for (int i = 0; i < n; i++) {\n int hel = v[i].second;\n ll cnt = hel;\n ans += cnt * sum;\n sum -= v[i].first;\n }\n\n return ans;\n }\n};", "memory": "158861" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<double,pair<int,int>>>v; int total=0;\n long long ans=0;\n for(int i=0;i<damage.size();i++){\n total+=damage[i];\n int shots=ceil((double)health[i]/(double)power);\n v.push_back({(double)damage[i]/(double)shots,{damage[i],health[i]}});\n }\n sort(v.rbegin(),v.rend());\n \n for(int i=0;i<v.size();i++){\n long long shots=ceil((double)v[i].second.second/(double)power); \n ans+=shots*total;\n v[i].second.second-=power;\n \n total-=v[i].second.first;\n\n }\n return ans;\n\n }\n};", "memory": "159550" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = health.size();\n long long sum = 0, ans = 0;\n for (int i = 0; i < n; i++) {\n if (health[i] % power == 0) health[i] /= power;\n else {\n health[i] /= power;\n health[i]++;\n }\n sum += damage[i];\n }\n\n vector<pair<double, int>> v;\n\n for (int i = 0; i < n; i++) {\n v.push_back({(double)damage[i] / health[i], i});\n }\n\n sort(v.rbegin(), v.rend());\n\n for (int i = 0; i < n; i++) {\n ans += sum * health[v[i].second];\n sum -= damage[v[i].second];\n }\n return ans;\n }\n};", "memory": "159550" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool comp(const pair <long double, pair <int, int>> &a, const pair <long double, pair <int, int>> &b) {\n return a.first > b.first;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector <pair <long double, pair <long long, long long>>> desc(n);\n long long ans = 0;\n\n for (int i = 0; i < n; i++) {\n int time_to_kill = health[i] / power;\n if (health[i] % power)\n ++time_to_kill;\n \n long double dang = damage[i] / (long double)time_to_kill;\n \n desc[i] = {dang, {damage[i], health[i]}};\n }\n\n sort(desc.begin(), desc.end(), comp);\n vector <long long> suff(n);\n suff[n - 1] = desc[n - 1].second.first;\n\n for (int i = n - 2; i >= 0; i--)\n suff[i] = suff[i + 1] + desc[i].second.first;\n \n for (int i = 0; i < n; i++) {\n ans += ((desc[i].second.second) / power) * suff[i];\n if ((desc[i].second.second) % power)\n ans += suff[i];\n }\n\n return ans;\n }\n};", "memory": "160239" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> timeToKill;\n for(int i = 0; i < health.size(); i++) {\n if(health[i] < power) {\n timeToKill.push_back(1);\n } else if(health[i] > power && health[i] % power != 0) {\n timeToKill.push_back(health[i] / power + 1);\n } else {\n timeToKill.push_back(health[i] / power);\n }\n }\n\n vector<float> ratio;\n for(int i = 0; i < health.size(); i++) {\n float rati = (damage[i] * 1.0f) / timeToKill[i];\n ratio.push_back(rati);\n }\n\n priority_queue<pair<float, int>> pq;\n for(int i = 0; i < timeToKill.size(); i++) {\n pq.push({ratio[i], i});\n }\n\n long long totalSum = 0;\n for(int i = 0; i < damage.size(); i++) {\n totalSum += damage[i];\n }\n\n long long minDmg = 0;\n while(!pq.empty()) {\n float ratii = pq.top().first;\n int idx = pq.top().second;\n pq.pop(); // Pop the element after accessing it\n\n // Check for overflow before performing the multiplication\n if (totalSum > LLONG_MAX / timeToKill[idx]) {\n // Handle overflow, you can return an error code or do something appropriate\n return -1; // Example error code\n }\n\n long long term = totalSum * timeToKill[idx];\n minDmg += term;\n totalSum -= damage[idx];\n }\n\n return minDmg;\n }\n};", "memory": "160239" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "bool cmp(pair<long long int,long long int>&a,pair<long long int,long long int>&b){\n return (((long double)a.first/(long double)a.second)>((long double)b.first/(long double)b.second));\n }\nclass Solution {\npublic:\n long long int f(long long int a,int p){\n if(a%p==0){\n return a/p;\n }\n else{\n return a/p+1;\n }\n }\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<long long int,long long int>>v;\n int n=damage.size();\n for(int i=0;i<damage.size();i++){\n v.push_back({damage[i],f(health[i],power)});\n }\n sort(v.begin(),v.end(),&cmp);\n long long int ans=0;\n long long int sum=0;\n for(auto x:damage) sum+=x;\n vector<long long int>temp(n);\n for(int i=0;i<n;i++){\n temp[i]=v[i].first;\n }\n // for(int i=0;i<n;i++){\n // cout<<v[i].first<<\" \";\n // }\n for(int i=1;i<n;i++) temp[i]+=temp[i-1];\n for(int i=0;i<n;i++){\n if(i==0){\n ans=ans+(sum)*v[i].second;\n }\n else{\n ans=ans+(sum-temp[i-1])*v[i].second;\n }\n }\n return ans;\n }\n};", "memory": "160928" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n static bool compare(pair<int,long long> a, pair<int,long long> b){\n return ( (1LL*b.first*a.second) < (1LL*a.first*b.second) );\n }\n \n int ceiling(int a, int b){\n int c=a/b;\n if((b*c)==a) return c;\n else return c+1;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n vector<int> time;\n for(int i=0;i<n;i++){\n time.push_back(ceiling(health[i],power));\n }\n vector<pair<int,long long>> order;\n for(int i=0;i<n;i++){\n order.push_back({damage[i],time[i]});\n }\n sort(order.begin(),order.end(),compare);\n //for(int i=0;i<n;i++) cout<<order[i].first<<order[i].second<<\" \";\n for(int i=1;i<n;i++){\n long long a=order[i].second;\n a+=order[i-1].second;\n order[i].second=a;\n }\n long long ans=0;\n for(int i=0;i<n;i++){\n ans+=order[i].first*order[i].second;\n }\n return ans;\n }\n};", "memory": "160928" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nstatic bool comp(pair<long long int, long long int>&a, pair<long long int,long long int> &b){\n double x = (double)a.first/(double)a.second;\n double y = (double)b.first/(double)b.second;\n return x>y;\n}\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = health.size();\n vector<long long int>time(n,0);\n for(int i=0;i<n;i++){\n time[i] = ceil((double)health[i]/power);\n }\n vector<pair<long long int,long long int>>v;\n for(int i=0;i<n;i++){\n v.push_back({damage[i],time[i]});\n }\n sort(v.begin(),v.end(),comp);\n long long int total=0;\n for(int i=0;i<n;i++){\n total+=damage[i];\n }\n long long int ans=0;\n for(int i=0;i<n;i++){\n ans+=total*v[i].second;\n total=total-v[i].first;\n }\n return ans;\n }\n};", "memory": "161616" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using ii = pair<int,int>;\n #define F first \n #define S second\n long long minDamage(int power, vector<int>& d, vector<int>& h) {\n int n = d.size();\n vector<int> t(n);\n for(int i=0;i<n;i++){\n t[i] = (h[i]+power-1)/power;\n }\n vector<pair<double,pair<int,int>>> v;\n for(int i=0;i<n;i++){\n double persec = (1.0*d[i])/(1.0*t[i]);\n v.push_back({persec,{d[i],t[i]}});\n }\n sort(v.begin(),v.end());\n reverse(v.begin(),v.end());\n long long ans = 0;\n long long sum = 0;\n for(int i=0;i<n;i++){\n sum += (v[i].S.S);\n ans += (1LL*sum*v[i].S.F);\n }\n return ans;\n }\n};", "memory": "161616" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& d, vector<int>& h) {\n int n=d.size();\n vector<int>vdiv;\n vector<pair<double,int>>vc;\n ll sum=0;\n for(int i=0;i<n;i++){\n int curr=(h[i]+power-1)/power;\n vdiv.push_back(curr);\n double here=(double)(((double)d[i])/(double)vdiv[i]);\n //cout<<d[i]<<\" \"<<curr<<\" \"<<here<<endl;\n vc.push_back({here,i});\n sum+=d[i];\n }\n sort(vc.begin(),vc.end());\n reverse(vc.begin(),vc.end());\n ll ans=0;\n \n \n for(int i=0;i<n;i++){\n int ind=vc[i].second;\n ans+=(ll)((ll)sum*(ll)vdiv[ind]);\n cout<<ind<<endl;\n sum-=d[ind];\n }\n return ans;\n }\n};", "memory": "162305" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n static bool sortbyCond(const pair<double,pair<int, int>>& a, pair<double,pair<int, int>>& b)\n {\n return a.first>b.first;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n \n vector<int>at;\n int n = damage.size();\n for(int i=0 ; i<n ; i++){\n int curr = health[i]%power;\n int q = health[i]/power;\n if(curr)q++;\n at.push_back(q);\n \n }\n\n vector<pair<double,pair<int,int>>>vp;\n for(int i=0 ; i<n ; i++){\n \n double ratio = (double)damage[i]/(double)at[i];\n vp.push_back({ratio,{damage[i],at[i]}});\n\n }\n\n sort(vp.begin(),vp.end(),sortbyCond);\n\n long long ans = 0;\n long long pref = 0;\n\n for(int i=0 ; i<vp.size() ; i++){\n\n pref+=vp[i].second.second;\n ans+=(vp[i].second.first*pref);\n }\n\n return ans;\n }\n};", "memory": "162305" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n int n = damage.size();\n long long sum = 0;\n vector<int> hits;\n vector<pair<double, int>> damageRate;\n for(int i = 0; i < n; i++){\n sum += damage[i];\n hits.push_back(ceil((double)health[i]/(double)power));\n damageRate.push_back({(double)damage[i]/(double)hits[i], i});\n }\n sort(damageRate.rbegin(), damageRate.rend());\n long long ans = 0;\n for(int i = 0; i < n; i++){\n ans += (1LL*sum*(hits[damageRate[i].second]));\n sum -= (1LL*damage[damageRate[i].second]);\n }\n return ans;\n }\n};", "memory": "162994" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int p, vector<int>& dmg, vector<int>& hp) {\n using LL = long long;\n LL res = 0;\n int n = dmg.size();\n LL dmgSum = 0;\n vector<int> F;\n vector<float> R;\n vector<tuple<float, float, int>> v;\n \n for (int i = 0; i < n; ++i) {\n int d = dmg[i];\n int h = hp[i];\n dmgSum += d;\n \n F.push_back(ceil((float) h / p));\n R.push_back((float)d / F[i]);\n v.push_back({R[i], (float)1 / F[i], i});\n }\n \n ranges::sort(v.rbegin(), v.rend());\n \n for (auto& [r, _, i] : v) {\n res += (F[i] * dmgSum);\n dmgSum -= dmg[i];\n }\n \n return res;\n }\n};", "memory": "162994" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n\n vector<long long> time(n);\n for (int i = 0; i < n; i++)\n time[i] = (health[i] + power - 1)/power;\n \n vector<pair<double, int>> dps;\n for (int i = 0; i < n; i++)\n dps.push_back({(double)damage[i]/(double)time[i], i});\n\n sort(dps.begin(), dps.end());\n\n long long ans = 0, total = 0;\n for (int i = 0; i < n; i++)\n total += damage[i];\n \n for (int i = n - 1; i >= 0; i--)\n {\n int idx = dps[i].second;\n\n ans += (total * time[idx]);\n total -= damage[idx];\n }\n return ans;\n }\n};", "memory": "163683" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int> count(n, 0);\n for(int i = 0; i < n; i++) {\n count[i] = health[i] / power;\n if(health[i] % power != 0) count[i]++;\n }\n vector<double> damageD(damage.begin(), damage.end());\n priority_queue<pair<double, int>> pq;\n for(int i = 0; i < n; i++) {\n pq.push({damageD[i] / count[i], i});\n }\n int i = 0;\n int damageSum = 0;\n for(int i = 0; i < n; i++) {\n // cout << count[i] << \" \";\n damageSum += damage[i];\n }\n // cout << endl;\n long long res = 0;\n while(i < n) {\n auto [val, idx] = pq.top();\n // cout << val << endl;\n pq.pop();\n res += damageSum * count[idx];\n damageSum -= damage[idx];\n\n i++;\n }\n return res;\n }\n};", "memory": "163683" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int>killTime;\n for( int i=0; i < n; i++ ){\n int t = health[i]/power;\n if( health[i]%power != 0 ) t+=1;\n killTime.push_back(t);\n }\n priority_queue<pair<double,int>>q;\n for( int i=0; i < n; i++ ){\n double dps = (double)damage[i]/(double)killTime[i];\n q.push({dps,i});\n }\n\n long long sum = 0;\n for( int i=0; i < n; i++ ) sum += damage[i];\n\n long long ans = 0;\n while(!q.empty()){\n auto x = q.top();\n int time = killTime[x.second];\n q.pop();\n ans += sum*(long long)time;\n sum -= damage[x.second];\n }\n\n return ans;\n\n }\n};", "memory": "164371" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "typedef long long int ll;\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n ll n = damage.size();\n ll total = 0;\n vector<pair<ll, ll>> v;\n vector<int> time;\n for(auto it: health){\n int val = (it % power == 0) ? it / power : it / power + 1;\n time.push_back(val);\n }\n for(int i = 0; i < n; i++){\n total += damage[i];\n v.push_back({damage[i], time[i]});\n }\n sort(v.begin(), v.end(), [&](pair<ll, ll> p1, pair<ll, ll> p2){\n return (double)p1.first/p1.second > (double)p2.first/p2.second;\n });\n ll ans = 0;\n for(auto it: v){\n ans += total * it.second;\n total -= it.first;\n }\n return ans;\n }\n};", "memory": "164371" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<double, int>> pq;\n int n = damage.size();\n long long s = 0;\n vector<int> time;\n for (int i=0; i<n; i++) {\n s += damage[i];\n if (health[i]%power != 0) time.push_back(health[i]/power + 1);\n else time.push_back(health[i]/power);\n pq.push({double(damage[i])/double(time[i]), i});\n }\n\n long long ans = 0;\n while (!pq.empty()) {\n int i = pq.top().second;\n pq.pop();\n\n ans += s*time[i];\n s -= damage[i];\n }\n\n return ans;\n }\n};", "memory": "165060" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool mycomp(pair<double,int> &a, pair<double,int> &b)\n {\n return a.first > b.first;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long sum = accumulate(damage.begin(),damage.end(),0);\n vector <pair<int ,int>> dh;\n int n = damage.size();\n vector <int> timeTokill;\n for(int i=0;i<n;i++)\n {\n int time;\n if(health[i] % power != 0)\n time = (health[i]/power)+1;\n else\n time = health[i]/power;\n timeTokill.push_back(time);\n }\n vector <pair<double,int>> v;\n for(int i=0;i<n;i++)\n {\n double dt = damage[i];\n dt = dt/timeTokill[i];\n v.push_back({dt,i});\n }\n sort(v.begin(),v.end(),mycomp);\n long long maxP = 0;\n for(int i=0;i<n;i++)\n {\n int index = v[i].second;\n int temp;\n if(health[index] % power != 0)\n temp = (int)(health[index]/power) + 1;\n else\n temp = (health[index]/power);\n maxP += (sum*temp);\n cout << maxP << endl;\n sum -= damage[index]; \n }\n return maxP;\n }\n};", "memory": "165060" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<tuple<double,long long,long long>>pq;\n long long tsum=0;\n for(int i=0;i<damage.size();i++){\n long long val=ceil((health[i])/(double)power);\n pq.push({damage[i]/(double)val,damage[i],health[i]});\n tsum+=damage[i];\n }\n\n long long ans=0;\n\n while(!pq.empty()){\n long long d=get<1>(pq.top());\n long long h=get<2>(pq.top());\n pq.pop();\n long long vl=ceil((h)/(double)power)*tsum;\n ans+=vl;\n tsum-=d;\n \n }\n\n return ans;\n }\n};", "memory": "165749" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=health.size();\n\n for(int i=0;i<n;i++){\n int a=health[i]/power;\n if(health[i]%power)a++;\n health[i]=a;\n }\n\n // for(auto it:health)cout<<it<<\" \";\n\n long long sum=accumulate(damage.begin(),damage.end(),1ll*0);\n\n priority_queue<pair<double,pair<long long,int>>>pq;\n\n for(int i=0;i<n;i++){\n pq.push({double(double(damage[i])/health[i]),{damage[i],health[i]}});\n }\n\n long long ans=0;\n\n while(!pq.empty()){\n long long a = pq.top().second.first;\n long long b = pq.top().second.second;\n pq.pop();\n\n ans+=(sum*b);\n // cout<<sum<<\" \"<<a<<\" \"<<b<<\" \"<<endl;\n sum-=a;\n }\n\n return ans;\n }\n};", "memory": "165749" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = health.size();\n vector<int> time(n, 0);\n \n for(int i=0; i<n; i++)\n {\n time[i] = (health[i] + power - 1) / power;\n }\n \n vector<double> DPS(n, 0);\n \n priority_queue<pair<double, int>> pq;\n \n for(int i=0; i<n; i++)\n {\n DPS[i] = (double) (damage[i] / (time[i] * 1.0));\n pq.push({DPS[i], i});\n }\n \n long long sum = accumulate(damage.begin(), damage.end(), 0ll);\n long long ans = 0;\n \n while(!pq.empty())\n {\n int index = pq.top().second;\n pq.pop();\n \n ans = ans + (long long) sum * (time[index]);\n sum = sum - (long long) damage[index];\n }\n \n return ans;\n }\n};", "memory": "166438" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=health.size();\n vector<int>time(n,0);\n priority_queue<pair<double,int>> pq;\n\n for(int i=0;i<n;i++){\n time[i]+= health[i]%power==0?(health[i]/power):(health[i]/power+1);\n }\n vector<double> damage_ps(n,0);\n for(int i=0;i<n;i++){\n damage_ps[i]=(double)(damage[i]/(time[i]*1.0));\n pq.push({damage_ps[i],i});\n }\n\n long long total=accumulate(damage.begin(),damage.end(),0);\n long long ans=0;\n\n while(!pq.empty()){\n int indx=pq.top().second; \n pq.pop();\n ans+=total*(time[indx]);\n total=total-damage[indx];\n \n }\n\n \n return ans;\n }\n};\n", "memory": "166438" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<double,pair<long long,long long>>>v;\n double p=power;\n long long sum=0;\n for(int i=0;i<health.size();i++)\n {\n sum+=damage[i];\n double h=health[i];\n double time=ceil(h/p);\n double d=damage[i];\n double ratio=d/time;\n v.push_back({ratio,{damage[i],health[i]}});\n }\n sort(v.rbegin(),v.rend());\n long long ans=0;\n for(auto i:v)\n {\n double h=i.second.second;\n double time=ceil(h/p);\n ans+=time*sum;\n sum-=i.second.first;\n }\n return ans;\n }\n};", "memory": "167126" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) \n {\n long res = 0;\n long d_sum = accumulate(damage.begin(), damage.end(), 0L);\n vector<array<double, 3>> pq;\n \n for(int i = 0 ; i < damage.size() ; i++)\n {\n double dps = 1.0 * damage[i] / ((health[i] + power-1) / power);\n pq.push_back({dps, (double)damage[i], (double)i});\n }\n sort(pq.rbegin(), pq.rend());\n for(auto &[dps, d, i] : pq)\n {\n res += d_sum * ((health[i] + power-1) / power);\n d_sum -= damage[i];\n }\n return res; \n }\n};", "memory": "167126" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n auto compare = [](pair<ll,ll>&a,pair<ll,ll>&b){\n double x = (double)a.first/a.second;\n double y = (double)b.first/b.second;\n\n return x>y;\n\n };\n\n vector<ll>time;\n for(int i=0;i<health.size();i++){\n time.push_back(ceil((double)health[i]/power));\n }\n //Now we need to have max damage in min time:\n vector<pair<ll,ll>>v;\n\n for(int i=0;i<damage.size();i++){\n v.push_back({damage[i],time[i]});\n }\n\n sort(v.begin(),v.end(),compare);\n\n ll total_damage =0;\n for(auto it:damage){\n total_damage += it;\n }\n\n ll res = 0;\n\n for(auto it:v){\n res += it.second * total_damage;\n total_damage -= it.first;\n }\n\n return res;\n }\n};", "memory": "167815" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n using ll=long long;\n using pp=pair<double,int>;\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pp> res;\n vector<ll> time;\n\n for(int i=0;i<damage.size();i++){\n time.push_back(ceil(health[i]/(double)power));\n double dps=damage[i]/(double)time[i];\n res.push_back({dps,i});\n }\n\n ll sm=0,ans=0;\n for(int val: damage) sm+=val;\n\n sort(res.rbegin(),res.rend());\n for(auto [_,idx]: res){\n ans+=time[idx]*sm;\n sm-=damage[idx];\n }\n\n return ans;\n }\n};", "memory": "167815" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<long long> tr;\n for(int i = 0; i < health.size(); i++){\n tr.push_back(ceil(static_cast<double>(health[i]) / power));\n }\n vector<pair<double, long long>> tot;\n int dmg = 0;\n for(int i = 0; i < damage.size(); i++){\n tot.push_back({static_cast<double>(damage[i]) / tr[i], i});\n dmg += damage[i];\n }\n sort(tot.begin(), tot.end());\n long long min = 0;\n for(int i = tot.size()-1; i >= 0; i--){\n long long x = tot[i].second;\n while(tr[x]){\n min += dmg;\n //cout << dmg << endl;\n tr[x]--;\n }\n dmg -= damage[x];\n }\n return min;\n }\n};", "memory": "170570" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int pow, vector<int>& dam, vector<int>& hea) \n {\n\n vector<long long >ht;\n\n\n for(auto h:hea)\n {\n ht.push_back((h+pow-1)/pow);\n\n }\n\n vector<pair<double ,int>>s;\n\n long long sum=0;\n\n for(auto k: dam)\n {\n sum+=k;\n }\n\n for(int i=0;i<hea.size();i++)\n {\n \n s.push_back({(1.0*dam[i])/(1.0*ht[i]),i});\n }\n\n sort(s.rbegin(),s.rend());\n long long ans=0,ctr=0;\n while(sum>0 && ctr<s.size())\n {\n \n \n int i=s[ctr].second;\n\n \n ans+=(sum*ht[i]);\n\n sum-=dam[i];\n \n \n\n \n\n \n\n \n ctr++;\n }\n\n\n return ans;\n\n\n \n }\n};", "memory": "170570" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<double,pair<long long,long long>>> prr;\n int n=damage.size();\n for(int i=0;i<n;i++){\n long long time=(health[i]+power-1)/power;\n double ratio=(double)damage[i]/time;\n prr.push_back({ratio,{damage[i],time}});\n }\n \n sort(prr.begin(),prr.end());\n long long cnt=0;\n vector<long long> pre(n,0);\n for(int i=0;i<n;i++){\n cnt+=prr[i].second.first;\n pre[i]=cnt;\n }\n long long ans=0;\n for(int i=n-1;i>=0;i--){\n ans+=pre[i]*prr[i].second.second;\n }\n \n return ans;\n }\n};", "memory": "171259" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#ifndef lowbit\n#define lowbit(x) (x & (-x))\n#endif\n#define ll long long\n#define pii pair<int, int>\n#define pll pair<ll, ll>\n#define pb push_back\n#define F first\n#define S second\n#define all(_obj) _obj.begin(), _obj.end()\n#define vi vector<int>\n#define vvi vector<vector<int>>\n#define vll vector<ll>\n#define vvll vector<vector<ll>>\n#define rep(i,a,b) for(int i=(a);i<=(b);++i)\n#define per(i,a,b) for(int i=(a);i>=(b);--i)\n#define _up(x) (int)ceil(1.0*x)\n#define _down(x) (int)floor(1.0*x)\n#define debug(a) cout << #a << \" = \" << a << endl;\n#define debug2(a,b) cout<<#a<<\" = \"<<a<<' '<<#b<<\" = \"<<b<<endl;\n#define debug3(a,b,c) cout<<#a<<\" = \"<<a<<' '<<#b<<\" = \"<<b<<' '<<#c<<\" = \"<<c<<endl;\n#define INF 0x3f3f3f3f3f3f3f3f\n#define eps 0.000000001\nconst int mod = 1e9 + 7;\nconst int N = 1e6 + 7;\nll gcd(ll x, ll y){return !y?x:gcd(y, x%y);}\nll lcm(ll x, ll y){return x/gcd(x, y)*y;}\nll max(ll x, ll y){return x > y ? x : y;}\nll min(ll x, ll y){return x < y ? x: y;}\nll Mod(ll x, int mod){return (x % mod + mod) % mod;}\nll qml(ll x, ll y, ll mod){\n\tll res = 1, cur = x;\n\twhile(y){\n\t\tif(y&1)\tres = res*cur % mod;\n\t\tcur = cur * cur % mod;\n\t\ty >>= 1;\n\t}\t\n\treturn res % mod;\n} \nclass UF {\npublic:\n int n;\n int comp_cnt;\n vector<int> fa;\n vector<int> sz;\n \npublic:\n UF(int _n): n(_n), comp_cnt(_n), fa(_n), sz(_n, 1) {\n iota(fa.begin(), fa.end(), 0);\n }\n \n int findset(int x) {\n return fa[x] == x ? x : fa[x] = findset(fa[x]);\n }\n \n bool unite(int x, int y) {\n x = findset(x);\n y = findset(y);\n if (x == y) {\n return false;\n }\n if (sz[x] < sz[y]) {\n swap(x, y);\n }\n fa[y] = x;\n sz[x] += sz[y];\n --comp_cnt;\n return true;\n }\n \n bool connected(int x, int y) {\n return findset(x) == findset(y);\n }\n};\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& da, vector<int>& he) {\n ll ans = 0, n = da.size();\n vector<pair<double, pair<ll, ll>>> res;\n for (int i = 0; i < n; ++i) {\n ll time = (he[i] + power - 1) / power;\n // pair<ll, ll> p(da[i], he[i]);\n res.push_back(make_pair<double, pair<ll, ll>>(time * 1.0 / da[i], make_pair<ll, ll>(-da[i], he[i])));\n }\n sort(all(res));\n vll pS(n + 1, 0);\n for (int i = 0; i < n; ++i) {\n pS[i + 1] = pS[i] - res[i].second.first;\n }\n for (int i = 0; i < n; ++i) {\n ans += (pS[n] - pS[i]) * ((res[i].second.second + power - 1) / power);\n }\n return ans;\n }\n};", "memory": "171259" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<vector<int>> arr(n);\n for(int i=0; i<n; ++i) {\n arr[i] = {damage[i], health[i], power};\n }\n sort(begin(arr), end(arr), [](vector<int>&a, vector<int>&b){\n int time1 = a[1] / a[2] + (a[1] % a[2] != 0);\n int time2 = b[1] / b[2] + (b[1] % b[2] != 0);\n\n int cost1 = time1 * (a[0] + b[0]) + time2 * b[0];\n int cost2 = time2 * (a[0] + b[0]) + time1 * a[0];\n\n return cost1 < cost2;\n });\n for(int i=0; i<n; ++i) {\n damage[i] = arr[i][0];\n health[i] = arr[i][1];\n }\n long long ans = 0, sum = accumulate(begin(damage), end(damage), 0LL);\n for(int i=0; i<n; ++i) {\n long long prevAns = ans;\n // cout << \"health[i] = \" << health[i] << \" damage[i] = \" << damage[i] << \"\\n\";\n // cout << \"no. of seconds to kill = \" << ((health[i] / power) + ((health[i] % power) != 0)) << \"\\n\"; \n ans += sum * ((health[i] / power) + ((health[i] % power) != 0));\n sum -= damage[i];\n // cout << \"damage dealt = \" << ans - prevAns << \"\\n\";\n }\n return ans;\n }\n};", "memory": "171948" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = health.size();\n vector<vector<int>>temp(n,vector<int>(2));\n\n for(int i = 0; i < n; i++){\n int time;\n if(health[i]%power==0) time= health[i]/power;\n else time= health[i]/power+1;\n temp[i] = {damage[i],time};\n }\n auto lambda = [](vector<int>& a, vector<int>& b){\n double x = double(a[0]/(double)a[1]);\n double y = double(b[0]/(double)b[1]);\n\n return x>y;\n };\n sort(temp.begin(),temp.end(),lambda);\n\n long long total_dam = accumulate(damage.begin(),damage.end(),0);\n long long ans = 0;\n\n for(int i = 0; i < n; i++){\n ans += (temp[i][1]*total_dam);\n total_dam -= temp[i][0];\n }\n return ans;\n }\n};", "memory": "171948" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& access) {\n long long n = access.size();\n long long tot_sum = 0;\n vector<int>v(n);\n for(long long i=0;i<n;i++){\n long long temp;\n if(access[i]%power==0){\n temp = access[i]/power;\n }\n else{\n temp = (access[i]/power) + 1;\n }\n v[i] = temp;\n tot_sum+=damage[i];\n }\n int x=0;\n for(auto it:damage){\n x++;\n }\n long long sol = 0;\n vector<double> temp(n);\n priority_queue<pair<double,pair<long long,long long>>> piror;\n for(int i=0;i<n;i++){\n for(int k=0;k<1;++k){\n int lop;\n }\n double x = (double)damage[i];\n double y = (double)v[i];\n double p = x/y;\n temp[i] = p;\n piror.push({p,{v[i],damage[i]}});\n }\n int y=0,z=0;\n for(auto it:damage){\n y++;\n }\n\n while(!piror.empty())\n {\n for(int k=0;k<1;++k){\n int lop;\n }\n auto it = piror.top();\n long long cnt = it.second.first;\n long long d = it.second.second;\n piror.pop();\n sol += tot_sum*cnt;\n tot_sum -= d;\n }\n for(auto it:damage){\n z++;\n }\n for(int k=0;k<1;++k){\n int lop;\n }\n\n return sol;\n }\n};", "memory": "172636" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long n = health.size();\n long long sum = 0;\n vector<int> steps(n);\n for(long long i=0;i<n;i++){\n long long temp;\n if(health[i]%power==0){\n temp = health[i]/power;\n }\n else{\n temp = (health[i]/power) + 1;\n }\n steps[i] = temp;\n // cout<<steps[i]<<\" \";\n sum+=damage[i];\n }\n\n long long ans = 0;\n \n vector<double> temp(n);\n priority_queue<pair<double,pair<long long,long long>>> pq;\n for(int i=0;i<n;i++){\n double x = (double)damage[i];\n double y = (double)steps[i];\n double p = x/y;\n temp[i] = p;\n pq.push({p,{steps[i],damage[i]}});\n }\n\n while(!pq.empty())\n {\n auto it = pq.top();\n long long cnt = it.second.first;\n long long d = it.second.second;\n\n // cout<<it.first<<\" \"<<cnt<<\" \"<<d<<endl;\n\n pq.pop();\n\n ans += sum*cnt;\n sum -= d;\n }\n\n return ans;\n }\n};", "memory": "172636" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <map>\n#include <cmath>\n\nusing namespace std;\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n multimap<double, pair<int, int>, greater<>> mp;\n int n = damage.size();\n\n // Calculate the proportion of damage to the number of attacks needed for each enemy\n for (int i = 0; i < n; i++) {\n int attacks = ceil((double)health[i] / power); // number of attacks needed to kill enemy i\n double damagePerAttack = (double)damage[i] / attacks; // proportion of damage to attacks\n mp.insert(make_pair(damagePerAttack, make_pair(damage[i], health[i])));\n }\n\n long long total = 0;\n long long sum = accumulate(damage.begin(), damage.end(), 0LL);\n\n // Iterate over the enemies, sorted by the damage per attack ratio in descending order\n for (auto it : mp) {\n int count = ceil((double)it.second.second / power); // number of attacks to kill this enemy\n total += 1LL * sum * count; // accumulate the total damage dealt to Bob\n sum -= it.second.first; // decrease the remaining total damage\n }\n\n return total;\n }\n};\n", "memory": "173325" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int sum = 0;\n for(auto it : damage) sum += it;\n vector<pair<int, int>> arr;\n for(int i = 0; i < health.size(); i++){\n int temp = health[i]/power;\n if(health[i] % power != 0) temp++;\n arr.push_back({temp, i});\n }\n vector<pair<double, int>> store;\n for(int i = 0; i < damage.size(); i++){\n // cout<<damage[i]<<\" \"<<arr[i].first<<endl;\n // cout<<damage[i]/arr[i].first<<endl;\n store.push_back({damage[i]/(double)arr[i].first, arr[i].second});\n }\n sort(store.begin(), store.end(), greater<pair<double, int>>());\n for(auto it : store) cout<<it.first<<\" \"<<it.second<<endl;\n vector<int> array;\n for(auto it : store) array.push_back(it.second);\n long long ans = 0;\n for(auto it : array){\n int idx = it;\n int num = health[idx]/power;\n if(health[idx] % power != 0) num++;\n cout<<num<<endl;\n ans += (sum*num);\n sum -= damage[idx];\n }\n return ans;\n }\n};", "memory": "173325" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int checkceil(int x , int y){\n if(x%y == 0){\n return x/y;\n }\n else{\n return (x/y)+1;\n }\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> hits;\n long long ans = 0;\n long long dam = 0;\n\n for(int i=0; i<health.size(); i++){\n hits.push_back(checkceil(health[i], power));\n dam += damage[i];\n }\n\n //{{d/hits,-hits},d}\n priority_queue<pair< pair<double,int> , int>> pq;\n\n for(int i=0; i<hits.size(); i++){\n double x = damage[i]/(hits[i]*1.0);\n pq.push({{x,-hits[i]}, damage[i]});\n }\n\n while(!pq.empty()){\n int h = abs(pq.top().first.second);\n int d = pq.top().second;\n pq.pop();\n\n ans += h*dam;\n\n dam -= d;\n }\n\n return ans;\n\n\n }\n};", "memory": "174014" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "\n#define ll long long int\n\nclass Solution {\npublic:\n \n ll minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n ll ans = 0;\n ll sum = 0;\n vector<double> arr(n);\n for(int i=0; i<n; i++) {\n arr[i] = double(damage[i]) / ceil(double(health[i]) / power);\n sum += damage[i];\n }\n vector<vector<double>> store(n);\n for(int i=0; i<n; i++) {\n store[i] = {arr[i], double(i)};\n }\n sort(store.rbegin(), store.rend());\n for (int i=0; i<n; i++) {\n ll currDamage = damage[store[i][1]];\n ll currHealth = health[store[i][1]];\n ll times = ceil(double(currHealth) / power);\n ans += times*sum;\n sum -= currDamage;\n }\n return ans;\n }\n};\n", "memory": "174014" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n \n int n =d.size();\n vector<ll>time(n);\n for(int i =0;i<n;i++){\n time[i]=ceil((1.0*h[i])/(1.0*p));\n }\n vector<pair<double,pair<ll,ll>>>v;\n for(int i=0;i<n;i++){\n v.push_back({1.0*d[i]/time[i],{d[i],h[i]}});\n }\n sort(v.rbegin(),v.rend());\n ll sum=0;\n for(int i =0;i<n;i++){\n sum+=d[i];\n }\n ll ans=0;\n for(int i =0;i<n;i++){\n \n ans+=(sum*ceil((1.0*v[i].second.second)/(1.0*p)));\n sum-=v[i].second.first;\n\n }\n return ans;\n }\n};", "memory": "174703" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& dam, vector<int>& h) {\n long long td = accumulate(dam.begin(),dam.end(),0LL);\n set<pair<double,int>> s; int n= h.size();\n for(int i = 0;i<n;i++){\n int time_to_kill = (h[i]+power-1)/power;\n int index = i;\n s.insert({1.0*dam[i]/time_to_kill*1.0,i});\n }\n\n long long ans = 0;\n while(s.size()){\n pair<double,int> p = *s.rbegin(); s.erase(*s.rbegin());\n int i = p.second;\n int t =( h[i]+power-1)/power;\n ans+=1LL*(td)*t;\n td-=dam[i];\n }\n return ans;\n }\n};", "memory": "174703" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int len = damage.size();\n vector<int> secs(len, 0);\n multimap<double, int> pq; // d1/sec1, index\n for (int i=0; i<len; i++) {\n secs[i] = ceil((double)health[i] / power);\n double temp = ((double)-damage[i] / secs[i]);\n pq.insert(pair(temp, i));\n }\n long long time = 0;\n long long ans = 0;\n for (auto [tmp, i] : pq) {\n ans += damage[i] * (time + secs[i]);\n time += secs[i];\n }\n return ans;\n }\n};\n\n\n\n// 1,2,3,4\n// 1,2,2,2 times\n// 1(t1) + 2(t1+t2) + 3(t1+t2+t3) + 4(t1+t2+t3+t4)\n// t1(a1+a2+a3+a4) + t2(a2+a3+a4)\n\n// t1(1+2+3) + t2(1+2) + t3(3)\n// t1(a+b) + t2(b)\n// t2(a+b) + t1(a)\n// => t1*b - t2*a\n// => b/t2 - a/t1 = d2/(h2/power) - d1(h1/power)", "memory": "175391" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#define ll long long\n#define X first\n#define Y second\nclass Solution {\npublic:\n static bool custom(const pair<int,int>& p1,const pair<int,int>& p2){\n if(p1.X!=p2.X){\n return p1.X*p1.Y<p2.X*p2.Y;\n }\n else{\n return p1.Y<p2.Y;\n }\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<ll> numTimes;\n int n = damage.size();\n for(auto it:health){\n numTimes.push_back((it+power-1)/power);\n }\n vector<pair<ll,ll>> pairss;\n vector<pair<long double,ll>> damagepersec(n);\n for(int i = 0;i<n;i++){\n damagepersec[i] = {1ll*(1.0*damage[i])/(1.0*numTimes[i]),i};\n }\n sort(damagepersec.rbegin(),damagepersec.rend());\n vector<ll> newDamage(n),newnum(n);\n for(int i = 0;i<n;i++){\n newDamage[i] = damage[damagepersec[i].Y];\n newnum[i] = numTimes[damagepersec[i].Y];\n }\n ll sum = accumulate(damage.begin(),damage.end(),0ll);\n ll ans = 0;\n for(int i = 0;i<damage.size();i++){\n ans += (newnum[i]) * sum;\n sum -= newDamage[i];\n }\n return ans;\n }\n};", "memory": "175391" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<double,pair<int,int>>p;\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int>t;\n int n = damage.size();\n\n for (int i=0; i<n; i++){\n t.push_back((health[i]+power-1)/power);\n }\n vector<double>r;\n\n for (int i=0; i<n; i++){\n r.push_back(static_cast<double>(t[i])/damage[i]);\n }\n long long sum = accumulate(damage.begin(),damage.end(),0);\n\n priority_queue<p,vector<p>,greater<p>>pq;\n\n for (int i=0;i<n; i++){\n pq.push({r[i],{damage[i],t[i]}});\n }\n long long ans = 0;\n while(!pq.empty()){\n auto x = pq.top(); pq.pop();\n int d1 = x.second.first;\n int t1 = x.second.second;\n\n ans += (sum*t1);\n sum -= d1;\n }\n return ans;\n }\n};", "memory": "176080" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Enemy{\npublic:\n long long damage;\n long long health;\n long long power;\n double ratio;\n\n Enemy(long long _damage, long long _health, long long _power) {\n damage = _damage;\n health = _health;\n power = _power;\n ratio = (double)damage / (double)health;\n }\n\n bool operator<(const Enemy &enemy) const & {\n\n long long enemyTime = enemy.health / enemy.power + (enemy.health % enemy.power != 0);\n long long thisTime = this->health / this->power + (this->health % this->power != 0);\n\n long long totalTime = enemyTime + thisTime;\n\n // Kill enemy before this\n long long enemyBefore = enemy.damage * enemyTime + this->damage * totalTime;\n // Kill this before enemy\n long long thisBefore = this->damage * thisTime + enemy.damage * totalTime;\n\n return enemyBefore < thisBefore;\n }\n};\n\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n long long totalSum = 0;\n\n priority_queue<Enemy> maxQ;\n\n for(int i=0; i<n; i++) {\n maxQ.push(Enemy(damage[i], health[i], power));\n totalSum += damage[i];\n }\n\n long long totalDamage = 0;\n\n while(!maxQ.empty()) {\n Enemy enemy = maxQ.top();\n maxQ.pop();\n\n // cout << enemy.damage << \" \" << enemy.health << endl;\n\n long long timeReq = enemy.health / power;\n if(enemy.health % power != 0) timeReq++;\n\n // cout << timeReq << \" \" << timeReq * totalSum << endl;\n\n totalDamage += timeReq * totalSum;\n\n totalSum -= enemy.damage;\n\n // cout << endl;\n }\n return totalDamage;\n }\n};\n\n\n/*\n1 2 3 4\n4 5 6 8\n1 2 2 2\n\n7 seconds total required\n*/", "memory": "176080" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int> rounds(n, 0);\n for (int i = 0; i < n; i++) {\n // Calculate the number of rounds needed to defeat each enemy\n rounds[i] = (health[i] + power - 1) / power;\n }\n\n // Vector to store pairs of {calc, {damage, rounds}}\n vector<pair<double, pair<int, int>>> v;\n for (int i = 0; i < n; i++) {\n double calc = (double)damage[i] / (double)rounds[i];\n v.push_back({calc, {damage[i], rounds[i]}});\n }\n\n // Sort in descending order based on calc\n sort(v.begin(), v.end(), greater<pair<double, pair<int, int>>>());\n\n // Separate sorted damage and rounds\n vector<long long int> newd(n, 0);\n vector<long long int> newr(n, 0);\n for (int i = 0; i < n; i++) {\n newd[i] = v[i].second.first; // Extract damage\n newr[i] = v[i].second.second; // Extract rounds\n }\n\n // Compute suffix sums\n vector<long long int> suffix(n, 0);\n suffix[n - 1] = newd[n - 1];\n for (int i = n - 2; i >= 0; i--) {\n suffix[i] = suffix[i + 1] + newd[i];\n }\n\n // Calculate the total damage\n long long int ans = 0;\n for (int i = 0; i < n; i++) {\n ans += (suffix[i]*newr[i]);\n }\n\n return ans;\n }\n};\n", "memory": "176769" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int i=0;\n static bool comp(vector<long long> &a, vector<long long> &b){\n int power=a[2];\n // cout<<\" comp \"<<a[0]<<\" \"<<b[0]<<endl;\n double s1 = ceil((float)a[1]/(float)power);\n double s2 = ceil((float)b[1]/(float)power);\n // cout<<\"s1 \"<<s1<<\" s2 \"<<s2<<endl;\n long long d1=s1*a[0] + b[0]*(s1 + s2);\n long long d2=s2*b[0] + a[0]*(s1 + s2);\n // cout<<d1<<\" \"<<d2<<endl;\n if(d1==d2){\n return a[0]<b[0];\n }\n return d1<d2;\n // cout<<i;\n // if(s1==s2){\n // return a[1]<b[1];\n // }else{\n // return s1>s2;\n // }\n }\n long long minDamage(int power, vector<int>& dam, vector<int>& hea) {\n long long n=dam.size();\n vector<vector<long long>> vec(n);\n for(int i=0;i<n;i++){\n vec[i] = {dam[i],hea[i], power};\n }\n \n sort(vec.begin(),vec.end(),comp);\n \n long long sum=0;\n for(int i=0;i<vec.size();i++){\n sum+=1ll*vec[i][0];\n // cout<<vec[i][0]<<\" \"<<vec[i][1]<<endl;\n }\n \n long long ans=0;\n for(int i=0;i<n;i++){\n if(vec[i][1]%power==0){\n ans+=sum*(vec[i][1]/power);\n }else{\n ans+=sum*(vec[i][1]/power + 1); \n }\n sum-=vec[i][0];\n }\n return ans;\n }\n};", "memory": "176769" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long sum=0;\n int n=damage.size();\n long long ans=0;\n // {damage per second,i}\n vector<pair<long double,int>> vp;\n for(int i=0;i<n;i++){\n sum+=damage[i];\n int time = ceil(health[i]/(double)power);\n long double dsp = 1LL*(damage[i]/(double)time);\n vp.push_back({dsp,i});\n }\n sort(vp.begin(),vp.end(),[&](auto a,auto b){\n return a.first>b.first;\n });\n for(int i=0;i<n;i++){\n int x= ceil(health[vp[i].second]/(double)power);\n ans+=sum*x;\n sum-=damage[vp[i].second];\n }\n return ans;\n }\n};", "memory": "177458" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "typedef long long ll;\n\nclass Solution {\npublic:\n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n \n int n = d.size();\n vector<pair<long double, int>> a;\n \n ll sum = 0;\n \n for(int i = 0; i<n; i++)\n {\n ll time = ceil((1.0 * h[i]) / (1.0 * p));\n long double x = (1.0 * d[i]) / time;\n a.push_back({x, i});\n sum += d[i];\n }\n \n sort(a.rbegin(), a.rend());\n ll ans = 0;\n \n for(auto i: a)\n {\n int id = i.second;\n ll sec = ceil((1.0 * h[id]) / (1.0 * p));\n ans += (sum * sec);\n sum -= d[id];\n }\n \n return ans;\n }\n};", "memory": "177458" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n \n vector<pair<double, int>> enemies;\n // 計算每個敵人的危險度(每秒傷害/擊敗時間)\n for (int i = 0; i < n; i++) {\n int time = (health[i] + power - 1) / power;\n enemies.push_back({(double)damage[i] / time, i});\n }\n // 根據危險度從大到小排序\n sort(enemies.rbegin(), enemies.rend());\n \n vector<pair<long long, int>> E;\n for (int i = 0; i < n; ++i) {\n // E.push_back({damage[i], health[i]});\n int idx = enemies[i].second;\n E.push_back({damage[idx], health[idx]});\n }\n // sort(E.begin(), E.end(), [](auto& a, auto& b) {\n // return a.first > b.first || (a.first == b.first && a.second < b.second);\n // });\n\n long long totalDamage = 0;\n long long currentTime = 0;\n \n for (auto& [d, h] : E) {\n long long seconds = (h + power - 1) / power; // 向上取整\n currentTime += seconds;\n \n totalDamage += d * currentTime;\n }\n \n return totalDamage;\n }\n};", "memory": "178146" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long ans=0;\n int n = damage.size();\n long long sum =0;\n for(int i=0; i <n; i++){\n sum += damage[i];\n } \n vector<pair<double,int>> vp;\n for(int i =0; i <n; i++){\n \n double time = (health[i]+power -1)/power;\n // cout<<time<<\" \";\n double ratio = (double)damage[i]/time;\n vp.push_back({ratio,i});\n }\n sort(vp.begin(), vp.end());\n reverse(vp.begin(), vp.end());\n vector<pair<double,pair<int,int>>> v;\n for(int i =0; i <vp.size(); i++){\n int ind = vp[i].second;\n v.push_back({vp[i].first,{damage[ind], ind}});\n }\n sort(v.begin(), v.end(), greater<pair<double, pair<int, int>>>());\n // for(auto it: v){\n // cout<<it.first<<\" \"<<it.second.first<<\" \"<<it.second.second<<endl;\n // }\n for(auto it: v){\n ans += sum*((health[it.second.second]+power-1)/power);\n sum -= it.second.first;\n // cout<<ans<<\" \";\n }\n return ans;\n }\n};", "memory": "178146" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, std::vector<int>& damage, std::vector<int>& health) {\n std::vector<int> time;\n int n = damage.size();\n \n for (int i = 0; i < n; i++) {\n time.push_back(ceil(health[i] * 1.0 / power));\n }\n std::multimap<double, int> final_val;\n for (int i = 0; i < n; i++) {\n double ration = time[i] * 1.0 / damage[i];\n final_val.insert(std::make_pair(ration, i));\n\n }\n\n long long int total_damage = 0;\n int cumulative_time = 0;\n\n for (auto it = final_val.begin(); it != final_val.end(); ++it) {\n int index = it->second;\n cumulative_time += time[index]; \n total_damage += cumulative_time * damage[index]; \n }\n\n return total_damage;\n }\n};", "memory": "178835" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int> samay;\n for(int i=0;i<n;i++){\n samay.push_back(ceil(health[i]*1.0/power));\n }\n multimap<double,int> destination;\n for(int i=0;i<n;i++){\n double ans = samay[i]*1.0/damage[i];\n destination.insert(make_pair(ans,i));\n }\n long long int answer = 0;\n int total_samay = 0;\n for(auto it = destination.begin();it!=destination.end();it++){\n int idx = it->second;\n total_samay += samay[idx];\n answer += total_samay*damage[idx];\n }\n return answer;\n }\n};", "memory": "178835" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n #define ll long long\n ll p;\n static bool cmp(const pair<pair<ll,ll>,pair<ll,ll>> &a,const pair<pair<ll,ll>,pair<ll,ll>> &b){\n ll sum = a.first.first + b.first.first;\n ll p1 = a.first.second;\n ll t1 = (a.second.first+p1-1)/p1;\n ll t2 = (b.second.first+p1-1)/p1;\n return t1*sum + t2*(sum-a.first.first) < t2*sum + t1*(sum-b.first.first);\n \n }\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n p = power;\n vector<pair<pair<ll,ll>,pair<ll,ll>>> v;\n ll sum = accumulate(damage.begin(),damage.end(),0ll);\n for(ll i=0;i<n;i++){\n ll x = (health[i]+power-1)/power;\n v.push_back({{damage[i]*1ll,p},{health[i]*1ll,i}});\n }\n sort(v.begin(),v.end(),cmp);\n long long ans = 0;\n for(ll i=0;i<n;i++){\n ll t = (v[i].second.first+p-1)/p;\n ans+=(t*sum);\n sum-=v[i].first.first;\n }\n return ans;\n }\n};", "memory": "179524" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <algorithm>\n#include <set>\n\nusing namespace std;\n\nclass DSU {\nprivate:\n vector<int> parent;\n vector<int> size;\n\npublic:\n DSU(int n) {\n parent.resize(n);\n size.resize(n, 1); \n for (int i = 0; i < n; ++i) {\n parent[i] = i; \n }\n }\n\n int find(int x) {\n if (x != parent[x]) {\n parent[x] = find(parent[x]); \n }\n return parent[x];\n }\n\n void union_size(int x, int y) {\n int rootX = find(x);\n int rootY = find(y);\n\n if (rootX != rootY) {\n if (size[rootX] < size[rootY]) {\n parent[rootX] = rootY;\n size[rootY] += size[rootX];\n } else {\n parent[rootY] = rootX;\n size[rootX] += size[rootY];\n }\n }\n }\n};\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n int maxVal = *max_element(damage.begin(), damage.end());\n maxVal = max(maxVal, *max_element(health.begin(), health.end()));\n DSU ds(maxVal + 1); // Initialize DSU based on the max value in damage/health\n \n vector<pair<int, int>> dushman;\n multiset<int> lai;\n \n for (int i = 0; i < n; ++i) {\n dushman.push_back({damage[i], health[i]});\n int ulp_u = ds.find(damage[i]);\n int ulp_v = ds.find(health[i]);\n \n if (ulp_u != ulp_v) {\n ds.union_size(damage[i], health[i]);\n lai.insert(damage[i]);\n lai.insert(health[i]);\n }\n }\n \n sort(dushman.begin(), dushman.end(), [&](const pair<int, int>& a, const pair<int, int>& b) {\n long long pehla = static_cast<long long>(a.first) * ((b.second + power - 1) / power);\n long long dusra = static_cast<long long>(b.first) * ((a.second + power - 1) / power);\n return dusra < pehla;\n });\n \n long long ans = 0;\n long long injury_curr = 0;\n \n for (const auto& ek_dushman : dushman) {\n if (lai.find(ek_dushman.first) != lai.end())\n lai.erase(lai.find(ek_dushman.first));\n if (lai.find(ek_dushman.second) != lai.end())\n lai.erase(lai.find(ek_dushman.second));\n injury_curr += ek_dushman.first;\n }\n \n for (const auto& ek_dushman : dushman) {\n int hits = (ek_dushman.second + power - 1) / power; \n ans += hits * injury_curr; \n injury_curr -= ek_dushman.first; \n // Removed ds.union_size(ans, injury_curr) as it's incorrect here\n }\n \n return ans;\n }\n};\n", "memory": "179524" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n typedef long long ll;\n typedef long double ld;\n\n static bool comp(const pair<ll, ll>& p1, const pair<ll, ll>& p2) {\n if (p1.first != p2.first)\n return p1.first > p2.first; // Sort by damage descending\n return p1.second < p2.second; // If damage is the same, sort by health ascending\n }\n\n long long minDamage(int pw, vector<int>&dg, vector<int>&ht) {\n\n ll sum = 0;\n\n vector<pair<ld, pair<ll, ll>>>v;\n\n for(int i=0; i<ht.size(); i++){\n \n if(ht[i]%pw == 0){\n ht[i] /= pw;\n }else{\n ht[i] = ht[i]/pw+1;\n }\n \n sum += dg[i];\n\n ld x = dg[i];\n ld y = ht[i];\n ld z = x/y;\n\n v.push_back({z, {dg[i], ht[i]}});\n }\n\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n\n\n\n ll x = 0;\n ll ans = 0;\n\n for(int i=0; i<v.size(); i++){\n ans += sum*v[i].second.second;\n sum -= v[i].second.first;\n }\n\n return ans;\n\n }\n};", "memory": "180213" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n //total damage from enemy[i] = tAlive * damage[i]\n //time to kill enemey[i] = power / health[i]\n //damage = sum(tAlive * damage[i]) -> (power/health[i]) * damage[i]\n //should always kill an enemy fully \n \n\n vector<pair<long double, int>> enemies;\n long long totalDamage = 0;\n for(int i = 0; i < damage.size(); i++){\n enemies.push_back({(double)damage[i] / ceil((double)health[i] / power), i});\n totalDamage += damage[i];\n }\n sort(enemies.begin(), enemies.end(), [](pair<long double, int> v1, pair<long double, int> v2){\n return v1.first > v2.first;\n });\n \n long long out = 0;\n for(int i = 0; i < enemies.size(); i++){\n out += ceil((double)health[enemies[i].second] / power) * totalDamage;\n totalDamage -= damage[enemies[i].second];\n }\n return out;\n }\n};", "memory": "180213" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "#define ll long long\n#define fi first \n#define se second\n#define ld long double\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long p = power, n = health.size();\n vector<ll> t(n);\n for(int i=0; i<n; ++i) t[i] = (health[i]+power-1)/power;\n priority_queue<pair<ld, ll>> qu;\n for(int i=0; i<n; ++i) qu.push({ ((ld)(damage[i]))/((ld)(t[i])), i});\n ll mn=0, tot=0;\n for(int i=0; i<n; ++i) tot+= (ll)damage[i];\n pair<ld, ll> tp;\n ll mul;\n while(qu.size()){\n tp = qu.top();\n mn += tot*t[tp.se];\n tot -= (ll)damage[tp.se];\n qu.pop();\n }\n return mn;\n \n }\n};", "memory": "180901" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "using ll = long long;\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vb = vector<bool>;\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\n\n#define fi first\n#define se second\n#define pb push_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\nclass Solution {\npublic:\n ll minDamage(int power, vi& dmg, vi& hlt) {\n int n = dmg.size();\n\n ll answer = 0;\n ll tot_dmg = 0;\n ll tot_dt = 0;\n vll dt(n);\n for (int i = 0; i < n; i++)\n {\n tot_dmg += dmg[i];\n dt[i] = (hlt[i] + power - 1) / power;\n tot_dt += dt[i];\n }\n\n // priority_queue<pll> pq;\n // for (int i = 0; i < n; i++)\n // pq.push({(tot_dt - dt[i]) * dmg[i], i});\n priority_queue<pair<long double, int>> pq;\n for (int i = 0; i < n; i++)\n pq.push({(long double)dmg[i] / dt[i], i});\n\n while (!pq.empty())\n {\n auto [x, i] = pq.top();\n // cout << \"[\" << i << \"]\\n\";\n pq.pop();\n\n answer += tot_dmg * dt[i];\n tot_dmg -= dmg[i];\n }\n\n return answer;\n }\n};", "memory": "180901" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n // vector<pair<int,int>> temp;\n // for(int i=0;i<damage.size();i++){\n // temp.push_back(make_pair(damage[i],health[i]));\n // }\n map<double,vector<int>> mp;\n vector<double> impact;\n int sum=0;\n double power2=power;\n for(int i=0;i<damage.size();i++){\n double f=damage[i];\n sum+=damage[i];\n double x=damage[i]/ceil(health[i]/power2);\n // cout<<x<<\" \";\n if(mp.find(x)==mp.end()) impact.push_back(x);\n mp[x].push_back(damage[i]);\n }\n \n long long answer=0;\n sort(impact.begin(),impact.end(),greater<double>());\n for(auto&i:impact){\n cout<<i<<\" \";\n for(auto& j:mp[i]){\n cout<<i<<\" \"<<j<<\" \"<<sum<<endl;\n answer+=(j/i)*sum;\n sum-=j;\n \n }\n }\n return answer;\n }\n};", "memory": "181590" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<long long> time(n);\n vector<pair<long double, int>> v;\n\n for(int i = 0; i < n; i++)\n {\n time[i] = 1LL*ceil((1.0*health[i])/(1.0*power));\n long double temp = 1LL*(1.0*damage[i])/(1.0*time[i]);\n v.push_back({temp, i});\n }\n long long sum = 0, ans = 0;\n\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n\n for(int i = 0; i < n; i++)\n {\n sum += (1LL*damage[i]);\n }\n //cout << endl;\n\n for(int i = 0; i < n; i++)\n {\n // cout << ans << \" \";\n ans += 1LL*sum*(time[v[i].second]);\n // cout << ans << \" \";\n // cout << sum << \" \";\n sum -= damage[v[i].second];\n // cout << sum << endl;\n }\n return ans;\n }\n};", "memory": "181590" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nlong long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size(); // Number of enemies\n int total_damage = 0; // Total damage taken by Bob\n\n // Create a vector of pairs (damage, health)\n vector<long long> time(n);\n for(int i=0;i<n;i++)\n {\n time[i]=(health[i] + power - 1) / power;\n }\n vector<pair<long double, int>> enemies;\n for (int i = 0; i < n; ++i) {\n enemies.push_back({(double)damage[i]/time[i], i});\n }\n long long sum=0,ans=0;\n \n sort(enemies.rbegin(), enemies.rend()); \n\n \n for(int i=0;i<n;i++)\n {\n sum+=damage[enemies[i].second];\n }\n for(int i=0;i<n;i++)\n {\n ans=ans+sum*time[enemies[i].second];\n sum=sum-damage[enemies[i].second];\n }\n return ans;\n}\n};", "memory": "182279" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<int,int>>pq;\n int n=damage.size();\n vector<pair<int,int>>vp(n);\n long long sum=0;\n long long tot=0;\n vector<pair<long double,pair<int,int>>>vp2;\n for(int i=0;i<n;i++){\n int d=damage[i];\n sum+=d;\n int num=(health[i]+power-1)/power;\n vp[i]={d,num};\n long double f=(long double)d/(long double)num;\n vp2.push_back({f,{-num,i}});\n tot+=num;\n }\n long long ans=0;\n sort(vp2.rbegin(),vp2.rend());\n for(int i=0;i<n;i++){\n int ind=vp2[i].second.second;\n cout<<ind<<\" \";\n ans+=((long long)sum*((long long)vp[ind].second));\n sum-=vp[ind].first;\n }\n return ans;\n }\n};", "memory": "182279" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n \n long long ans = 0;\n\n vector<pair<long double, int>> dps;\n vector<int> time;\n for(int i = 0; i<n; i++){\n int t = (health[i]-1)/power + 1;\n dps.push_back({(double)damage[i]/t, i});\n time.push_back(t);\n }\n\n sort(dps.begin(), dps.end(), greater<>());\n\n long long total = 0;\n for(int i = 0; i<n; i++){\n total += damage[i];\n }\n\n for(int i = 0; i<n; i++){\n ans += 1ll*total*time[dps[i].second];\n total -= damage[dps[i].second];\n }\n\n return ans;\n }\n};", "memory": "182968" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\n // bool comparefn(pair<int,int>& a,pair<int,int>& b){\n // if(a.first==b.first) return (b.second>a.second);\n\n // return (a.first > b.first);\n // }\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long total = accumulate(damage.begin(),damage.end(),0);\n int n = damage.size();\n vector<pair<long double,int>> vp;\n vector<int> time(n);\n for(int i=0;i<n;i++){\n time[i] = ceil((float)health[i]/power);\n long double dps = 1LL*((1.0*damage[i])/time[i]);\n vp.push_back({dps,i});\n }\n\n sort(vp.rbegin(),vp.rend());\n long long ans = 0;\n for(auto it:vp){\n int idx = it.second;\n int dam = damage[idx];\n long long temp = time[idx]*total;\n ans+=temp;\n total-=dam;\n\n }\n\n return ans;\n }\n};", "memory": "182968" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n \n map<double, vector<int>> mp;\n int n = damage.size();\n int total = 0;\n \n for(int i=0; i<n; i++){\n int times = (health[i] + power-1)/power;\n double ratio = (damage[i]*1.0)/times;\n mp[ratio].push_back(i);\n // cout<<ratio<<\" \"<<i<<endl;\n total+=damage[i];\n }\n \n long long D =0;\n \n while(mp.size() > 0){\n \n auto it = mp.end();\n it--;\n vector<int> vec = it->second;\n // cout<<it->first<<\" \"<<vec.size()<<endl;\n mp.erase(it);\n \n for(int i=0; i<vec.size(); i++){\n \n int times= (health[vec[i]] + power-1)/power;\n D+=times*total;\n total-=damage[vec[i]];\n \n // cout<<D<<\" \"<<vec[i]<<endl;\n }\n }\n \n return D;\n }\n};", "memory": "183656" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> cycles;\n long long totc = 0;\n for (int h : health) {\n int c = h/power;\n if (h%power != 0) c++;\n cycles.push_back(c);\n totc += c;\n }\n long long totd = 0;\n for (int d : damage) {\n totd += d;\n }\n map<double, vector<long long>> monsters;\n size_t n = damage.size();\n assert(n == health.size());\n for (int i = 0; i < n; i++) {\n double dpc = static_cast<double>(damage[i])/static_cast<double>(cycles[i]);\n if (monsters.count(dpc) == 0) monsters[dpc] = vector<long long>();\n long long mval = (static_cast<long long>(cycles[i]) << 32) | \n static_cast<long long>(damage[i]);\n monsters[dpc].push_back(mval);\n }\n\n long long res = 0;\n long long curd = totd;\n for (auto itr = monsters.rbegin(); itr != monsters.rend(); itr++) {\n for (long long mval : itr->second) {\n long long dmg = mval & 0x0FFFFFFFFLL;\n long long cyc = mval >> 32;\n res += curd*cyc;\n curd -= dmg;\n }\n }\n assert(curd == 0);\n return res;\n }\n};", "memory": "183656" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) \n {\n vector<int>he;\n for(auto x:health)\n {\n if(x%power==0)\n he.push_back(x/power);\n else\n he.push_back((x/power)+1);\n } \n \n int fin=0;\n map< double,vector<pair<int,int>>>mp;\n for(int i=0;i<damage.size();i++)\n {\n mp[(1.0*he[i])/damage[i]].push_back({damage[i],i});\n fin+=damage[i];\n \n }\n \n \n auto it=mp.begin();\n long long ans=0;\n while(it!=mp.end())\n {cout<<(*it).first<<endl;\n vector<pair<int,int>>&temp=(*it).second;\n sort(temp.rbegin(),temp.rend());\n for(int i=0;i<temp.size();i++)\n {\n ans+=(fin*he[temp[i].second]);\n cout<<fin<<endl;\n cout<<temp[i].second<<endl;\n cout<<ans<<endl;\n fin-=damage[temp[i].second];\n }\n ++it;\n }\n return ans;\n }\n};", "memory": "184345" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n static bool comp(vector<long double> &a, vector<long double> &b){\n if(a[0]==b[0]){\n if(a[1]==b[1]) return a[2]>b[2];\n return a[1]<b[1];\n }\n return a[0]<b[0];\n }\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n \n int n=damage.size();\n \n long long ans=0, sum=0, cnt=0;\n \n vector<vector<long double>> help(n);\n for(int i=0;i<n;i++){\n sum+=damage[i];\n int t=0;\n if(health[i]%power!=0) t=1;\n health[i]/=power;\n health[i]+=t;\n help[i]={(long double)damage[i]/health[i],(long double)damage[i],(long double)health[i]};\n cnt+=health[i];\n }\n sort(help.begin(),help.end(),comp);\n ans=sum*cnt;\n \n long long sub=0;\n cnt=0;\n for(int i=0;i<n;i++){\n // cout<<help[i][1]<<\" \"<<help[i][2]<<\"\\n\";\n sub+=cnt*help[i][1];\n cnt+=help[i][2];\n }\n // cout<<ans<<\" \"<<sub<<\"\\n\";\n return ans-sub;\n \n }\n};", "memory": "184345" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n long long totalDamage = 0;\n long long ans = 0;\n priority_queue<pair<long double, int>> pq;\n vector<long double> times(n);\n for (int i = 0; i < n; i++) {\n long double time = ceil((long double)health[i] / power);\n times[i] = time;\n pq.push({(long double)damage[i] / time, i});\n }\n\n long long accumulatedTime = 0;\n\n while (!pq.empty()) {\n int idx = pq.top().second;\n pq.pop();\n\n long double time = times[idx];\n accumulatedTime += time;\n\n ans += accumulatedTime * damage[idx];\n }\n\n return ans;\n }\n};\n", "memory": "185034" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "\n bool sortbysec(const vector<int> &a,\n const vector<int> &b)\n {\n return (a[0]*a[1] + b[0]*(a[1] + b[1]) < a[0]*(a[1] + b[1]) + b[0] *b[1]);\n }\nclass Solution {\n \npublic:\n\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<vector<int>> v;\n for(int i=0;i<damage.size();i++){\n int time = health[i]/power;\n if(health[i]%power != 0){\n time++;\n }\n v.push_back({damage[i],time});\n }\n sort(v.begin(),v.end(),sortbysec);\n int time = 0;\n long long int dmg = 0;\n for(int i=0;i<v.size();i++){\n time+=v[i][1];\n dmg+=time*v[i][0];\n }\n return dmg;\n }\n};", "memory": "185034" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "using namespace std;\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<long double, int>> v;\n vector<int> t;\n int n = damage.size();\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n int temp;\n if (health[i] % power == 0) {\n temp = health[i] / power;\n t.push_back(temp);\n }\n else {\n temp = health[i] / power + 1;\n t.push_back(temp);\n }\n long double dps = (1.0*damage[i]) / (1.0*temp);\n v.push_back(make_pair(dps, i));\n }\n sort(v.rbegin(), v.rend());\n long long s = 0;\n for (auto i: damage) {\n s += i;\n }\n for (int i = 0; i < n; i++) {\n ans += s * (t[v[i].second]);\n s -= damage[v[i].second];\n }\n return ans;\n }\n};", "memory": "185723" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "using namespace std;\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<long double, int>> v;\n vector<int> t;\n int n = damage.size();\n long long ans = 0;\n for (int i = 0; i < n; i++) {\n int temp;\n if (health[i] % power == 0) {\n temp = health[i] / power;\n t.push_back(temp);\n }\n else {\n temp = health[i] / power + 1;\n t.push_back(temp);\n }\n long double dps = (1.0*damage[i]) / (1.0*temp);\n v.push_back(make_pair(dps, i));\n }\n sort(v.rbegin(), v.rend());\n long long s = 0;\n for (auto i: damage) {\n s += i;\n }\n for (int i = 0; i < n; i++) {\n ans += s * (t[v[i].second]);\n s -= damage[v[i].second];\n }\n return ans;\n }\n};", "memory": "186411" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool comp(vector<int> &a, vector<int> &b){\n return (double)a[1]/((a[2]+a[3]-1)/a[3]) < (double)b[1]/((b[2]+b[3]-1)/b[3]);\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n vector<vector<int>> v;\n for(int i=0;i<n;i++){\n v.push_back({i,damage[i],health[i],power});\n }\n sort(v.begin(),v.end(),comp);\n long long sum=0;\n for(int i=0;i<n;i++) sum+=damage[i];\n long long ans=0;\n for(int i=n-1;i>=0;i--){\n ans+=sum*((v[i][2] + power - 1)/power);\n sum-=v[i][1];\n }\n return ans;\n }\n};", "memory": "186411" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n vector<vector<int>> v;\n for (int i=0; i<n; ++i) {\n v.push_back({damage[i],health[i]});\n } \n sort(v.begin(),v.end(),[&](auto& a,auto& b){\n long long x=(long long)a[0]*((b[1]+power-1)/power);\n long long y=(long long)b[0]*((a[1]+power-1)/power);\n return x>y;\n });\n long long totDam=0;\n long long curDam=0;\n for (auto& e:v) {\n curDam+=e[0];\n }\n for (auto& e:v) {\n int need=(e[1]+power-1)/power;\n totDam+=need*curDam;\n curDam-=e[0];\n }\n return totDam;\n }\n};", "memory": "187100" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<double, pair<long long, long long>>, vector<pair<double, pair<long long, long long>>>, greater<pair<double, pair<long long, long long>>>> pq;\n vector<long long> noOfShots;\n long long totalDamage = 0;\n long long ans = 0;\n for(int i=0;i<damage.size();i++) {\n noOfShots.push_back(ceil((double)health[i] / (double)power));\n // cout << ceil((double)health[i] / (double)power);\n totalDamage += damage[i];\n }\n vector<double> weak;\n // cout << endl;\n for(int i=0;i<damage.size();i++) {\n weak.push_back((double)noOfShots[i] / (double)damage[i]);\n // cout << (double)noOfShots[i] / (double)damage[i];\n }\n \n for(int i=0;i<damage.size();i++) {\n pq.push({weak[i], {noOfShots[i], damage[i]}});\n }\n \n while(!pq.empty()) {\n auto temp = pq.top();\n pq.pop();\n // cout << temp.first << \" \" << temp.second.first << \" \" << temp.second.second << endl;\n ans += temp.second.first * totalDamage;\n totalDamage -= temp.second.second;\n }\n \n return ans;\n }\n};", "memory": "187100" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& d, vector<int>& h) \n {\n int n = d.size();\n vector<long long> time;\n vector<pair<long double, int>> DPS;\n long long sum = 0, ans = 0;\n\n for(int i=0; i<n; ++i)\n {\n time.push_back(1ll*ceil((double)h[i]/power));\n long double dps = (long double)(d[i])/time[i];\n DPS.push_back({dps, i});\n sum += d[i];\n } \n\n sort(DPS.begin(), DPS.end(), greater<pair<long double, int>>());\n\n for(int i=0; i<n; ++i)\n {\n ans += sum*(time[DPS[i].second]);\n sum -= d[DPS[i].second];\n }\n\n return ans;\n }\n};", "memory": "187789" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long n = health.size();\n vector<long long> time;\n vector<pair<long double, int>> arr;\n for (int i = 0; i < n; i++) {\n time.push_back(1LL * ceil((1.0 * health[i]) / (1.0 * power)));\n long double dps = 1LL * (1.0 * damage[i]) / (1.0 * time[i]);\n arr.push_back({dps, i});\n }\n sort(arr.begin(), arr.end());\n reverse(arr.begin(), arr.end());\n long long ans = 0, sum = 0;\n for (int i = 0; i < n; i++) \n {\n sum += (1LL * damage[arr[i].second]);\n }\n for (int i = 0; i < n; i++) \n {\n ans += 1LL * sum * (time[arr[i].second]);\n sum -= damage[arr[i].second];\n }\n return ans;\n }\n};", "memory": "187789" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long n = health.size();\n vector<long long> time;\n vector<pair<long double,int>> arr; \n \n for(int i=0;i<n;i++){\n time.push_back(1LL*ceil((1.0*health[i])/(1.0*power)));\n long double dps = 1LL*(1.0*damage[i])/(1.0*time[i]);\n arr.push_back({dps,i});\n }\n \n sort(arr.rbegin(),arr.rend());\n long long ans=0,sum=0;\n for(int i=0;i<n;i++){\n sum += (1LL*damage[arr[i].second]);\n }\n for(int i=0;i<n;i++){\n ans += 1LL*sum*(time[arr[i].second]);\n sum -= damage[arr[i].second];\n }\n return ans;\n }\n};", "memory": "188478" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long n = health.size();\n vector<long long> time;\n vector<pair<long double,int>> arr; \n \n for(int i=0;i<n;i++){\n time.push_back(1LL*ceil((1.0*health[i])/(1.0*power)));\n long double dps = 1LL*(1.0*damage[i])/(1.0*time[i]);\n arr.push_back({dps,i});\n }\n \n sort(arr.rbegin(),arr.rend());\n long long ans=0,sum=0;\n for(int i=0;i<n;i++){\n sum += (1LL*damage[arr[i].second]);\n }\n for(int i=0;i<n;i++){\n ans += 1LL*sum*(time[arr[i].second]);\n sum -= damage[arr[i].second];\n }\n return ans;\n }\n};", "memory": "188478" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n map<double,vector<pair<int,int>>> mp;\n int n = health.size();\n int sum = 0;\n for(int i=0;i<n;i++){\n health[i] = (health[i] + power - 1) / power;\n\n mp[double(damage[i])/double(health[i])].push_back({health[i],damage[i]});\n sum += damage[i];\n }\n long long ans = 0;\n for(auto it = mp.rbegin();it != mp.rend(); it++){\n vector<pair<int,int>> v = (*it).second;\n sort(v.begin(),v.end());\n for(int i=0;i<v.size();i++){\n cout << v[i].first << \" \" << v[i].second << endl;\n ans += sum * v[i].first;\n sum -= v[i].second;\n \n }\n // sum -= (*it).first * v.size();\n }\n return ans;\n }\n};", "memory": "189166" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n map<double,vector<pair<int,int>>> mp;\n int n = health.size();\n int sum = 0;\n for(int i=0;i<n;i++){\n health[i] = (health[i] + power - 1) / power;\n\n mp[double(damage[i])/double(health[i])].push_back({health[i],damage[i]});\n sum += damage[i];\n }\n long long ans = 0;\n for(auto it = mp.rbegin();it != mp.rend(); it++){\n vector<pair<int,int>> v = (*it).second;\n // sort(v.begin(),v.end());\n for(int i=0;i<v.size();i++){\n cout << v[i].first << \" \" << v[i].second << endl;\n ans += sum * v[i].first;\n sum -= v[i].second;\n \n }\n // sum -= (*it).first * v.size();\n }\n return ans;\n }\n};", "memory": "189166" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "typedef long long int ll;\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int> timeToDie(n, 0);\n\n for (int i = 0; i < n; i++) {\n timeToDie[i] = ceil((double)health[i] / (double)power);\n }\n\n vector<vector<double> > damageToDieTimeRatio;\n for (int i = 0; i < n; i++) {\n damageToDieTimeRatio.push_back({(double)damage[i] / (double)timeToDie[i], (double)i});\n }\n\n sort(damageToDieTimeRatio.begin(), damageToDieTimeRatio.end());\n ll totalDamage = 0, ctrDays = 0;\n for (int i = n - 1; i >= 0; i--) {\n int ind = (int)damageToDieTimeRatio[i][1];\n ctrDays += timeToDie[ind];\n totalDamage += ctrDays * (ll)damage[ind];\n }\n\n return totalDamage;\n }\n};", "memory": "189855" }
3,531
<p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p> <p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] &gt; 0</code>).</p> <p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p> <p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">39</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li> <li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li> <li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li> <li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li> <li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li> <li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p> <p><strong>Output:</strong> <span class="example-io">320</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= n == damage.length == health.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= damage[i], health[i] &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<long long>timeReq(n);\n vector<long double>damagePerSecond(n);\n long long sum = 0;\n for(int i=0;i<n;i++){\n sum+=damage[i];\n }\n priority_queue<pair<long double,int>>pq;\n for(int i=0;i<n;i++){\n timeReq[i] = ceil(float(health[i])/float(power));\n damagePerSecond[i] = float(damage[i])/float(timeReq[i]);\n pq.push({damagePerSecond[i],i});\n }\n long long ans = 0;\n while(!pq.empty()){\n int index = pq.top().second;\n pq.pop();\n // cout<<index<<\" \"<<\" \"<<points<<endl;\n // cout<<index<<\" \"<<sum*(timeReq[index])<<endl;\n ans += sum*(timeReq[index]);\n sum -= damage[index];\n }\n\n return ans;\n \n }\n};", "memory": "189855" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string &a, string &b) {\n return (a[0] + a[1]) % 2 == (b[0] + b[1]) % 2;\n }\n};", "memory": "7500" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool also_check(int a, int b) {\n if ( a % 2 == 0) {\n return b % 2 == 0;\n }\n else {\n return b % 2 == 1;\n }\n }\n\n bool check(char c1, char c2, char d1, char d2) {\n int i1 = c1 - 'a';\n int i2 = c2 - '1';\n int j1 = d1 - 'a';\n int j2 = d2 - '1';\n return also_check(i1, i2) == also_check(j1, j2);\n }\n \n bool checkTwoChessboards(string co1, string co2) {\n bool res = check(co1[0], co1[1], co2[0], co2[1]);\n return res;\n } \n\n};", "memory": "7600" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int a=coordinate1[0]-'a';\n int b=coordinate1[1]-'1';\n int c=coordinate2[0]-'a';\n int d=coordinate2[1]-'1';\n return (a+b)%2 == (c+d)%2 ;\n }\n};", "memory": "7700" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string c1, string c2) {\n int a = c1[0] - 'a', b = c1[1] - '1';\n int c = c2[0] - 'a', d = c2[1] - '1';\n return !(((a+b) & 1) ^ ((c + d) & 1));\n }\n};", "memory": "7700" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int res1 = coordinate1[0] + coordinate1[1];\n int res2 = coordinate2[0] + coordinate2[1]; \n return res1 % 2 == res2 % 2; \n }\n};", "memory": "7800" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n \n cout << 'c'- 0 - 96;\n int v1 = coordinate1[0] - 0 - 96;\n int v2 = coordinate2[0] - 0 - 96;\n int ans1 = v1 + (coordinate1[1] - '0');\n int ans2 = v2 + (coordinate2[1] - '0');\n\n if(ans1 %2 == 0 && ans2%2 == 0) return true;\n if(ans1 %2 != 0 && ans2%2 != 0) return true;\n return false;\n }\n};", "memory": "7800" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int c1=(coordinate1[0]-'a')+(coordinate1[1]-'0');\n int c2=(coordinate2[0]-'a')+(coordinate2[1]-'0');\n if((c1+c2)%2){\n return false;\n }\n return true;\n }\n};", "memory": "7900" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int col1 = coordinate1[0] - 'a'; // calculate the column index for coordinate1\n int row1 = coordinate1[1] - '1'; // calculate the row index for coordinate1\n int col2 = coordinate2[0] - 'a'; // calculate the column index for coordinate2\n int row2 = coordinate2[1] - '1'; // calculate the row index for coordinate2\n return (col1 + row1) % 2 == (col2 + row2) % 2; // check if both squares have the same colour\n }\n};", "memory": "7900" }
3,553
<p>You are given two strings, <code>coordinate1</code> and <code>coordinate2</code>, representing the coordinates of a square on an <code>8 x 8</code> chessboard.</p> <p>Below is the chessboard for reference.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png" style="width: 400px; height: 396px;" /></p> <p>Return <code>true</code> if these two squares have the same color and <code>false</code> otherwise.</p> <p>The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;c3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>Both squares are black.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coordinate1 = &quot;a1&quot;, coordinate2 = &quot;h3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>Square <code>&quot;a1&quot;</code> is black and <code>&quot;h3&quot;</code> is white.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>coordinate1.length == coordinate2.length == 2</code></li> <li><code>&#39;a&#39; &lt;= coordinate1[0], coordinate2[0] &lt;= &#39;h&#39;</code></li> <li><code>&#39;1&#39; &lt;= coordinate1[1], coordinate2[1] &lt;= &#39;8&#39;</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkTwoChessboards(string coordinate1, string coordinate2) {\n int res1 = coordinate1[0] + coordinate1[1];\n int res2 = coordinate2[0] + coordinate2[1]; \n return res1 % 2 == res2 % 2; \n }\n};", "memory": "8000" }