Created
September 7, 2026 11:28
-
-
Save SuryaPratapK/42374a29e252f0655f42541422571bdc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int countGroups(vector<int>& position, vector<int>& speed, int distance) { | |
| int n = position.size(); | |
| int groups = 1; //atleast 1 group will be made | |
| int group_speed = speed[n-1]; | |
| for(int i=n-2;i>=0;--i){ | |
| if(position[i+1]-position[i] <= distance){ | |
| continue; | |
| }else if(speed[i]<=group_speed){ | |
| groups++; | |
| group_speed = speed[i]; | |
| } | |
| } | |
| return groups; | |
| } | |
| }; | |
| /* | |
| //JAVA | |
| class Solution { | |
| public int countGroups(int[] position, int[] speed, int distance) { | |
| int n = position.length; | |
| int groups = 1; // at least 1 group will be made | |
| int groupSpeed = speed[n - 1]; | |
| for (int i = n - 2; i >= 0; --i) { | |
| if (position[i + 1] - position[i] <= distance) { | |
| continue; | |
| } else if (speed[i] <= groupSpeed) { | |
| groups++; | |
| groupSpeed = speed[i]; | |
| } | |
| } | |
| return groups; | |
| } | |
| } | |
| #Python | |
| class Solution: | |
| def countGroups(self, position, speed, distance): | |
| n = len(position) | |
| groups = 1 # at least 1 group will be made | |
| group_speed = speed[n - 1] | |
| for i in range(n - 2, -1, -1): | |
| if position[i + 1] - position[i] <= distance: | |
| continue | |
| elif speed[i] <= group_speed: | |
| groups += 1 | |
| group_speed = speed[i] | |
| return groups | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment