Skip to content

Commit fa7ac02

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 90005ea + bb1c54e commit fa7ac02

File tree

26 files changed

+681
-135
lines changed

26 files changed

+681
-135
lines changed

.github/workflows/deploy.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
- docs
88
workflow_dispatch:
99

10+
env:
11+
MKDOCS_API_KEYS: ${{ secrets.MKDOCS_API_KEYS }}
12+
1013
permissions:
1114
contents: write
1215

solution/1900-1999/1997.First Day Where You Have Been in All the Rooms/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
133133
}
134134
```
135135

136+
```ts
137+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
138+
const n = nextVisit.length;
139+
const mod = 1e9 + 7;
140+
const f: number[] = new Array<number>(n).fill(0);
141+
for (let i = 1; i < n; ++i) {
142+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
143+
}
144+
return f[n - 1];
145+
}
146+
```
147+
148+
```cs
149+
public class Solution {
150+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
151+
int n = nextVisit.Length;
152+
long[] f = new long[n];
153+
int mod = (int)1e9 + 7;
154+
for (int i = 1; i < n; ++i) {
155+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
156+
}
157+
return (int)f[n - 1];
158+
}
159+
}
160+
```
161+
136162
<!-- tabs:end -->
137163

138164
<!-- end -->

solution/1900-1999/1997.First Day Where You Have Been in All the Rooms/README_EN.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,17 @@ Day 6 is the first day where you have been in all the rooms.
6363

6464
## Solutions
6565

66-
### Solution 1
66+
### Solution 1: Dynamic Programming
67+
68+
We define $f[i]$ as the date number of the first visit to the $i$-th room, so the answer is $f[n - 1]$.
69+
70+
Consider the date number of the first arrival at the $(i-1)$-th room, denoted as $f[i-1]$. At this time, it takes one day to return to the $nextVisit[i-1]$-th room. Why return? Because the problem restricts $0 \leq nextVisit[i] \leq i$.
71+
72+
After returning, the $nextVisit[i-1]$-th room is visited an odd number of times, and the rooms from $nextVisit[i-1]+1$ to $i-1$ are visited an even number of times. At this time, we go to the $(i-1)$-th room again from the $nextVisit[i-1]$-th room, which takes $f[i-1] - f[nextVisit[i-1]]$ days, and then it takes one more day to reach the $i$-th room. Therefore, $f[i] = f[i-1] + 1 + f[i-1] - f[nextVisit[i-1]] + 1$. Since $f[i]$ may be very large, we need to take the remainder of $10^9 + 7$, and to prevent negative numbers, we need to add $10^9 + 7$.
73+
74+
Finally, return $f[n-1]$.
75+
76+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of rooms.
6777

6878
<!-- tabs:start -->
6979

@@ -119,6 +129,32 @@ func firstDayBeenInAllRooms(nextVisit []int) int {
119129
}
120130
```
121131

132+
```ts
133+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
134+
const n = nextVisit.length;
135+
const mod = 1e9 + 7;
136+
const f: number[] = new Array<number>(n).fill(0);
137+
for (let i = 1; i < n; ++i) {
138+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
139+
}
140+
return f[n - 1];
141+
}
142+
```
143+
144+
```cs
145+
public class Solution {
146+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
147+
int n = nextVisit.Length;
148+
long[] f = new long[n];
149+
int mod = (int)1e9 + 7;
150+
for (int i = 1; i < n; ++i) {
151+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
152+
}
153+
return (int)f[n - 1];
154+
}
155+
}
156+
```
157+
122158
<!-- tabs:end -->
123159

124160
<!-- end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Solution {
2+
public int FirstDayBeenInAllRooms(int[] nextVisit) {
3+
int n = nextVisit.Length;
4+
long[] f = new long[n];
5+
int mod = (int)1e9 + 7;
6+
for (int i = 1; i < n; ++i) {
7+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
8+
}
9+
return (int)f[n - 1];
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function firstDayBeenInAllRooms(nextVisit: number[]): number {
2+
const n = nextVisit.length;
3+
const mod = 1e9 + 7;
4+
const f: number[] = new Array<number>(n).fill(0);
5+
for (let i = 1; i < n; ++i) {
6+
f[i] = (f[i - 1] + 1 + f[i - 1] - f[nextVisit[i - 1]] + 1 + mod) % mod;
7+
}
8+
return f[n - 1];
9+
}

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

Lines changed: 0 additions & 21 deletions
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

Lines changed: 0 additions & 21 deletions
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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 33 additions & 1 deletion
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

Lines changed: 35 additions & 1 deletion
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 -->

0 commit comments

Comments
 (0)