Skip to content

Commit 5810fbf

Browse files
committed
feat: add js solutions to lc problems: No.2490~2493
* No.2490.Circular Sentence * No.2491.Divide Players Into Teams of Equal Skill * No.2492.Minimum Score of a Path Between Two Cities * No.2493.Divide Nodes Into the Maximum Number of Groups
1 parent 6cecb7f commit 5810fbf

File tree

12 files changed

+305
-0
lines changed

12 files changed

+305
-0
lines changed

solution/2400-2499/2490.Circular Sentence/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ func isCircularSentence(sentence string) bool {
150150
}
151151
```
152152

153+
### **JavaScript**
154+
155+
```js
156+
var isCircularSentence = function(sentence) {
157+
const words = sentence.split(' ');
158+
const post = words[0].charCodeAt(0);
159+
let prev = words[0].charCodeAt(words[0].length - 1);
160+
const n = words.length;
161+
for (let i = 1; i < n; i++) {
162+
let cur = words[i];
163+
if (cur.charCodeAt(0) !== prev) {
164+
return false;
165+
}
166+
prev = cur.charCodeAt(cur.length - 1);
167+
}
168+
return post === prev;
169+
};
170+
```
171+
153172
### **...**
154173

155174
```

solution/2400-2499/2490.Circular Sentence/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ func isCircularSentence(sentence string) bool {
134134
}
135135
```
136136

137+
### **JavaScript**
138+
139+
```js
140+
var isCircularSentence = function(sentence) {
141+
const words = sentence.split(' ');
142+
const post = words[0].charCodeAt(0);
143+
let prev = words[0].charCodeAt(words[0].length - 1);
144+
const n = words.length;
145+
for (let i = 1; i < n; i++) {
146+
let cur = words[i];
147+
if (cur.charCodeAt(0) !== prev) {
148+
return false;
149+
}
150+
prev = cur.charCodeAt(cur.length - 1);
151+
}
152+
return post === prev;
153+
};
154+
```
155+
137156
### **...**
138157

139158
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var isCircularSentence = function(sentence) {
2+
const words = sentence.split(' ');
3+
const post = words[0].charCodeAt(0);
4+
let prev = words[0].charCodeAt(words[0].length - 1);
5+
const n = words.length;
6+
for (let i = 1; i < n; i++) {
7+
let cur = words[i];
8+
if (cur.charCodeAt(0) !== prev) {
9+
return false;
10+
}
11+
prev = cur.charCodeAt(cur.length - 1);
12+
}
13+
return post === prev;
14+
};

solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ func dividePlayers(skill []int) (ans int64) {
146146
}
147147
```
148148

149+
### **JavaScript**
150+
151+
```js
152+
var dividePlayers = function(skill) {
153+
const n = skill.length, m = n / 2;
154+
skill.sort((a, b) => a - b);
155+
const sum = skill[0] + skill[n - 1];
156+
let ans = 0;
157+
for (let i = 0; i < m; i++) {
158+
const x = skill[i], y = skill[n - 1 - i];
159+
if (x + y != sum) return -1;
160+
ans += x * y;
161+
}
162+
return ans;
163+
};
164+
```
165+
149166
### **...**
150167

151168
```

solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ func dividePlayers(skill []int) (ans int64) {
126126
}
127127
```
128128

129+
### **JavaScript**
130+
131+
```js
132+
var dividePlayers = function(skill) {
133+
const n = skill.length, m = n / 2;
134+
skill.sort((a, b) => a - b);
135+
const sum = skill[0] + skill[n - 1];
136+
let ans = 0;
137+
for (let i = 0; i < m; i++) {
138+
const x = skill[i], y = skill[n - 1 - i];
139+
if (x + y != sum) return -1;
140+
ans += x * y;
141+
}
142+
return ans;
143+
};
144+
```
145+
129146
### **...**
130147

131148
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var dividePlayers = function(skill) {
2+
const n = skill.length, m = n / 2;
3+
skill.sort((a, b) => a - b);
4+
const sum = skill[0] + skill[n - 1];
5+
let ans = 0;
6+
for (let i = 0; i < m; i++) {
7+
const x = skill[i], y = skill[n - 1 - i];
8+
if (x + y != sum) return -1;
9+
ans += x * y;
10+
}
11+
return ans;
12+
};

solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,33 @@ func min(a, b int) int {
329329
}
330330
```
331331

332+
### **JavaScript**
333+
334+
```js
335+
var minScore = function(n, roads) {
336+
// 构建点到点的映射表
337+
const graph = Array.from({length: n + 1}, () => new Map());
338+
for (let [u, v, w] of roads) {
339+
graph[u].set(v, w);
340+
graph[v].set(u, w);
341+
}
342+
343+
// DFS
344+
const vis = new Array(n).fill(false);
345+
let ans = Infinity;
346+
var dfs = function (u) {
347+
vis[u] = true;
348+
for (const [v, w] of graph[u]) {
349+
ans = Math.min(ans, w);
350+
if (!vis[v]) dfs(v);
351+
}
352+
}
353+
dfs(1);
354+
355+
return ans;
356+
};
357+
```
358+
332359
### **...**
333360

334361
```

solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,33 @@ func min(a, b int) int {
303303
}
304304
```
305305

306+
### **JavaScript**
307+
308+
```js
309+
var minScore = function(n, roads) {
310+
// 构建点到点的映射表
311+
const graph = Array.from({length: n + 1}, () => new Map());
312+
for (let [u, v, w] of roads) {
313+
graph[u].set(v, w);
314+
graph[v].set(u, w);
315+
}
316+
317+
// DFS
318+
const vis = new Array(n).fill(false);
319+
let ans = Infinity;
320+
var dfs = function (u) {
321+
vis[u] = true;
322+
for (const [v, w] of graph[u]) {
323+
ans = Math.min(ans, w);
324+
if (!vis[v]) dfs(v);
325+
}
326+
}
327+
dfs(1);
328+
329+
return ans;
330+
};
331+
```
332+
306333
### **...**
307334

308335
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var minScore = function(n, roads) {
2+
// 构建点到点的映射表
3+
const graph = Array.from({length: n + 1}, () => new Map());
4+
for (let [u, v, w] of roads) {
5+
graph[u].set(v, w);
6+
graph[v].set(u, w);
7+
}
8+
9+
// DFS
10+
const vis = new Array(n).fill(false);
11+
let ans = Infinity;
12+
var dfs = function (u) {
13+
vis[u] = true;
14+
for (const [v, w] of graph[u]) {
15+
ans = Math.min(ans, w);
16+
if (!vis[v]) dfs(v);
17+
}
18+
}
19+
dfs(1);
20+
21+
return ans;
22+
};

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,51 @@ func abs(x int) int {
366366
}
367367
```
368368

369+
### **JavaScript**
370+
371+
```js
372+
var magnificentSets = function(n, edges) {
373+
const graph = Array.from({length: n + 1}, () => new Set());
374+
for (const [u, v] of edges) {
375+
graph[u].add(v);
376+
graph[v].add(u);
377+
}
378+
const hash = new Map();
379+
380+
// 2. BFS
381+
for (let i = 1; i <= n; i++) {
382+
let queue = [i];
383+
const dis = Array(n + 1).fill(0);
384+
dis[i] = 1;
385+
let mx = 1, mn = n;
386+
while (queue.length) {
387+
let next = [];
388+
for (let u of queue) {
389+
mn = Math.min(mn, u);
390+
for (const v of graph[u]) {
391+
if (!dis[v]) {
392+
dis[v] = dis[u] + 1;
393+
mx = Math.max(mx, dis[v]);
394+
next.push(v);
395+
}
396+
if (Math.abs(dis[u] - dis[v]) != 1) {
397+
return -1;
398+
}
399+
}
400+
}
401+
queue = next;
402+
}
403+
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
404+
}
405+
406+
let ans = 0;
407+
for (const [u, v] of hash) {
408+
ans += v;
409+
}
410+
return ans;
411+
};
412+
```
413+
369414
### **...**
370415

371416
```

solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md

+46
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,52 @@ func abs(x int) int {
348348
}
349349
```
350350

351+
### **JavaScript**
352+
353+
```js
354+
var magnificentSets = function(n, edges) {
355+
const graph = Array.from({length: n + 1}, () => new Set());
356+
for (const [u, v] of edges) {
357+
graph[u].add(v);
358+
graph[v].add(u);
359+
}
360+
const hash = new Map();
361+
362+
// 2. BFS
363+
for (let i = 1; i <= n; i++) {
364+
let queue = [i];
365+
const dis = Array(n + 1).fill(0);
366+
dis[i] = 1;
367+
let mx = 1, mn = n;
368+
while (queue.length) {
369+
let next = [];
370+
for (let u of queue) {
371+
mn = Math.min(mn, u);
372+
for (const v of graph[u]) {
373+
if (!dis[v]) {
374+
dis[v] = dis[u] + 1;
375+
mx = Math.max(mx, dis[v]);
376+
next.push(v);
377+
}
378+
if (Math.abs(dis[u] - dis[v]) != 1) {
379+
return -1;
380+
}
381+
}
382+
}
383+
queue = next;
384+
}
385+
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
386+
}
387+
388+
let ans = 0;
389+
for (const [u, v] of hash) {
390+
ans += v;
391+
}
392+
return ans;
393+
};
394+
```
395+
396+
351397
### **...**
352398

353399
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var magnificentSets = function(n, edges) {
2+
const graph = Array.from({length: n + 1}, () => new Set());
3+
for (const [u, v] of edges) {
4+
graph[u].add(v);
5+
graph[v].add(u);
6+
}
7+
const hash = new Map();
8+
9+
// 2. BFS
10+
for (let i = 1; i <= n; i++) {
11+
let queue = [i];
12+
const dis = Array(n + 1).fill(0);
13+
dis[i] = 1;
14+
let mx = 1, mn = n;
15+
while (queue.length) {
16+
let next = [];
17+
for (let u of queue) {
18+
mn = Math.min(mn, u);
19+
for (const v of graph[u]) {
20+
if (!dis[v]) {
21+
dis[v] = dis[u] + 1;
22+
mx = Math.max(mx, dis[v]);
23+
next.push(v);
24+
}
25+
if (Math.abs(dis[u] - dis[v]) != 1) {
26+
return -1;
27+
}
28+
}
29+
}
30+
queue = next;
31+
}
32+
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
33+
}
34+
35+
let ans = 0;
36+
for (const [u, v] of hash) {
37+
ans += v;
38+
}
39+
return ans;
40+
};

0 commit comments

Comments
 (0)