Skip to content

Commit 5050019

Browse files
committed
feat: add solutions to lc problems: No.1266, 1313
- No.1266.Minimum Time Visiting All Points - No.1313.Decompress Run-Length Encoded List
1 parent f689e24 commit 5050019

File tree

8 files changed

+254
-58
lines changed

8 files changed

+254
-58
lines changed

solution/1200-1299/1266.Minimum Time Visiting All Points/README.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,6 @@ class Solution {
9595
}
9696
```
9797

98-
### **TypeScript**
99-
100-
```ts
101-
function minTimeToVisitAllPoints(points: number[][]): number {
102-
let ans = 0;
103-
for (let i = 1; i < points.length; i++) {
104-
let dx = Math.abs(points[i][0] - points[i - 1][0]),
105-
dy = Math.abs(points[i][1] - points[i - 1][1]);
106-
ans += Math.max(dx, dy);
107-
}
108-
return ans;
109-
}
110-
```
111-
11298
### **C++**
11399

114100
```cpp
@@ -154,6 +140,53 @@ func abs(a int) int {
154140
}
155141
```
156142

143+
### **TypeScript**
144+
145+
```ts
146+
function minTimeToVisitAllPoints(points: number[][]): number {
147+
let ans = 0;
148+
for (let i = 1; i < points.length; i++) {
149+
let dx = Math.abs(points[i][0] - points[i - 1][0]),
150+
dy = Math.abs(points[i][1] - points[i - 1][1]);
151+
ans += Math.max(dx, dy);
152+
}
153+
return ans;
154+
}
155+
```
156+
157+
### **Rust**
158+
159+
```rust
160+
impl Solution {
161+
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
162+
let n = points.len();
163+
let mut ans = 0;
164+
for i in 1..n {
165+
let x = (points[i - 1][0] - points[i][0]).abs();
166+
let y = (points[i - 1][1] - points[i][1]).abs();
167+
ans += x.max(y);
168+
}
169+
ans
170+
}
171+
}
172+
```
173+
174+
### **C**
175+
176+
```c
177+
#define max(a, b) (((a) > (b)) ? (a) : (b))
178+
179+
int minTimeToVisitAllPoints(int **points, int pointsSize, int *pointsColSize) {
180+
int ans = 0;
181+
for (int i = 1; i < pointsSize; i++) {
182+
int x = abs(points[i - 1][0] - points[i][0]);
183+
int y = abs(points[i - 1][1] - points[i][1]);
184+
ans += max(x, y);
185+
}
186+
return ans;
187+
}
188+
```
189+
157190
### **...**
158191
159192
```

solution/1200-1299/1266.Minimum Time Visiting All Points/README_EN.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,6 @@ class Solution {
8181
}
8282
```
8383

84-
### **TypeScript**
85-
86-
```ts
87-
function minTimeToVisitAllPoints(points: number[][]): number {
88-
let ans = 0;
89-
for (let i = 1; i < points.length; i++) {
90-
let dx = Math.abs(points[i][0] - points[i - 1][0]),
91-
dy = Math.abs(points[i][1] - points[i - 1][1]);
92-
ans += Math.max(dx, dy);
93-
}
94-
return ans;
95-
}
96-
```
97-
9884
### **C++**
9985

10086
```cpp
@@ -140,6 +126,53 @@ func abs(a int) int {
140126
}
141127
```
142128

129+
### **TypeScript**
130+
131+
```ts
132+
function minTimeToVisitAllPoints(points: number[][]): number {
133+
let ans = 0;
134+
for (let i = 1; i < points.length; i++) {
135+
let dx = Math.abs(points[i][0] - points[i - 1][0]),
136+
dy = Math.abs(points[i][1] - points[i - 1][1]);
137+
ans += Math.max(dx, dy);
138+
}
139+
return ans;
140+
}
141+
```
142+
143+
### **Rust**
144+
145+
```rust
146+
impl Solution {
147+
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
148+
let n = points.len();
149+
let mut ans = 0;
150+
for i in 1..n {
151+
let x = (points[i - 1][0] - points[i][0]).abs();
152+
let y = (points[i - 1][1] - points[i][1]).abs();
153+
ans += x.max(y);
154+
}
155+
ans
156+
}
157+
}
158+
```
159+
160+
### **C**
161+
162+
```c
163+
#define max(a, b) (((a) > (b)) ? (a) : (b))
164+
165+
int minTimeToVisitAllPoints(int **points, int pointsSize, int *pointsColSize) {
166+
int ans = 0;
167+
for (int i = 1; i < pointsSize; i++) {
168+
int x = abs(points[i - 1][0] - points[i][0]);
169+
int y = abs(points[i - 1][1] - points[i][1]);
170+
ans += max(x, y);
171+
}
172+
return ans;
173+
}
174+
```
175+
143176
### **...**
144177
145178
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#define max(a, b) (((a) > (b)) ? (a) : (b))
2+
3+
int minTimeToVisitAllPoints(int **points, int pointsSize, int *pointsColSize) {
4+
int ans = 0;
5+
for (int i = 1; i < pointsSize; i++) {
6+
int x = abs(points[i - 1][0] - points[i][0]);
7+
int y = abs(points[i - 1][1] - points[i][1]);
8+
ans += max(x, y);
9+
}
10+
return ans;
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn min_time_to_visit_all_points(points: Vec<Vec<i32>>) -> i32 {
3+
let n = points.len();
4+
let mut ans = 0;
5+
for i in 1..n {
6+
let x = (points[i - 1][0] - points[i][0]).abs();
7+
let y = (points[i - 1][1] - points[i][1]).abs();
8+
ans += x.max(y);
9+
}
10+
ans
11+
}
12+
}

solution/1300-1399/1313.Decompress Run-Length Encoded List/README.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,6 @@ class Solution {
8181
}
8282
```
8383

