diff --git a/solution/2000-2099/2097.Valid Arrangement of Pairs/README.md b/solution/2000-2099/2097.Valid Arrangement of Pairs/README.md index 1fa6c8e20ed4e..ae3cdc696f5b7 100644 --- a/solution/2000-2099/2097.Valid Arrangement of Pairs/README.md +++ b/solution/2000-2099/2097.Valid Arrangement of Pairs/README.md @@ -89,6 +89,32 @@ end1 = 1 == 1 = start2 #### 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)] ``` diff --git a/solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md b/solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md index d6634170a35e5..7ebe2684b2d79 100644 --- a/solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md +++ b/solution/2000-2099/2097.Valid Arrangement of Pairs/README_EN.md @@ -87,6 +87,32 @@ end1 = 1 == 1 = start2 #### 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)] ``` diff --git a/solution/2000-2099/2097.Valid Arrangement of Pairs/Solution.py b/solution/2000-2099/2097.Valid Arrangement of Pairs/Solution.py new file mode 100644 index 0000000000000..f4b716c18f91f --- /dev/null +++ b/solution/2000-2099/2097.Valid Arrangement of Pairs/Solution.py @@ -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)] diff --git a/solution/2700-2799/2751.Robot Collisions/README.md b/solution/2700-2799/2751.Robot Collisions/README.md index ed0bb46be82ec..0f8fb742950e7 100644 --- a/solution/2700-2799/2751.Robot Collisions/README.md +++ b/solution/2700-2799/2751.Robot Collisions/README.md @@ -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 diff --git a/solution/2700-2799/2751.Robot Collisions/README_EN.md b/solution/2700-2799/2751.Robot Collisions/README_EN.md index f624b21067c2e..4bf8202571caa 100644 --- a/solution/2700-2799/2751.Robot Collisions/README_EN.md +++ b/solution/2700-2799/2751.Robot Collisions/README_EN.md @@ -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 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..6415e99fc3727 --- /dev/null +++ b/solution/2700-2799/2751.Robot Collisions/Solution.py @@ -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] diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md index 02f7391356a66..6cfff1d76ab4f 100644 --- a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/README_EN.md @@ -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 diff --git a/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py new file mode 100644 index 0000000000000..40d274514a87c --- /dev/null +++ b/solution/3000-3099/3068.Find the Maximum Sum of Node Values/Solution.py @@ -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