diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index ed0bb46be82ec..276c770bb4572 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -90,25 +90,186 @@ tags: #### Python3 ```python +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result ``` #### Java ```java - +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; ``` #### Go ```go - +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} ``` diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index f624b21067c2e..d3516d9da0290 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -90,25 +90,186 @@ tags: #### Python3 ```python +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result ``` #### Java ```java - +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; ``` #### Go ```go - +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} ``` diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.cpp b/solution/2700-2799/2751.Robot Collisions/Solution.cpp new file mode 100644 index 0000000000000..0743eac1fed2d --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + int n = positions.size(); + vector indices(n); + + iota(indices.begin(), indices.end(), 0); + stack st; + + auto lambda = [&](int i, int j) { return positions[i] < positions[j]; }; + + sort(begin(indices), end(indices), lambda); + + vector result; + for (int currentIndex : indices) { + if (directions[currentIndex] == 'R') { + st.push(currentIndex); + } else { + while (!st.empty() && healths[currentIndex] > 0) { + int topIndex = st.top(); + st.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + st.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + for (int i = 0; i < n; ++i) { + if (healths[i] > 0) { + result.push_back(healths[i]); + } + } + return result; + } +}; diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.go b/solution/2700-2799/2751.Robot Collisions/Solution.go new file mode 100644 index 0000000000000..bf12096531e5f --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.go @@ -0,0 +1,45 @@ +func survivedRobotsHealths(positions []int, healths []int, directions string) []int { + n := len(positions) + indices := make([]int, n) + for i := range indices { + indices[i] = i + } + + sort.Slice(indices, func(i, j int) bool { + return positions[indices[i]] < positions[indices[j]] + }) + + stack := []int{} + + for _, currentIndex := range indices { + if directions[currentIndex] == 'R' { + stack = append(stack, currentIndex) + } else { + for len(stack) > 0 && healths[currentIndex] > 0 { + topIndex := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + if healths[topIndex] > healths[currentIndex] { + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack = append(stack, topIndex) + } else if healths[topIndex] < healths[currentIndex] { + healths[currentIndex] -= 1 + healths[topIndex] = 0 + } else { + healths[currentIndex] = 0 + healths[topIndex] = 0 + } + } + } + } + + result := []int{} + for _, health := range healths { + if health > 0 { + result = append(result, health) + } + } + + return result +} \ No newline at end of file diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.java b/solution/2700-2799/2751.Robot Collisions/Solution.java new file mode 100644 index 0000000000000..59e12be376c0f --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.java @@ -0,0 +1,44 @@ +class Solution { + public List survivedRobotsHealths(int[] positions, int[] healths, String directions) { + int n = positions.length; + Integer[] indices = new Integer[n]; + for (int i = 0; i < n; i++) { + indices[i] = i; + } + + Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j])); + + Stack stack = new Stack<>(); + + for (int currentIndex : indices) { + if (directions.charAt(currentIndex) == 'R') { + stack.push(currentIndex); + } else { + while (!stack.isEmpty() && healths[currentIndex] > 0) { + int topIndex = stack.pop(); + + if (healths[topIndex] > healths[currentIndex]) { + healths[topIndex] -= 1; + healths[currentIndex] = 0; + stack.push(topIndex); + } else if (healths[topIndex] < healths[currentIndex]) { + healths[currentIndex] -= 1; + healths[topIndex] = 0; + } else { + healths[currentIndex] = 0; + healths[topIndex] = 0; + } + } + } + } + + List result = new ArrayList<>(); + for (int health : healths) { + if (health > 0) { + result.add(health); + } + } + + return result; + } +} \ No newline at end of file diff --git a/solution/2700-2799/2751.Robot Collisions/Solution.py b/solution/2700-2799/2751.Robot Collisions/Solution.py new file mode 100644 index 0000000000000..ed103f23961c5 --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.py @@ -0,0 +1,30 @@ +class Solution: + def survivedRobotsHealths( + self, positions: List[int], healths: List[int], directions: str + ) -> List[int]: + n = len(positions) + indices = list(range(n)) + stack = [] + + indices.sort(key=lambda i: positions[i]) + + for currentIndex in indices: + if directions[currentIndex] == "R": + stack.append(currentIndex) + else: + while stack and healths[currentIndex] > 0: + topIndex = stack.pop() + + if healths[topIndex] > healths[currentIndex]: + healths[topIndex] -= 1 + healths[currentIndex] = 0 + stack.append(topIndex) + elif healths[topIndex] < healths[currentIndex]: + healths[currentIndex] -= 1 + healths[topIndex] = 0 + else: + healths[currentIndex] = 0 + healths[topIndex] = 0 + + result = [health for health in healths if health > 0] + return result