Seesaw balance
EasyOnline Assessment
Decide which way a playground seesaw tips when children are standing on it.
The seesaw is a plank balanced on a pivot. A position is the signed distance from the pivot in decimetres: negative numbers are on the left half, positive on the right, and 0 is directly over the pivot. The plank itself is weightless and reaches half_length on each side.
Each child is [weight, position]. A child whose position is beyond the plank, abs(position) > half_length, is not on the seesaw and is ignored; a child standing exactly at half_length is on it.
Each child on the plank pushes their side down with a torque equal to weight * abs(position). A child over the pivot adds nothing. Add up the torque on each side. Then:
- if the left total is larger than the right total by more than
tolerance, the answer is"left"(the left side goes down); - if the right total is larger than the left total by more than
tolerance, the answer is"right"; - otherwise (including an empty plank) the answer is
"level".
Input: riders, a list of [weight, position] pairs; half_length, the plank's reach on each side; tolerance, the largest torque difference still considered level.
Output: one of the strings "left", "right" or "level".
Example 1
- Input:
- [[[30,-4],[20,-2],[25,6]],8,10]
- Output:
- "level"
- Explanation
- Left: 30 at distance 4 gives 120, and 20 at distance 2 gives 40, for 160. Right: 25 at distance 6 gives 150. The left is larger by 10, which is not more than the tolerance of 10, so the seesaw is
"level".
Example 2
- Input:
- [[[10,-8],[10,9],[40,1],[55,0]],8,0]
- Output:
- "left"
- Explanation
- The rider at position 9 is beyond the plank (half length 8) and is ignored. The rider at position -8 is exactly at the end and counts: 10 * 8 = 80 on the left. The right has 40 * 1 = 40 and the child over the pivot adds nothing. The left wins by 40, more than the tolerance of 0, so the answer is
"left".
Example 3
- Input:
- [[[3,5],[4,12]],10,0]
- Output:
- "right"
- Explanation
- Only one child is on the plank, at position 5 with weight 3: a torque of 15 on the right, no tolerance, so the answer is
"right".
Constraints
0 <= riders.length <= 2001 <= weight <= 200-1,000 <= position <= 1,0001 <= half_length <= 5000 <= tolerance <= 100,000