Skip to content

feat: add solutions to lc problems: No.3062,3063 #2391

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 3 commits into from
Feb 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Heights table:
| 6 |
+---------------------+
<strong>Explanation:</strong>
<img src="https://assets.leetcode.com/uploads/2024/02/26/trapping_rain_water.png" style="width:500px; height:200px;" />
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3061.Calculate%20Trapping%20Rain%20Water/images/trapping_rain_water.png" style="width:500px; height:200px;" />

The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Heights table:
| 6 |
+---------------------+
<strong>Explanation:</strong>
<img src="https://assets.leetcode.com/uploads/2024/02/26/trapping_rain_water.png" style="width:500px; height:200px;" />
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3061.Calculate%20Trapping%20Rain%20Water/images/trapping_rain_water.png" style="width:500px; height:200px;" />

The elevation map depicted above (in the black section) is graphically represented with the x-axis denoting the id and the y-axis representing the heights [0,1,0,2,1,0,1,3,2,1,2,1]. In this scenario, 6 units of rainwater are trapped within the blue section.
</pre>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 136 additions & 4 deletions solution/3000-3099/3062.Winner of the Linked List Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,156 @@

## 解法

### 方法一
### 方法一:模拟

遍历链表,每次取出两个节点,比较它们的值,然后根据比较结果更新奇数和偶数的得分。最后比较奇数和偶数的得分,返回结果。

时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def gameResult(self, head: Optional[ListNode]) -> str:
odd = even = 0
while head:
a = head.val
b = head.next.val
odd += a < b
even += a > b
head = head.next.next
if odd > even:
return "Odd"
if odd < even:
return "Even"
return "Tie"
```

```java

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public String gameResult(ListNode head) {
int odd = 0, even = 0;
for (; head != null; head = head.next.next) {
int a = head.val;
int b = head.next.val;
odd += a < b ? 1 : 0;
even += a > b ? 1 : 0;
}
if (odd > even) {
return "Odd";
}
if (odd < even) {
return "Even";
}
return "Tie";
}
}
```

```cpp

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
string gameResult(ListNode* head) {
int odd = 0, even = 0;
for (; head != nullptr; head = head->next->next) {
int a = head->val;
int b = head->next->val;
odd += a < b;
even += a > b;
}
if (odd > even) {
return "Odd";
}
if (odd < even) {
return "Even";
}
return "Tie";
}
};
```

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func gameResult(head *ListNode) string {
var odd, even int
for ; head != nil; head = head.Next.Next {
a, b := head.Val, head.Next.Val
if a < b {
odd++
}
if a > b {
even++
}
}
if odd > even {
return "Odd"
}
if odd < even {
return "Even"
}
return "Tie"
}
```

```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function gameResult(head: ListNode | null): string {
let [odd, even] = [0, 0];
for (; head; head = head.next.next) {
const [a, b] = [head.val, head.next.val];
odd += a < b ? 1 : 0;
even += a > b ? 1 : 0;
}
if (odd > even) {
return 'Odd';
}
if (odd < even) {
return 'Even';
}
return 'Tie';
}
```

<!-- tabs:end -->
Expand Down
140 changes: 136 additions & 4 deletions solution/3000-3099/3062.Winner of the Linked List Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,156 @@

## Solutions

### Solution 1
### Solution 1: Simulation

Traverse the linked list, each time taking out two nodes, compare their values, and then update the scores of odd and even numbers based on the comparison results. Finally, compare the scores of odd and even numbers and return the result.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

```python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def gameResult(self, head: Optional[ListNode]) -> str:
odd = even = 0
while head:
a = head.val
b = head.next.val
odd += a < b
even += a > b
head = head.next.next
if odd > even:
return "Odd"
if odd < even:
return "Even"
return "Tie"
```

```java

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public String gameResult(ListNode head) {
int odd = 0, even = 0;
for (; head != null; head = head.next.next) {
int a = head.val;
int b = head.next.val;
odd += a < b ? 1 : 0;
even += a > b ? 1 : 0;
}
if (odd > even) {
return "Odd";
}
if (odd < even) {
return "Even";
}
return "Tie";
}
}
```

```cpp

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
string gameResult(ListNode* head) {
int odd = 0, even = 0;
for (; head != nullptr; head = head->next->next) {
int a = head->val;
int b = head->next->val;
odd += a < b;
even += a > b;
}
if (odd > even) {
return "Odd";
}
if (odd < even) {
return "Even";
}
return "Tie";
}
};
```

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func gameResult(head *ListNode) string {
var odd, even int
for ; head != nil; head = head.Next.Next {
a, b := head.Val, head.Next.Val
if a < b {
odd++
}
if a > b {
even++
}
}
if odd > even {
return "Odd"
}
if odd < even {
return "Even"
}
return "Tie"
}
```

```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function gameResult(head: ListNode | null): string {
let [odd, even] = [0, 0];
for (; head; head = head.next.next) {
const [a, b] = [head.val, head.next.val];
odd += a < b ? 1 : 0;
even += a > b ? 1 : 0;
}
if (odd > even) {
return 'Odd';
}
if (odd < even) {
return 'Even';
}
return 'Tie';
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
string gameResult(ListNode* head) {
int odd = 0, even = 0;
for (; head != nullptr; head = head->next->next) {
int a = head->val;
int b = head->next->val;
odd += a < b;
even += a > b;
}
if (odd > even) {
return "Odd";
}
if (odd < even) {
return "Even";
}
return "Tie";
}
};
26 changes: 26 additions & 0 deletions solution/3000-3099/3062.Winner of the Linked List Game/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func gameResult(head *ListNode) string {
var odd, even int
for ; head != nil; head = head.Next.Next {
a, b := head.Val, head.Next.Val
if a < b {
odd++
}
if a > b {
even++
}
}
if odd > even {
return "Odd"
}
if odd < even {
return "Even"
}
return "Tie"
}
Loading