Count Good Subsegments
Learn this problemProblem statement
TikTok is organizing a grand TikTok event and wants to optimize the content delivery routes for the users. They have numServers servers numbered from 1 to numServers. The servers will be arranged in a sequence as follows: [1, 2, 3, ..., numServers]. The company has a list of numDisconnectedPairs pairs of servers that are not directly connected. Any pair not present in this list are directly connected. A subsegment of the route starting from server start and ending at server end is [start, start+1, start+2, ..., end]. A subsegment of the route is called good when all pairs of servers in that segment are directly connected. TikTok wants to know how many pairs (start, end) there are (1 ≤ start ≤ end ≤ numServers), such that the subsegment starting from server start and ending at server end is good.
Note: Servers can be reused across different subsegments. Each subsegment is considered independently, so a server can be part of multiple "good" subsegments. For example, if a server 3 is part of a "good" subsegment [2, 3, 4], it can still be reused in another "good" subsegment, like [3, 4, 5].
Function
countGoodSubsegments(numServers: int, numDisconnectedPairs: int, disconnectedPairs: int[][]) → intExamples
Example 1
numServers = 4numDisconnectedPairs = 2disconnectedPairs = [[1, 2], [2, 3]]return = 5Constraints
🍅🍅More Tiktok problems
- Can Reach the Exit with TeleportsOA · Seen Jul 2026
- Check Monotonic TriplesOA · Seen Jul 2026
- Shift Every K-th ConsonantOA · Seen Jul 2026
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Sort Matrix BordersOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Validate 3x3 Digit WindowsOA · Seen Jul 2026