1
1
#include < iostream>
2
2
#include < algorithm>
3
3
#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
+
4
21
5
22
class Solution
6
23
{
@@ -19,6 +36,41 @@ class Solution
19
36
currMax = std::max (currMax, neededTime[i]);
20
37
}
21
38
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
+ }
22
74
return res;
23
75
}
24
76
};
0 commit comments