From ff0976c2c585f9f8d2e6fd341445dfb0d2c6b83d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 3 Nov 2025 07:13:24 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1578 --- .../README.md | 60 ++++++++++++++++- .../README_EN.md | 66 +++++++++++++++++-- .../Solution.go | 6 +- .../Solution.rs | 27 ++++++++ .../Solution.ts | 19 ++++++ 5 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.rs create mode 100644 solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.ts diff --git a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README.md b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README.md index a5d4598ad5e19..f40f0966ca799 100644 --- a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README.md +++ b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README.md @@ -160,9 +160,7 @@ func minCost(colors string, neededTime []int) (ans int) { s, mx := 0, 0 for j < n && colors[j] == colors[i] { s += neededTime[j] - if mx < neededTime[j] { - mx = neededTime[j] - } + mx = max(mx, neededTime[j]) j++ } if j-i > 1 { @@ -173,6 +171,62 @@ func minCost(colors string, neededTime []int) (ans int) { } ``` +#### TypeScript + +```ts +function minCost(colors: string, neededTime: number[]): number { + let ans = 0; + const n = neededTime.length; + + for (let i = 0, j = 0; i < n; i = j) { + j = i; + let [s, mx] = [0, 0]; + while (j < n && colors[j] === colors[i]) { + s += neededTime[j]; + mx = Math.max(mx, neededTime[j]); + ++j; + } + if (j - i > 1) { + ans += s - mx; + } + } + + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_cost(colors: String, needed_time: Vec) -> i32 { + let n = needed_time.len(); + let mut ans = 0; + let bytes = colors.as_bytes(); + let mut i = 0; + + while i < n { + let mut j = i; + let mut s = 0; + let mut mx = 0; + + while j < n && bytes[j] == bytes[i] { + s += needed_time[j]; + mx = mx.max(needed_time[j]); + j += 1; + } + + if j - i > 1 { + ans += s - mx; + } + i = j; + } + + ans + } +} +``` + diff --git a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README_EN.md b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README_EN.md index 68221a9e9ea6d..563484ef2c2cc 100644 --- a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README_EN.md +++ b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README_EN.md @@ -70,7 +70,11 @@ There are no longer two consecutive balloons of the same color. Total time = 1 + -### Solution 1 +### Solution 1: Two Pointers + Greedy + +We can use two pointers to point to the beginning and end of the current consecutive balloons with the same color, then calculate the total time $s$ of these consecutive balloons with the same color, as well as the maximum time $mx$. If the number of consecutive balloons with the same color is greater than $1$, we can greedily choose to keep the balloon with the maximum time and remove the other balloons with the same color, which takes time $s - mx$, and add it to the answer. Then we continue to traverse until all balloons are traversed. + +The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the number of balloons. @@ -154,9 +158,7 @@ func minCost(colors string, neededTime []int) (ans int) { s, mx := 0, 0 for j < n && colors[j] == colors[i] { s += neededTime[j] - if mx < neededTime[j] { - mx = neededTime[j] - } + mx = max(mx, neededTime[j]) j++ } if j-i > 1 { @@ -167,6 +169,62 @@ func minCost(colors string, neededTime []int) (ans int) { } ``` +#### TypeScript + +```ts +function minCost(colors: string, neededTime: number[]): number { + let ans = 0; + const n = neededTime.length; + + for (let i = 0, j = 0; i < n; i = j) { + j = i; + let [s, mx] = [0, 0]; + while (j < n && colors[j] === colors[i]) { + s += neededTime[j]; + mx = Math.max(mx, neededTime[j]); + ++j; + } + if (j - i > 1) { + ans += s - mx; + } + } + + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_cost(colors: String, needed_time: Vec) -> i32 { + let n = needed_time.len(); + let mut ans = 0; + let bytes = colors.as_bytes(); + let mut i = 0; + + while i < n { + let mut j = i; + let mut s = 0; + let mut mx = 0; + + while j < n && bytes[j] == bytes[i] { + s += needed_time[j]; + mx = mx.max(needed_time[j]); + j += 1; + } + + if j - i > 1 { + ans += s - mx; + } + i = j; + } + + ans + } +} +``` + diff --git a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.go b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.go index f08e113027115..63120dafadc51 100644 --- a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.go +++ b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.go @@ -5,9 +5,7 @@ func minCost(colors string, neededTime []int) (ans int) { s, mx := 0, 0 for j < n && colors[j] == colors[i] { s += neededTime[j] - if mx < neededTime[j] { - mx = neededTime[j] - } + mx = max(mx, neededTime[j]) j++ } if j-i > 1 { @@ -15,4 +13,4 @@ func minCost(colors string, neededTime []int) (ans int) { } } return -} \ No newline at end of file +} diff --git a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.rs b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.rs new file mode 100644 index 0000000000000..cd252f206a420 --- /dev/null +++ b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.rs @@ -0,0 +1,27 @@ +impl Solution { + pub fn min_cost(colors: String, needed_time: Vec) -> i32 { + let n = needed_time.len(); + let mut ans = 0; + let bytes = colors.as_bytes(); + let mut i = 0; + + while i < n { + let mut j = i; + let mut s = 0; + let mut mx = 0; + + while j < n && bytes[j] == bytes[i] { + s += needed_time[j]; + mx = mx.max(needed_time[j]); + j += 1; + } + + if j - i > 1 { + ans += s - mx; + } + i = j; + } + + ans + } +} diff --git a/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.ts b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.ts new file mode 100644 index 0000000000000..37856746d4543 --- /dev/null +++ b/solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.ts @@ -0,0 +1,19 @@ +function minCost(colors: string, neededTime: number[]): number { + let ans = 0; + const n = neededTime.length; + + for (let i = 0, j = 0; i < n; i = j) { + j = i; + let [s, mx] = [0, 0]; + while (j < n && colors[j] === colors[i]) { + s += neededTime[j]; + mx = Math.max(mx, neededTime[j]); + ++j; + } + if (j - i > 1) { + ans += s - mx; + } + } + + return ans; +}