Dynamic Critical Alert System
In a fast-paced environment, real-time decision-making often hinges on prioritizing critical information. Imagine a system designed to manage and display the most urgent alerts from a continuous stream. These alerts have varying priorities and impact scores, which can dynamically change. Your task is to design and implement a CriticalAlertSystem that efficiently tracks and retrieves the top K most critical alerts at any given moment, even as new alerts arrive, existing alerts' impact changes, or alerts are dismissed.
Implement the CriticalAlertSystem class with the following methods:
constructor(k: number): Initializes the system to track up to k most critical alerts.addAlert(alertId: string, priority: number, impactScore: number): Adds a new alert to the system. The alert's criticality is defined as priority + impactScore. If an alert with the given alertId already exists, this operation should fail.updateImpact(alertId: string, newImpactScore: number): Updates the impactScore of an existing alert. The alert's priority remains unchanged. This will recalculate its criticality.dismissAlert(alertId: string): Removes an existing alert from the system.getTopKAlerts(): Returns a list of alertIds representing the k most critical active alerts currently in the system. The alerts should be ordered by criticality in descending order. If two alerts have the same criticality, they should be ordered by alertId in ascending alphabetical order. If fewer than k active alerts exist, return all of them.Class: CriticalAlertSystem
constructor(k: number)
k: number - The maximum number of top critical alerts to track. (k >= 1)void (implicitly null in test cases).addAlert(alertId: string, priority: number, impactScore: number)
alertId: string - A unique identifier for the alert.priority: number - The base priority of the alert (non-negative integer).impactScore: number - The initial impact score of the alert (non-negative integer).boolean - true if the alert was successfully added, false if an alert with alertId already exists.updateImpact(alertId: string, newImpactScore: number)
alertId: string - The identifier of the alert to update.newImpactScore: number - The new impact score for the alert (non-negative integer).boolean - true if the alert's impact was successfully updated, false if alertId does not exist or has been dismissed.dismissAlert(alertId: string)
alertId: string - The identifier of the alert to dismiss.boolean - true if the alert was successfully dismissed, false if alertId does not exist or has already been dismissed.getTopKAlerts()
string[] - A list of alertIds of the top k most critical alerts, sorted as specified in the task description.priority + impactScore.getTopKAlerts: When two alerts have the same criticality, the one with the lexicographically smaller alertId should come first.K alerts: If the system has fewer than k active alerts, getTopKAlerts should return all currently active alerts, sorted by criticality and alertId as specified.alertId: If updateImpact or dismissAlert is called with an alertId that does not exist or has already been dismissed, it should return false.addAlert with existing alertId: If addAlert is called with an alertId that already exists (and is active), it should return false.Examples
Example 1
Input:
operations = ["CriticalAlertSystem","addAlert","addAlert","addAlert","getTopKAlerts","updateImpact","getTopKAlerts","dismissAlert","getTopKAlerts"] params = [[2],["A1",10,5],["A2",8,8],["A3",12,1],[],["A1",10],[],["A2"],[]]
Output:
[null,true,true,true,["A2","A1"],true,["A1","A3"],true,["A1","A3"]]
Note:
Example 2
Input:
operations = ["CriticalAlertSystem","addAlert","addAlert","addAlert","dismissAlert","addAlert","getTopKAlerts"] params = [[1],["X1",5,5],["X2",10,10],["X3",15,15],["X2"],["X4",20,20],[]]
Output:
[null,true,true,true,true,true,["X4"]]
Note:
Example 3
Input:
operations = ["CriticalAlertSystem","addAlert","updateImpact","addAlert","getTopKAlerts","dismissAlert","getTopKAlerts","updateImpact"] params = [[3],["AlertA",10,10],["AlertA",5],["AlertB",20,20],[],["AlertA"],[],["AlertC",10]]
Output:
[null,true,true,true,["AlertB","AlertA"],true,["AlertB"],false]
Note:
Constraints
1 <= k <= 10^40 <= priority, impactScore, newImpactScore <= 10^9alertId consists of alphanumeric characters.addAlert, updateImpact, dismissAlert, and getTopKAlerts operations will not exceed 10^5.priority and impactScore will not exceed 2 * 10^9.O(log K) amortized time complexity for addAlert, updateImpact, and dismissAlert operations. getTopKAlerts should ideally be O(K log K) in the worst case.Loading editor...