Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a string s of length n and an integer array order, where order is a permutation of the numbers in the range [0, n - 1].
Starting from time t = 0, replace the character at index order[t] in s with '*' at each time step.
A substring is valid if it contains at least one '*'.
A string is active if the total number of valid substrings is greater than or equal to k.
Return the minimum time t at which the string s becomes active. If it is impossible, return -1.
Example 1:
Input: s = "abc", order = [1,0,2], k = 2
Output: 0
Explanation:
t |
order[t] |
Modified s |
Valid Substrings | Count | Active (Count >= k) |
|---|---|---|---|---|---|
| 0 | 1 | "a*c" |
"*", "a*", "*c", "a*c" |
4 | Yes |
The string s becomes active at t = 0. Thus, the answer is 0.
Example 2:
Input: s = "cat", order = [0,2,1], k = 6
Output: 2
Explanation:
t |
order[t] |
Modified s |
Valid Substrings | Count | Active (Count >= k) |
|---|---|---|---|---|---|
| 0 | 0 | "*at" |
"*", "*a", "*at" |
3 | No |
| 1 | 2 | "*a*" |
"*", "*a", ", ", "*" |
5 | No |
| 2 | 1 | "***" |
All substrings (contain '*') |
6 | Yes |
The string s becomes active at t = 2. Thus, the answer is 2.
Example 3:
Input: s = "xy", order = [0,1], k = 4
Output: -1
Explanation:
Even after all replacements, it is impossible to obtain k = 4 valid substrings. Thus, the answer is -1.
Constraints:
1 <= n == s.length <= 105order.length == n0 <= order[i] <= n - 1s consists of lowercase English letters.order is a permutation of integers from 0 to n - 1.1 <= k <= 109Problem summary: You are given a string s of length n and an integer array order, where order is a permutation of the numbers in the range [0, n - 1]. Starting from time t = 0, replace the character at index order[t] in s with '*' at each time step. A substring is valid if it contains at least one '*'. A string is active if the total number of valid substrings is greater than or equal to k. Return the minimum time t at which the string s becomes active. If it is impossible, return -1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
"abc" [1,0,2] 2
"cat" [0,2,1] 6
"xy" [0,1] 4
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3639: Minimum Time to Activate String
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3639: Minimum Time to Activate String
// package main
//
// import "sort"
//
// // https://space.bilibili.com/206214
// func minTime1(s string, order []int, k int) int {
// n := len(s)
// if n*(n+1)/2 < k { // 全改成星号也无法满足要求
// return -1
// }
//
// star := make([]int, n) // 避免在二分内部反复创建/初始化列表
// ans := sort.Search(n-1, func(m int) bool {
// m++
// for _, j := range order[:m] {
// star[j] = m
// }
// cnt := 0
// last := -1 // 上一个 '*' 的位置
// for i, x := range star {
// if x == m { // s[i] 是 '*'
// last = i
// }
// cnt += last + 1
// if cnt >= k { // 提前退出循环
// return true
// }
// }
// return false
// })
// return ans
// }
//
// func minTime(s string, order []int, k int) int {
// n := len(s)
// cnt := n * (n + 1) / 2
// if cnt < k { // 全改成星号也无法满足要求
// return -1
// }
//
// // 数组模拟双向链表
// prev := make([]int, n+1)
// next := make([]int, n)
// for i := range n {
// prev[i] = i - 1
// next[i] = i + 1
// }
//
// for t := n - 1; ; t-- {
// i := order[t]
// l, r := prev[i], next[i]
// cnt -= (i - l) * (r - i)
// if cnt < k {
// return t
// }
// // 删除链表中的 i
// if l >= 0 {
// next[l] = r
// }
// prev[r] = l
// }
// }
// Accepted solution for LeetCode #3639: Minimum Time to Activate String
package main
import "sort"
// https://space.bilibili.com/206214
func minTime1(s string, order []int, k int) int {
n := len(s)
if n*(n+1)/2 < k { // 全改成星号也无法满足要求
return -1
}
star := make([]int, n) // 避免在二分内部反复创建/初始化列表
ans := sort.Search(n-1, func(m int) bool {
m++
for _, j := range order[:m] {
star[j] = m
}
cnt := 0
last := -1 // 上一个 '*' 的位置
for i, x := range star {
if x == m { // s[i] 是 '*'
last = i
}
cnt += last + 1
if cnt >= k { // 提前退出循环
return true
}
}
return false
})
return ans
}
func minTime(s string, order []int, k int) int {
n := len(s)
cnt := n * (n + 1) / 2
if cnt < k { // 全改成星号也无法满足要求
return -1
}
// 数组模拟双向链表
prev := make([]int, n+1)
next := make([]int, n)
for i := range n {
prev[i] = i - 1
next[i] = i + 1
}
for t := n - 1; ; t-- {
i := order[t]
l, r := prev[i], next[i]
cnt -= (i - l) * (r - i)
if cnt < k {
return t
}
// 删除链表中的 i
if l >= 0 {
next[l] = r
}
prev[r] = l
}
}
# Accepted solution for LeetCode #3639: Minimum Time to Activate String
# Time: O(n)
# Space: O(n)
# backward simulation, doubly linked list
class Solution(object):
def minTime(self, s, order, k):
"""
:type s: str
:type order: List[int]
:type k: int
:rtype: int
"""
left = range(-1, len(s)-1)
right = range(1, len(s)+1)
cnt = (len(s)+1)*len(s)//2
if cnt < k:
return -1
for t in reversed(xrange(len(order))):
i = order[t]
l = left[i]
r = right[i]
cnt -= (i-l)*(r-i)
if cnt < k:
break
if l >= 0:
right[l] = r
if r < len(left):
left[r] = l
return t
// Accepted solution for LeetCode #3639: Minimum Time to Activate String
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3639: Minimum Time to Activate String
// package main
//
// import "sort"
//
// // https://space.bilibili.com/206214
// func minTime1(s string, order []int, k int) int {
// n := len(s)
// if n*(n+1)/2 < k { // 全改成星号也无法满足要求
// return -1
// }
//
// star := make([]int, n) // 避免在二分内部反复创建/初始化列表
// ans := sort.Search(n-1, func(m int) bool {
// m++
// for _, j := range order[:m] {
// star[j] = m
// }
// cnt := 0
// last := -1 // 上一个 '*' 的位置
// for i, x := range star {
// if x == m { // s[i] 是 '*'
// last = i
// }
// cnt += last + 1
// if cnt >= k { // 提前退出循环
// return true
// }
// }
// return false
// })
// return ans
// }
//
// func minTime(s string, order []int, k int) int {
// n := len(s)
// cnt := n * (n + 1) / 2
// if cnt < k { // 全改成星号也无法满足要求
// return -1
// }
//
// // 数组模拟双向链表
// prev := make([]int, n+1)
// next := make([]int, n)
// for i := range n {
// prev[i] = i - 1
// next[i] = i + 1
// }
//
// for t := n - 1; ; t-- {
// i := order[t]
// l, r := prev[i], next[i]
// cnt -= (i - l) * (r - i)
// if cnt < k {
// return t
// }
// // 删除链表中的 i
// if l >= 0 {
// next[l] = r
// }
// prev[r] = l
// }
// }
// Accepted solution for LeetCode #3639: Minimum Time to Activate String
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3639: Minimum Time to Activate String
// package main
//
// import "sort"
//
// // https://space.bilibili.com/206214
// func minTime1(s string, order []int, k int) int {
// n := len(s)
// if n*(n+1)/2 < k { // 全改成星号也无法满足要求
// return -1
// }
//
// star := make([]int, n) // 避免在二分内部反复创建/初始化列表
// ans := sort.Search(n-1, func(m int) bool {
// m++
// for _, j := range order[:m] {
// star[j] = m
// }
// cnt := 0
// last := -1 // 上一个 '*' 的位置
// for i, x := range star {
// if x == m { // s[i] 是 '*'
// last = i
// }
// cnt += last + 1
// if cnt >= k { // 提前退出循环
// return true
// }
// }
// return false
// })
// return ans
// }
//
// func minTime(s string, order []int, k int) int {
// n := len(s)
// cnt := n * (n + 1) / 2
// if cnt < k { // 全改成星号也无法满足要求
// return -1
// }
//
// // 数组模拟双向链表
// prev := make([]int, n+1)
// next := make([]int, n)
// for i := range n {
// prev[i] = i - 1
// next[i] = i + 1
// }
//
// for t := n - 1; ; t-- {
// i := order[t]
// l, r := prev[i], next[i]
// cnt -= (i - l) * (r - i)
// if cnt < k {
// return t
// }
// // 删除链表中的 i
// if l >= 0 {
// next[l] = r
// }
// prev[r] = l
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.