Skip to content
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

Added the python solution for Problem no 2097 #3828

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,32 @@ end<sub>1</sub> = 1 == 1 = start<sub>2</sub>
#### Python3

```python
class Solution:
def validArrangement(self, pairs):
graph = defaultdict(deque)
degree = defaultdict(int)

for u, v in pairs:
graph[u].append(v)
degree[u] += 1
degree[v] -= 1

start = pairs[0][0]
for node in graph:
if degree[node] > 0:
start = node
break

path = []

def traverse(node):
while graph[node]:
traverse(graph[node].popleft())
path.append(node)

traverse(start)
path.reverse()
return [[path[i], path[i + 1]] for i in range(len(path) - 1)]

```

Expand Down
26 changes: 26 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@ end<sub>1</sub> = 1 == 1 = start<sub>2</sub>
#### Python3

```python
class Solution:
def validArrangement(self, pairs):
graph = defaultdict(deque)
degree = defaultdict(int)

for u, v in pairs:
graph[u].append(v)
degree[u] += 1
degree[v] -= 1

start = pairs[0][0]
for node in graph:
if degree[node] > 0:
start = node
break

path = []

def traverse(node):
while graph[node]:
traverse(graph[node].popleft())
path.append(node)

traverse(start)
path.reverse()
return [[path[i], path[i + 1]] for i in range(len(path) - 1)]

```

Expand Down
26 changes: 26 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:
def validArrangement(self, pairs):
graph = defaultdict(deque)
degree = defaultdict(int)

for u, v in pairs:
graph[u].append(v)
degree[u] += 1
degree[v] -= 1

start = pairs[0][0]
for node in graph:
if degree[node] > 0:
start = node
break

path = []

def traverse(node):
while graph[node]:
traverse(graph[node].popleft())
path.append(node)

traverse(start)
path.reverse()
return [[path[i], path[i + 1]] for i in range(len(path) - 1)]
25 changes: 24 additions & 1 deletion solution/2700-2799/2751.Robot Collisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,30 @@ tags:
#### Python3

```python

class Solution:
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
temp = {x: i for i,x in enumerate(positions)}

stack = []
for x in sorted(positions):
i = temp[x]

if directions[i] == "R":
stack.append(i)
else:
while stack and healths[i]:
j = stack.pop()
if healths[i] > healths[j]:
healths[j] = 0
healths[i] -= 1
elif healths[i] < healths[j]:
healths[i] = 0
healths[j] -= 1
stack.append(j)
else:
healths[i] = healths[j] = 0

return [item for item in healths if item > 0]
```

#### Java
Expand Down
25 changes: 24 additions & 1 deletion solution/2700-2799/2751.Robot Collisions/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,30 @@ tags:
#### Python3

```python

class Solution:
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
temp = {x: i for i,x in enumerate(positions)}

stack = []
for x in sorted(positions):
i = temp[x]

if directions[i] == "R":
stack.append(i)
else:
while stack and healths[i]:
j = stack.pop()
if healths[i] > healths[j]:
healths[j] = 0
healths[i] -= 1
elif healths[i] < healths[j]:
healths[i] = 0
healths[j] -= 1
stack.append(j)
else:
healths[i] = healths[j] = 0

return [item for item in healths if item > 0]
```

#### Java
Expand Down
24 changes: 24 additions & 0 deletions solution/2700-2799/2751.Robot Collisions/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
temp = {x: i for i,x in enumerate(positions)}

stack = []
for x in sorted(positions):
i = temp[x]

if directions[i] == "R":
stack.append(i)
else:
while stack and healths[i]:
j = stack.pop()
if healths[i] > healths[j]:
healths[j] = 0
healths[i] -= 1
elif healths[i] < healths[j]:
healths[i] = 0
healths[j] -= 1
stack.append(j)
else:
healths[i] = healths[j] = 0

return [item for item in healths if item > 0]
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ It can be shown that 9 is the maximum achievable sum of values.
#### Python3

```python

class Solution:
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
n = len(nums)
res = [(x^k)-x for x in nums]
res.sort(reverse=True)
ans = sum(nums)
val = ans
for i in range(0, n-1, 2):
ans += res[i] + res[i+1]
val = max(ans, val)
return val
```

#### Java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
n = len(nums)
res = [(x^k)-x for x in nums]
res.sort(reverse=True)
ans = sum(nums)
val = ans
for i in range(0, n-1, 2):
ans += res[i] + res[i+1]
val = max(ans, val)
return val