straightforward solution using 3 joins

  • 2

    nothing fancy but very straightforward, just update the student name by the definition of "swap"

    case when mod(, 2)=1 then 
        case when s2.student is null then s1.student else s2.student end
        else s3.student end as student
    from seat s1
    left join seat s2
    left join seat s3
    order by asc

  • 1

    almost the same with yours:

    SELECT, IF(MOD(, 2) = 1, IFNULL(n.student, s.student), p.student) AS student
    FROM seat s
    LEFT JOIN seat p ON - 1 =
    LEFT JOIN seat n ON + 1 =
    ORDER BY id;

Log in to reply

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