Problem · Array

Count Good Subsegments

Learn this problem
MediumTiktok logoTiktokFULLTIMEOA
See Tiktok hiring insights

Problem 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[][]) → int

Examples

Example 1

numServers = 4numDisconnectedPairs = 2disconnectedPairs = [[1, 2], [2, 3]]return = 5
The good subsegments are:
  • [1]
  • [2]
  • [3]
  • [4]
  • [3, 4]
  • Total = 5

    Constraints

    🍅🍅

    More Tiktok problems

    drafts saved locally
    public int countGoodSubsegments(int numServers, int numDisconnectedPairs, int[][] disconnectedPairs) {
      // write your code here
    }
    
    numServers4
    numDisconnectedPairs2
    disconnectedPairs[[1, 2], [2, 3]]
    expected5
    checking account