Find ID of Soldier
There are N soldiers standing in a line, with IDs from 1 to N, in ascending order. They are participating in an exercise consisting of Q actions. During the ith action, the Major calls S numbers row and col. The soldiers at the rowith and (coli)th positions swap places, and so on until (rowi+m < (coli - m). Each of the soldier's IDs will be covered in the range [rowi, coli] for at most one action.
Write an algorithm to find the ID of the soldier at Kth position in the line after all the actions are completed.
Input
Output
Print an integer representing the ID of the Kth position soldier in the line after Q actions.
Constraints
1 ≤ posSoldier ≤ num ≤ 10^5
1 ≤ actions ≤ 10^5
1 ≤ rowi, coli ≤ num
1 ≤ i ≤ actions
num = 10 actions = 2 numSoldiers = 2 swaps = [[1, 5], [6, 10]] posSoldier = 1 return = 5
Step2: After the 2nd action, the position of soldiers is in the order: 5 4 3 2 1 10 9 8 7 6.
Step3: The ID of the soldier at position 1 is 5.
So, the output is 5.
🐝public int findIdOfSoldier(int num, int actions, int numSoldiers, int[][] swaps, int posSoldier) {
// write your code here
}