I don't understand why it is failing.


  • 0
    S

    Here is my code if anyone wants to check.
    Tested it with different combinations and able to produce the expected output. Leetcode says wrong answer.

    Input:
    {"headers": {"Employee": ["Id"
    "Name"
    "Salary"
    "DepartmentId"]
    "Department": ["Id"
    "Name"]}
    "rows": {"Employee": [[1
    "Joe"
    60000
    1]
    [2
    "Ralph"
    30000
    1]
    [3
    "Joel"
    50000
    1]
    [4
    "Tracy"
    55000
    1]]
    "Department": [[1
    "IT"]]}}
    Output:
    {"headers": ["Department", "Employee", "Salary"], "values": []}
    Expected:
    {"headers": ["Department", "Employee", "Salary"], "values": [["IT", "Joe", 60000], ["IT", "Tracy", 55000], ["IT", "Joel", 50000]]}
    
    SELECT t2.Name Department, 
           t1.Name Employee, 
           t1.Salary 
      FROM (SELECT *, 
                   @RowNum := IF(@DepartmentId = DepartmentId AND @Salary <> Salary, @RowNum + 1, 1) AS rn, 
                   @DepartmentId := DepartmentId,
                   @Salary := Salary
              FROM Employee 
             ORDER BY DepartmentId, 
                      Salary DESC) t1 
           JOIN Department t2 
             ON t1.DepartmentId = t2.ID 
     WHERE rn <= 3 
     ORDER BY t2.Name, 
              t1.Salary DESC; 
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.