Skip to content

Commit bb1c54e

Browse files
authoredMar 28, 2024
feat: add solutions to lc problems: No.2046,2050 (doocs#2515)
1 parent 7a1c6bb commit bb1c54e

File tree

9 files changed

+113
-59
lines changed

9 files changed

+113
-59
lines changed
 

‎solution/2000-2099/2000.Reverse Prefix of Word/README.md

-21
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,4 @@ class Solution {
164164

165165
<!-- tabs:end -->
166166

167-
### 方法二
168-
169-
<!-- tabs:start -->
170-
171-
```java
172-
class Solution {
173-
public String reversePrefix(String word, char ch) {
174-
int j = word.indexOf(ch);
175-
if (j == -1) {
176-
return word;
177-
}
178-
return new StringBuilder(word.substring(0, j + 1))
179-
.reverse()
180-
.append(word.substring(j + 1))
181-
.toString();
182-
}
183-
}
184-
```
185-
186-
<!-- tabs:end -->
187-
188167
<!-- end -->

‎solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md

-21
Original file line numberDiff line numberDiff line change
@@ -163,25 +163,4 @@ class Solution {
163163

164164
<!-- tabs:end -->
165165

166-
### Solution 2
167-
168-
<!-- tabs:start -->
169-
170-
```java
171-
class Solution {
172-
public String reversePrefix(String word, char ch) {
173-
int j = word.indexOf(ch);
174-
if (j == -1) {
175-
return word;
176-
}
177-
return new StringBuilder(word.substring(0, j + 1))
178-
.reverse()
179-
.append(word.substring(j + 1))
180-
.toString();
181-
}
182-
}
183-
```
184-
185-
<!-- tabs:end -->
186-
187166
<!-- end -->

‎solution/2000-2099/2000.Reverse Prefix of Word/Solution2.java

-12
This file was deleted.

‎solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858

5959
### 方法一:头插法
6060

61-
先默认第一个点已经排序完毕。然后从第二个点开始,遇到值为负数的节点,采用头插法;非负数,则继续往下遍历即可。
61+
我们先默认第一个点已经排序完毕,然后从第二个点开始,遇到值为负数的节点,采用头插法;非负数,则继续往下遍历即可。
62+
63+
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。
6264

6365
<!-- tabs:start -->
6466

@@ -172,6 +174,36 @@ func sortLinkedList(head *ListNode) *ListNode {
172174
}
173175
```
174176

177+
```ts
178+
/**
179+
* Definition for singly-linked list.
180+
* class ListNode {
181+
* val: number
182+
* next: ListNode | null
183+
* constructor(val?: number, next?: ListNode | null) {
184+
* this.val = (val===undefined ? 0 : val)
185+
* this.next = (next===undefined ? null : next)
186+
* }
187+
* }
188+
*/
189+
190+
function sortLinkedList(head: ListNode | null): ListNode | null {
191+
let [prev, curr] = [head, head.next];
192+
while (curr !== null) {
193+
if (curr.val < 0) {
194+
const t = curr.next;
195+
prev.next = t;
196+
curr.next = head;
197+
head = curr;
198+
curr = t;
199+
} else {
200+
[prev, curr] = [curr, curr.next];
201+
}
202+
}
203+
return head;
204+
}
205+
```
206+
175207
<!-- tabs:end -->
176208

177209
<!-- end -->

‎solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/README_EN.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ The linked list is already sorted in non-decreasing order.
5454

5555
## Solutions
5656

57-
### Solution 1
57+
### Solution 1: Head Insertion Method
58+
59+
We first assume that the first node is already sorted. Starting from the second node, when we encounter a node with a negative value, we use the head insertion method. For non-negative values, we continue to traverse down.
60+
61+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
5862

5963
<!-- tabs:start -->
6064

@@ -168,6 +172,36 @@ func sortLinkedList(head *ListNode) *ListNode {
168172
}
169173
```
170174

175+
```ts
176+
/**
177+
* Definition for singly-linked list.
178+
* class ListNode {
179+
* val: number
180+
* next: ListNode | null
181+
* constructor(val?: number, next?: ListNode | null) {
182+
* this.val = (val===undefined ? 0 : val)
183+
* this.next = (next===undefined ? null : next)
184+
* }
185+
* }
186+
*/
187+
188+
function sortLinkedList(head: ListNode | null): ListNode | null {
189+
let [prev, curr] = [head, head.next];
190+
while (curr !== null) {
191+
if (curr.val < 0) {
192+
const t = curr.next;
193+
prev.next = t;
194+
curr.next = head;
195+
head = curr;
196+
curr = t;
197+
} else {
198+
[prev, curr] = [curr, curr.next];
199+
}
200+
}
201+
return head;
202+
}
203+
```
204+
171205
<!-- tabs:end -->
172206

173207
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function sortLinkedList(head: ListNode | null): ListNode | null {
14+
let [prev, curr] = [head, head.next];
15+
while (curr !== null) {
16+
if (curr.val < 0) {
17+
const t = curr.next;
18+
prev.next = t;
19+
curr.next = head;
20+
head = curr;
21+
curr = t;
22+
} else {
23+
[prev, curr] = [curr, curr.next];
24+
}
25+
}
26+
return head;
27+
}

‎solution/2000-2099/2050.Parallel Courses III/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
- 数组 $f$ 存储每个节点的最早完成时间,初始时 $f[i] = 0$;
7979
- 变量 $ans$ 记录最终的答案,初始时 $ans = 0$;
8080

81-
当 $q$ 非空时,依次取出队首节点 $i$,遍历 $g[i]$ 中的每个节点 $j$,更新 $f[j] = max(f[j], f[i] + time[j])$,同时更新 $ans = \max(ans, f[j])$,并将 $j$ 的入度减 $1$,如果此时 $j$ 的入度为 $0$,则将 $j$ 加入队列 $q$ 中;
81+
当 $q$ 非空时,依次取出队首节点 $i$,遍历 $g[i]$ 中的每个节点 $j$,更新 $f[j] = \max(f[j], f[i] + time[j])$,同时更新 $ans = \max(ans, f[j])$,并将 $j$ 的入度减 $1$,如果此时 $j$ 的入度为 $0$,则将 $j$ 加入队列 $q$ 中;
8282

8383
最终返回 $ans$。
8484

‎solution/2000-2099/2050.Parallel Courses III/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,22 @@ Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Topological Sorting + Dynamic Programming
67+
68+
First, we construct a directed acyclic graph based on the given prerequisite course relationships, perform topological sorting on this graph, and then use dynamic programming to find the minimum time required to complete all courses according to the results of the topological sorting.
69+
70+
We define the following data structures or variables:
71+
72+
- Adjacency list $g$ stores the directed acyclic graph, and an array $indeg$ stores the in-degree of each node;
73+
- Queue $q$ stores all nodes with an in-degree of $0$;
74+
- Array $f$ stores the earliest completion time of each node, initially $f[i] = 0$;
75+
- Variable $ans$ records the final answer, initially $ans = 0$;
76+
77+
When $q$ is not empty, take out the head node $i$ in turn, traverse each node $j$ in $g[i]$, update $f[j] = \max(f[j], f[i] + time[j])$, update $ans = \max(ans, f[j])$ at the same time, and reduce the in-degree of $j$ by $1$. If the in-degree of $j$ is $0$ at this time, add $j$ to the queue $q$;
78+
79+
Finally, return $ans$.
80+
81+
The time complexity is $O(m + n)$, and the space complexity is $O(m + n)$. Where $m$ is the length of the array $relations$.
6782

6883
<!-- tabs:start -->
6984

‎solution/2000-2099/2054.Two Best Non-Overlapping Events/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
### 方法一:排序 + 二分查找
5959

60-
时间复杂度 $O(nlogn)$,其中 $n$ 表示 $events$ 的长度。
60+
时间复杂度 $O(n \times \log n)$,其中 $n$ 表示 $events$ 的长度。
6161

6262
<!-- tabs:start -->
6363

0 commit comments

Comments
 (0)