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

feat: update solutions to lc problems: No.1171,1891 #1843

Merged
merged 1 commit into from
Oct 18, 2023
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 @@ -229,7 +229,7 @@ function removeZeroSumSublists(head: ListNode | null): ListNode | null {

### **Rust**

```
```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@

## Solutions

**Solution 1: Prefix Sum + Hash Table**

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.

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.

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$.

Finally, we return the head node of the linked list $dummy.next$.

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

<!-- tabs:start -->

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

### **Rust**

```
```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
Expand Down
4 changes: 2 additions & 2 deletions solution/1800-1899/1891.Cutting Ribbons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

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

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

<!-- tabs:start -->

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

```rust
impl Solution {
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
let mut left = 0i32;
let mut right = *ribbons.iter().max().unwrap();
while left < right {
Expand Down
12 changes: 11 additions & 1 deletion solution/1800-1899/1891.Cutting Ribbons/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ Now you have 4 ribbons of length 4.

## Solutions

**Solution 1: Binary Search**

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$.

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$.

Finally, we return $left$ as the maximum length of the ropes we can obtain.

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)$.

<!-- tabs:start -->

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

```rust
impl Solution {
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
let mut left = 0i32;
let mut right = *ribbons.iter().max().unwrap();
while left < right {
Expand Down
2 changes: 1 addition & 1 deletion solution/1800-1899/1891.Cutting Ribbons/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
impl Solution {
fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
pub fn max_length(ribbons: Vec<i32>, k: i32) -> i32 {
let mut left = 0i32;
let mut right = *ribbons.iter().max().unwrap();
while left < right {
Expand Down