Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ There are no longer two consecutive balloons of the same color. Total time = 1 +

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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 {
Expand All @@ -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>) -> 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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ 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 {
ans += s - mx
}
}
return
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
impl Solution {
pub fn min_cost(colors: String, needed_time: Vec<i32>) -> 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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}