Skip to content

Commit c6953e5

Browse files
authored
feat: update solutions to lc problems: No.1171,1891 (#1843)
* No.1171.Remove Zero Sum Consecutive Nodes from Linked List * No.1891.Cutting Ribbons
1 parent 44de958 commit c6953e5

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function removeZeroSumSublists(head: ListNode | null): ListNode | null {
229229

230230
### **Rust**
231231

232-
```
232+
```rust
233233
// Definition for singly-linked list.
234234
// #[derive(PartialEq, Eq, Clone, Debug)]
235235
// pub struct ListNode {

solution/1100-1199/1171.Remove Zero Sum Consecutive Nodes from Linked List/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Prefix Sum + Hash Table**
47+
48+
If two prefix sums of the linked list are equal, it means that the sum of the continuous node sequence between the two prefix sums is $0$, so we can remove this part of the continuous nodes.
49+
50+
We first traverse the linked list and use a hash table $last$ to record the prefix sum and the corresponding linked list node. For the same prefix sum $s$, the later node overwrites the previous node.
51+
52+
Next, we traverse the linked list again. If the current node $cur$ has a prefix sum $s$ that appears in $last$, it means that the sum of all nodes between $cur$ and $last[s]$ is $0$, so we directly modify the pointer of $cur$ to $last[s].next$, which removes this part of the continuous nodes with a sum of $0$. We continue to traverse and delete all continuous nodes with a sum of $0$.
53+
54+
Finally, we return the head node of the linked list $dummy.next$.
55+
56+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the linked list.
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**
@@ -208,7 +220,7 @@ function removeZeroSumSublists(head: ListNode | null): ListNode | null {
208220

209221
### **Rust**
210222

211-
```
223+
```rust
212224
// Definition for singly-linked list.
213225
// #[derive(PartialEq, Eq, Clone, Debug)]
214226
// pub struct ListNode {

solution/1800-1899/1891.Cutting Ribbons/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
最后,我们返回 $left$ 即可。
7676

77-
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(1)$。其中 $n$ 和 $M$ 分别为绳子的数量和绳子的最大长度。
77+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别为绳子的数量和绳子的最大长度。空间复杂度 $O(1)$
7878

7979
<!-- tabs:start -->
8080

@@ -232,7 +232,7 @@ function maxLength(ribbons: number[], k: number): number {
232232

233233
```rust
234234
impl Solution {
235-
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
235+
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
236236
let mut left = 0i32;
237237
let mut right = *ribbons.iter().max().unwrap();
238238
while left < right {

solution/1800-1899/1891.Cutting Ribbons/README_EN.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ Now you have 4 ribbons of length 4.
6565

6666
## Solutions
6767

68+
**Solution 1: Binary Search**
69+
70+
We observe that if we can obtain $k$ ropes of length $x$, then we can also obtain $k$ ropes of length $x-1$. This implies that there is a monotonicity property, and we can use binary search to find the maximum length $x$ such that we can obtain $k$ ropes of length $x$.
71+
72+
We define the left boundary of the binary search as $left=0$, the right boundary as $right=\max(ribbons)$, and the middle value as $mid=(left+right+1)/2$. We then calculate the number of ropes we can obtain with length $mid$, denoted as $cnt$. If $cnt \geq k$, it means we can obtain $k$ ropes of length $mid$, so we update $left$ to $mid$. Otherwise, we update $right$ to $mid-1$.
73+
74+
Finally, we return $left$ as the maximum length of the ropes we can obtain.
75+
76+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the number of ropes and the maximum length of the ropes, respectively. The space complexity is $O(1)$.
77+
6878
<!-- tabs:start -->
6979

7080
### **Python3**
@@ -217,7 +227,7 @@ function maxLength(ribbons: number[], k: number): number {
217227

218228
```rust
219229
impl Solution {
220-
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
230+
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
221231
let mut left = 0i32;
222232
let mut right = *ribbons.iter().max().unwrap();
223233
while left < right {

solution/1800-1899/1891.Cutting Ribbons/Solution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
impl Solution {
2-
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
2+
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
33
let mut left = 0i32;
44
let mut right = *ribbons.iter().max().unwrap();
55
while left < right {

0 commit comments

Comments
 (0)