84-
### **TypeScript**
85-
86-
```ts
87-
function decompressRLElist(nums: number[]): number[] {
88-
let n = nums.length >> 1;
89-
let ans = [];
90-
for (let i = 0; i < n; i++) {
91-
let freq = nums[2 * i],
92-
val = nums[2 * i + 1];
93-
ans.push(...new Array(freq).fill(val));
94-
}
95-
return ans;
96-
}
97-
```
98-
9984
### **C++**
10085

10186
```cpp
@@ -127,6 +112,60 @@ func decompressRLElist(nums []int) []int {
127112
}
128113
```
129114

115+
### **TypeScript**
116+
117+
```ts
118+
function decompressRLElist(nums: number[]): number[] {
119+
let n = nums.length >> 1;
120+
let ans = [];
121+
for (let i = 0; i < n; i++) {
122+
let freq = nums[2 * i],
123+
val = nums[2 * i + 1];
124+
ans.push(...new Array(freq).fill(val));
125+
}
126+
return ans;
127+
}
128+
```
129+
130+
### **Rust**
131+
132+
```rust
133+
impl Solution {
134+
pub fn decompress_rl_elist(nums: Vec<i32>) -> Vec<i32> {
135+
let n = nums.len() >> 1;
136+
let mut ans = Vec::new();
137+
for i in 0..n {
138+
for _ in 0..nums[2 * i] {
139+
ans.push(nums[2 * i + 1]);
140+
}
141+
}
142+
ans
143+
}
144+
}
145+
```
146+
147+
### **C**
148+
149+
```c
150+
/**
151+
* Note: The returned array must be malloced, assume caller calls free().
152+
*/
153+
int *decompressRLElist(int *nums, int numsSize, int *returnSize) {
154+
int size = 0;
155+
for (int i = 0; i < numsSize; i += 2) {
156+
size += nums[i];
157+
}
158+
int *ans = malloc(size * sizeof(int));
159+
for (int i = 0, j = 0; j < numsSize; j += 2) {
160+
for (int k = 0; k < nums[j]; k++) {
161+
ans[i++] = nums[j + 1];
162+
}
163+
}
164+
*returnSize = size;
165+
return ans;
166+
}
167+
```
168+
130169
### **...**
131170
132171
```

solution/1300-1399/1313.Decompress Run-Length Encoded List/README_EN.md

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,6 @@ class Solution {
7272
}
7373
```
7474

75-
### **TypeScript**
76-
77-
```ts
78-
function decompressRLElist(nums: number[]): number[] {
79-
let n = nums.length >> 1;
80-
let ans = [];
81-
for (let i = 0; i < n; i++) {
82-
let freq = nums[2 * i],
83-
val = nums[2 * i + 1];
84-
ans.push(...new Array(freq).fill(val));
85-
}
86-
return ans;
87-
}
88-
```
89-
9075
### **C++**
9176

9277
```cpp
@@ -118,6 +103,60 @@ func decompressRLElist(nums []int) []int {
118103
}
119104
```
120105

106+
### **TypeScript**
107+
108+
```ts
109+
function decompressRLElist(nums: number[]): number[] {
110+
let n = nums.length >> 1;
111+
let ans = [];
112+
for (let i = 0; i < n; i++) {
113+
let freq = nums[2 * i],
114+
val = nums[2 * i + 1];
115+
ans.push(...new Array(freq).fill(val));
116+
}
117+
return ans;
118+
}
119+
```
120+
121+
### **Rust**
122+
123+
```rust
124+
impl Solution {
125+
pub fn decompress_rl_elist(nums: Vec<i32>) -> Vec<i32> {
126+
let n = nums.len() >> 1;
127+
let mut ans = Vec::new();
128+
for i in 0..n {
129+
for _ in 0..nums[2 * i] {
130+
ans.push(nums[2 * i + 1]);
131+
}
132+
}
133+
ans
134+
}
135+
}
136+
```
137+
138+
### **C**
139+
140+
```c
141+
/**
142+
* Note: The returned array must be malloced, assume caller calls free().
143+
*/
144+
int *decompressRLElist(int *nums, int numsSize, int *returnSize) {
145+
int size = 0;
146+
for (int i = 0; i < numsSize; i += 2) {
147+
size += nums[i];
148+
}
149+
int *ans = malloc(size * sizeof(int));
150+
for (int i = 0, j = 0; j < numsSize; j += 2) {
151+
for (int k = 0; k < nums[j]; k++) {
152+
ans[i++] = nums[j + 1];
153+
}
154+
}
155+
*returnSize = size;
156+
return ans;
157+
}
158+
```
159+
121160
### **...**
122161
123162
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *decompressRLElist(int *nums, int numsSize, int *returnSize) {
5+
int size = 0;
6+
for (int i = 0; i < numsSize; i += 2) {
7+
size += nums[i];
8+
}
9+
int *ans = malloc(size * sizeof(int));
10+
for (int i = 0, j = 0; j < numsSize; j += 2) {
11+
for (int k = 0; k < nums[j]; k++) {
12+
ans[i++] = nums[j + 1];
13+
}
14+
}
15+
*returnSize = size;
16+
return ans;
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn decompress_rl_elist(nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len() >> 1;
4+
let mut ans = Vec::new();
5+
for i in 0..n {
6+
for _ in 0..nums[2 * i] {
7+
ans.push(nums[2 * i + 1]);
8+
}
9+
}
10+
ans
11+
}
12+
}

0 commit comments

Comments
 (0)