Day 76 of 100 days dsa coding challenge
Published: (December 19, 2025 at 12:48 AM EST)
1 min read
Source: Dev.to
Source: Dev.to
Problem
Bus Conductor – GeeksforGeeks
Difficulty: Easy
Accuracy: 75.3%
Examples
Example 1
- Input:
chairs = [2, 2, 6, 6],passengers = [1, 3, 2, 6] - Output:
4 - Explanation:
- The first passenger moves from position 1 to 2 (1 move).
- The second passenger moves from position 3 to 6 (3 moves).
- The third and fourth passengers are already at a chair (0 moves each).
- Total moves = 1 + 3 + 0 + 0 = 4.
Example 2
- The first passenger is moved from position 2 to 1 using 1 move.
- The second passenger is moved from position 7 to 5 using 2 moves.
- The third passenger is moved from position 4 to 3 using 1 move.
- Total moves: 1 + 2 + 1 = 4.
Constraints
- (1 \le n \le 10^5)
- (1 \le \text{chairs}[i], \text{passengers}[j] \le 10^4)
Solution
class Solution:
def findMoves(self, chairs, passengers):
chairs.sort()
passengers.sort()
moves = 0
for c, p in zip(chairs, passengers):
moves += abs(c - p)
return moves