Skip to content

feat: add solutions to lc problem: No.2751 #3270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 111 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
f4b58ec
feat: add solutions to lc problem: No.3033
Nothing-avil Feb 11, 2024
28d320d
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
bd08e77
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
bcc78e5
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
fcb8cff
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
bffd742
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
9da0d92
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
9d92cbf
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
90b9198
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
a1fee31
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
00bacf9
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
b778ba5
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
0a9cf6c
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
d80fccd
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
7df70e1
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
e0507ce
style: format code and docs with prettier
Nothing-avil Feb 11, 2024
22517c1
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
a941ad3
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
d34a5e0
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
5f429be
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
2990d85
feat: add solutions to lc problem: No.3034
Nothing-avil Feb 11, 2024
1f0f649
Merge branch 'doocs:main' into main
Nothing-avil Feb 12, 2024
e00919a
Merge branch 'doocs:main' into main
Nothing-avil Feb 13, 2024
bba266c
Merge branch 'doocs:main' into main
Nothing-avil Feb 14, 2024
c242103
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
432451a
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
b8ca15e
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
3874792
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
f76f1ed
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
4d0afb6
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
eec158c
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
1acdb34
style: format code and docs with prettier
Nothing-avil Feb 14, 2024
80427df
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
db0355c
feat: add solutions to lc problem: No.3036
Nothing-avil Feb 14, 2024
7e482bf
Update README.md
yanglbme Feb 15, 2024
7fa5e9a
Update README_EN.md
yanglbme Feb 15, 2024
0e917c8
Update Solution.go
yanglbme Feb 15, 2024
b1f7fb3
Update README.md
yanglbme Feb 15, 2024
e29bc0e
Update README_EN.md
yanglbme Feb 15, 2024
b3c1e89
Merge branch 'doocs:main' into main
Nothing-avil Feb 15, 2024
78d3d02
Merge branch 'doocs:main' into main
Nothing-avil Feb 16, 2024
1412dcf
Merge branch 'doocs:main' into main
Nothing-avil Feb 17, 2024
5aa8f0d
Merge branch 'doocs:main' into main
Nothing-avil Feb 17, 2024
5703df8
Merge branch 'doocs:main' into main
Nothing-avil Feb 18, 2024
4e36cf7
Merge branch 'doocs:main' into main
Nothing-avil Feb 18, 2024
8684d17
Merge branch 'doocs:main' into main
Nothing-avil Feb 19, 2024
bcc84c3
Merge branch 'doocs:main' into main
Nothing-avil Feb 20, 2024
6db66be
Merge branch 'doocs:main' into main
Nothing-avil Feb 21, 2024
871bd1d
Merge branch 'doocs:main' into main
Nothing-avil Feb 22, 2024
565c84c
Merge branch 'doocs:main' into main
Nothing-avil Feb 23, 2024
5b014e6
Merge branch 'doocs:main' into main
Nothing-avil Feb 24, 2024
0756942
Merge branch 'doocs:main' into main
Nothing-avil Feb 24, 2024
b0655b8
Merge branch 'doocs:main' into main
Nothing-avil Feb 25, 2024
b3a5aab
Merge branch 'doocs:main' into main
Nothing-avil Feb 25, 2024
8cbd311
Merge branch 'doocs:main' into main
Nothing-avil Feb 26, 2024
b83b291
Merge branch 'doocs:main' into main
Nothing-avil Feb 27, 2024
ae71b93
Merge branch 'doocs:main' into main
Nothing-avil Feb 29, 2024
e14f2c2
Merge branch 'doocs:main' into main
Nothing-avil Feb 29, 2024
7579619
Merge branch 'doocs:main' into main
Nothing-avil Mar 1, 2024
c018f15
Merge branch 'doocs:main' into main
Nothing-avil Mar 2, 2024
5ba14ea
Merge branch 'doocs:main' into main
Nothing-avil Mar 3, 2024
d603443
Merge branch 'doocs:main' into main
Nothing-avil Mar 4, 2024
ea6c627
Merge branch 'doocs:main' into main
Nothing-avil Mar 5, 2024
fd78ade
Merge branch 'doocs:main' into main
Nothing-avil Mar 7, 2024
5e10711
Merge branch 'doocs:main' into main
Nothing-avil Mar 8, 2024
f123dd2
Merge branch 'doocs:main' into main
Nothing-avil Mar 9, 2024
3368e73
Merge branch 'doocs:main' into main
Nothing-avil Mar 9, 2024
1f19428
Merge branch 'doocs:main' into main
Nothing-avil Mar 9, 2024
e9b1497
Merge branch 'doocs:main' into main
Nothing-avil Mar 10, 2024
f297611
Merge branch 'doocs:main' into main
Nothing-avil Mar 11, 2024
9c67a48
Merge branch 'doocs:main' into main
Nothing-avil Mar 12, 2024
8bf447f
Merge branch 'doocs:main' into main
Nothing-avil Mar 13, 2024
2bbbec2
Merge branch 'doocs:main' into main
Nothing-avil Mar 14, 2024
de9d1cc
Merge branch 'doocs:main' into main
Nothing-avil Mar 15, 2024
3566913
Merge branch 'doocs:main' into main
Nothing-avil Mar 16, 2024
994e05b
Merge branch 'doocs:main' into main
Nothing-avil Mar 17, 2024
015cd3d
Merge branch 'doocs:main' into main
Nothing-avil Mar 18, 2024
03fcdff
Merge branch 'doocs:main' into main
Nothing-avil Mar 19, 2024
e85d579
Merge branch 'doocs:main' into main
Nothing-avil Mar 20, 2024
bd2ea84
Merge branch 'doocs:main' into main
Nothing-avil Mar 21, 2024
5288ce9
Merge branch 'doocs:main' into main
Nothing-avil Mar 22, 2024
d60575e
Merge branch 'doocs:main' into main
Nothing-avil Mar 22, 2024
9a8c3cb
Merge branch 'doocs:main' into main
Nothing-avil Mar 23, 2024
70c8207
Merge branch 'doocs:main' into main
Nothing-avil Mar 24, 2024
1e147ce
Merge branch 'doocs:main' into main
Nothing-avil Mar 26, 2024
90005ea
Merge branch 'doocs:main' into main
Nothing-avil Mar 27, 2024
fa7ac02
Merge branch 'doocs:main' into main
Nothing-avil Mar 28, 2024
a67e7e3
Merge branch 'doocs:main' into main
Nothing-avil Apr 3, 2024
d57f61a
Merge branch 'doocs:main' into main
Nothing-avil Apr 5, 2024
4266245
Merge branch 'doocs:main' into main
Nothing-avil Apr 7, 2024
1a3c6cc
Merge branch 'doocs:main' into main
Nothing-avil Apr 15, 2024
3c4dcf6
Merge branch 'doocs:main' into main
Nothing-avil Apr 16, 2024
df43a34
Merge branch 'doocs:main' into main
Nothing-avil Apr 17, 2024
044bc10
Merge branch 'doocs:main' into main
Nothing-avil Apr 24, 2024
bff758f
Merge branch 'doocs:main' into main
Nothing-avil May 7, 2024
fe78bba
Merge branch 'doocs:main' into main
Nothing-avil May 15, 2024
623c46b
Merge branch 'doocs:main' into main
Nothing-avil Jun 7, 2024
ecde427
Merge branch 'doocs:main' into main
Nothing-avil Jun 10, 2024
a8e9684
Merge branch 'doocs:main' into main
Nothing-avil Jun 13, 2024
3e856b4
Merge branch 'doocs:main' into main
Nothing-avil Jun 25, 2024
fbdbd9b
Merge branch 'doocs:main' into main
Nothing-avil Jun 28, 2024
c253ed1
Merge branch 'doocs:main' into main
Nothing-avil Jul 4, 2024
fe06399
Merge branch 'doocs:main' into main
Nothing-avil Jul 8, 2024
2bb4569
Merge branch 'doocs:main' into main
Nothing-avil Jul 13, 2024
64784b2
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
1d7cf55
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
7c95881
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
5ff9779
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
da1d074
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
2c83fb3
feat: add solutions to lc problem: No.2751
Nothing-avil Jul 13, 2024
f6ac07a
Merge branch 'main' into main
Nothing-avil Jul 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 164 additions & 3 deletions solution/2700-2799/2751.Robot Collisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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<Integer> 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<Integer> result = new ArrayList<>();
for (int health : healths) {
if (health > 0) {
result.add(health);
}
}

return result;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
int n = positions.size();
vector<int> indices(n);

iota(indices.begin(), indices.end(), 0);
stack<int> st;

auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };

sort(begin(indices), end(indices), lambda);

vector<int> 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
}
```

<!-- tabs:end -->
Expand Down
167 changes: 164 additions & 3 deletions solution/2700-2799/2751.Robot Collisions/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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<Integer> 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<Integer> result = new ArrayList<>();
for (int health : healths) {
if (health > 0) {
result.add(health);
}
}

return result;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
int n = positions.size();
vector<int> indices(n);

iota(indices.begin(), indices.end(), 0);
stack<int> st;

auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };

sort(begin(indices), end(indices), lambda);

vector<int> 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
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading