Skip to content

Commit 15bc70d

Browse files
committed
Time exceed example and some comments
1 parent c99bf8f commit 15bc70d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Cpp/minTimetoMakeRope.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
#include <iostream>
22
#include <algorithm>
33
#include <vector>
4+
#include <limits.h>
5+
6+
// 1578. Minimum Time to Make Rope Colorful
7+
8+
// Find accumulation of array with subracting max element
9+
// compare current array and maxElement then add the min one into variable
10+
// compare current and maxElement then keep maxElement
11+
// repeat process
12+
13+
// int maxElement = 0, result = 0;
14+
// for(int i=0; i<vec.size(); ++i)
15+
// {
16+
// res += std::min(maxElement, vec[i]);
17+
// maxElement = std::max(maxElement, vec[i]);
18+
// }
19+
// return res;
20+
421

522
class Solution
623
{
@@ -19,6 +36,41 @@ class Solution
1936
currMax = std::max(currMax, neededTime[i]);
2037
}
2138

39+
return res;;
40+
}
41+
42+
// stupid solution
43+
// try to use "Minimnum movement for 3 identical consecutive letters"
44+
// find consecutive elements then process these elements one by one]
45+
// this will give time exceed because while loop inside for loop
46+
int minCostTimeExceed(std::string colors, std::vector<int>& neededTime) {
47+
unsigned int res = 0;
48+
49+
auto size = colors.size();
50+
51+
for(int i = 0; i < size;)
52+
{
53+
int j = i + 1;
54+
bool flag =false;
55+
while(j < size && (colors[j] == colors[i]))
56+
{
57+
j++;
58+
flag = true;
59+
}
60+
61+
int k = i;
62+
int l = j -1;
63+
while(flag == true && k<l)
64+
{
65+
auto it = std::min_element(std::begin(neededTime) + i, std::begin(neededTime) + j);
66+
int indexSmallestEl = std::distance(std::begin(neededTime), it);
67+
res = res + neededTime[indexSmallestEl];
68+
neededTime[indexSmallestEl] = INT_MAX;
69+
k++;
70+
}
71+
72+
i = j;
73+
}
2274
return res;
2375
}
2476
};

0 commit comments

Comments
 (0)