Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SuryaPratapK/42374a29e252f0655f42541422571bdc to your computer and use it in GitHub Desktop.

Select an option

Save SuryaPratapK/42374a29e252f0655f42541422571bdc to your computer and use it in GitHub Desktop.
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