leetcode

Find Closest Person - Link

Question Description

Given three integers x, y, and z, return:


Constraints


Approach


Dry Run

Example Input: x = 3, y = 7, z = 5

Step-by-step execution:


Solution

class Solution {
    public int findClosest(int x, int y, int z) {
        int xStepNeed = Math.abs(x - z);
        int yStepNeed = Math.abs(y - z);

        if (xStepNeed < yStepNeed) return 1;
        else if (xStepNeed > yStepNeed) return 2;
        else return 0;
    }
}

Time and Space Complexity