Skip to content

Commit d71890e

Browse files
committed
test: 1578 solution
py, c++, go, java
1 parent 5ac5a5d commit d71890e

File tree

6 files changed

+63
-24
lines changed

6 files changed

+63
-24
lines changed
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
33

4-
54
using namespace std;
65
using json = nlohmann::json;
76

87
class Solution {
98
public:
10-
int minCost(string colors, vector<int>& neededTime) {
11-
9+
int minCost(const string &colors, const vector<int> &neededTime) {
10+
int ans = 0, n = colors.length();
11+
for (int i = 0, j = 0; i < n - 1; i = j) {
12+
for (j = i + 1; j < n && colors[j] == colors[i]; ++j) {
13+
if (neededTime[i] < neededTime[j]) {
14+
ans += neededTime[i];
15+
i = j;
16+
} else {
17+
ans += neededTime[j];
18+
}
19+
}
1220
}
21+
return ans;
22+
}
1323
};
1424

1525
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
26+
vector<string> inputArray;
27+
size_t pos = input_json_values.find('\n');
28+
while (pos != string::npos) {
29+
inputArray.push_back(input_json_values.substr(0, pos));
30+
input_json_values = input_json_values.substr(pos + 1);
31+
pos = input_json_values.find('\n');
32+
}
33+
inputArray.push_back(input_json_values);
2434

25-
Solution solution;
26-
string colors = json::parse(inputArray.at(0));
27-
vector<int> neededTime = json::parse(inputArray.at(1));
28-
return solution.minCost(colors, neededTime);
35+
Solution solution;
36+
string colors = json::parse(inputArray.at(0));
37+
vector<int> neededTime = json::parse(inputArray.at(1));
38+
return solution.minCost(colors, neededTime);
2939
}

problems/problems_1578/Solution.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@
44
import java.util.*;
55
import qubhjava.BaseSolution;
66

7-
87
public class Solution extends BaseSolution {
98
public int minCost(String colors, int[] neededTime) {
10-
9+
int ans = 0;
10+
int n = colors.length();
11+
for (int i = 0, j = 0; i < n - 1; i = j) {
12+
for (j = i + 1; j < n && colors.charAt(i) == colors.charAt(j); j++) {
13+
if (neededTime[i] < neededTime[j]) {
14+
ans += neededTime[i];
15+
i = j;
16+
} else {
17+
ans += neededTime[j];
18+
}
19+
}
20+
}
21+
return ans;
1122
}
1223

1324
@Override
1425
public Object solve(String[] inputJsonValues) {
1526
String colors = jsonStringToString(inputJsonValues[0]);
16-
int[] neededTime = jsonArrayToIntArray(inputJsonValues[1]);
27+
int[] neededTime = jsonArrayToIntArray(inputJsonValues[1]);
1728
return JSON.toJSON(minCost(colors, neededTime));
1829
}
1930
}

problems/problems_1578/solution.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@ import (
66
"strings"
77
)
88

9-
func minCost(colors string, neededTime []int) int {
10-
9+
func minCost(colors string, neededTime []int) (ans int) {
10+
n := len(colors)
11+
for i, j := 0, 0; i < n-1; i = j {
12+
for j = i + 1; j < n && colors[j] == colors[i]; j++ {
13+
if neededTime[i] < neededTime[j] {
14+
ans += neededTime[i]
15+
i = j
16+
} else {
17+
ans += neededTime[j]
18+
}
19+
}
20+
}
21+
return
1122
}
1223

1324
func Solve(inputJsonValues string) any {

problems/problems_1578/solution.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from itertools import pairwise
2+
13
import solution
24
from typing import *
35

@@ -7,5 +9,9 @@ def solve(self, test_input=None):
79
return self.minCost(*test_input)
810

911
def minCost(self, colors: str, neededTime: List[int]) -> int:
10-
pass
11-
12+
ans = 0
13+
for i, (a, b) in enumerate(pairwise(colors)):
14+
if a == b:
15+
ans += min(neededTime[i], neededTime[i + 1])
16+
neededTime[i + 1] = max(neededTime[i], neededTime[i + 1])
17+
return ans

problems/problems_1578/testcase

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
["\"abaac\"\n[1,2,3,4,5]", "\"abc\"\n[1,2,3]", "\"aabaa\"\n[1,2,3,4,1]"]
2-
[3, 0, 2]
1+
["\"abaac\"\n[1,2,3,4,5]", "\"abc\"\n[1,2,3]", "\"aabaa\"\n[1,2,3,4,1]", "\"aaabbbabbbb\"\n[3,5,10,7,5,3,5,5,4,8,1]"]
2+
[3, 0, 2, 26]

problems/problems_1578/testcase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self):
1010
self.testcases.append(case(Input=['abaac', [1, 2, 3, 4, 5]], Output=3))
1111
self.testcases.append(case(Input=['abc', [1, 2, 3]], Output=0))
1212
self.testcases.append(case(Input=['aabaa', [1, 2, 3, 4, 1]], Output=2))
13+
self.testcases.append(case(Input=["aaabbbabbbb",[3,5,10,7,5,3,5,5,4,8,1]], Output=26))
1314

1415
def get_testcases(self):
1516
return self.testcases

0 commit comments

Comments
 (0)