Skip to content

Commit

Permalink
feat: add js solutions to lc problems: No.2490~2493
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zhaocchen committed Dec 4, 2022
1 parent 6cecb7f commit 5810fbf
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 0 deletions.
19 changes: 19 additions & 0 deletions solution/2400-2499/2490.Circular Sentence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,25 @@ func isCircularSentence(sentence string) bool {
}
```

### **JavaScript**

```js
var isCircularSentence = function(sentence) {
const words = sentence.split(' ');
const post = words[0].charCodeAt(0);
let prev = words[0].charCodeAt(words[0].length - 1);
const n = words.length;
for (let i = 1; i < n; i++) {
let cur = words[i];
if (cur.charCodeAt(0) !== prev) {
return false;
}
prev = cur.charCodeAt(cur.length - 1);
}
return post === prev;
};
```

### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions solution/2400-2499/2490.Circular Sentence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ func isCircularSentence(sentence string) bool {
}
```

### **JavaScript**

```js
var isCircularSentence = function(sentence) {
const words = sentence.split(' ');
const post = words[0].charCodeAt(0);
let prev = words[0].charCodeAt(words[0].length - 1);
const n = words.length;
for (let i = 1; i < n; i++) {
let cur = words[i];
if (cur.charCodeAt(0) !== prev) {
return false;
}
prev = cur.charCodeAt(cur.length - 1);
}
return post === prev;
};
```

### **...**

```
Expand Down
14 changes: 14 additions & 0 deletions solution/2400-2499/2490.Circular Sentence/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var isCircularSentence = function(sentence) {
const words = sentence.split(' ');
const post = words[0].charCodeAt(0);
let prev = words[0].charCodeAt(words[0].length - 1);
const n = words.length;
for (let i = 1; i < n; i++) {
let cur = words[i];
if (cur.charCodeAt(0) !== prev) {
return false;
}
prev = cur.charCodeAt(cur.length - 1);
}
return post === prev;
};
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ func dividePlayers(skill []int) (ans int64) {
}
```

### **JavaScript**

```js
var dividePlayers = function(skill) {
const n = skill.length, m = n / 2;
skill.sort((a, b) => a - b);
const sum = skill[0] + skill[n - 1];
let ans = 0;
for (let i = 0; i < m; i++) {
const x = skill[i], y = skill[n - 1 - i];
if (x + y != sum) return -1;
ans += x * y;
}
return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ func dividePlayers(skill []int) (ans int64) {
}
```

### **JavaScript**

```js
var dividePlayers = function(skill) {
const n = skill.length, m = n / 2;
skill.sort((a, b) => a - b);
const sum = skill[0] + skill[n - 1];
let ans = 0;
for (let i = 0; i < m; i++) {
const x = skill[i], y = skill[n - 1 - i];
if (x + y != sum) return -1;
ans += x * y;
}
return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var dividePlayers = function(skill) {
const n = skill.length, m = n / 2;
skill.sort((a, b) => a - b);
const sum = skill[0] + skill[n - 1];
let ans = 0;
for (let i = 0; i < m; i++) {
const x = skill[i], y = skill[n - 1 - i];
if (x + y != sum) return -1;
ans += x * y;
}
return ans;
};
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ func min(a, b int) int {
}
```

### **JavaScript**

```js
var minScore = function(n, roads) {
// 构建点到点的映射表
const graph = Array.from({length: n + 1}, () => new Map());
for (let [u, v, w] of roads) {
graph[u].set(v, w);
graph[v].set(u, w);
}

// DFS
const vis = new Array(n).fill(false);
let ans = Infinity;
var dfs = function (u) {
vis[u] = true;
for (const [v, w] of graph[u]) {
ans = Math.min(ans, w);
if (!vis[v]) dfs(v);
}
}
dfs(1);

return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,33 @@ func min(a, b int) int {
}
```

### **JavaScript**

```js
var minScore = function(n, roads) {
// 构建点到点的映射表
const graph = Array.from({length: n + 1}, () => new Map());
for (let [u, v, w] of roads) {
graph[u].set(v, w);
graph[v].set(u, w);
}

// DFS
const vis = new Array(n).fill(false);
let ans = Infinity;
var dfs = function (u) {
vis[u] = true;
for (const [v, w] of graph[u]) {
ans = Math.min(ans, w);
if (!vis[v]) dfs(v);
}
}
dfs(1);

return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var minScore = function(n, roads) {
// 构建点到点的映射表
const graph = Array.from({length: n + 1}, () => new Map());
for (let [u, v, w] of roads) {
graph[u].set(v, w);
graph[v].set(u, w);
}

// DFS
const vis = new Array(n).fill(false);
let ans = Infinity;
var dfs = function (u) {
vis[u] = true;
for (const [v, w] of graph[u]) {
ans = Math.min(ans, w);
if (!vis[v]) dfs(v);
}
}
dfs(1);

return ans;
};
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,51 @@ func abs(x int) int {
}
```

### **JavaScript**

```js
var magnificentSets = function(n, edges) {
const graph = Array.from({length: n + 1}, () => new Set());
for (const [u, v] of edges) {
graph[u].add(v);
graph[v].add(u);
}
const hash = new Map();

// 2. BFS
for (let i = 1; i <= n; i++) {
let queue = [i];
const dis = Array(n + 1).fill(0);
dis[i] = 1;
let mx = 1, mn = n;
while (queue.length) {
let next = [];
for (let u of queue) {
mn = Math.min(mn, u);
for (const v of graph[u]) {
if (!dis[v]) {
dis[v] = dis[u] + 1;
mx = Math.max(mx, dis[v]);
next.push(v);
}
if (Math.abs(dis[u] - dis[v]) != 1) {
return -1;
}
}
}
queue = next;
}
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
}

let ans = 0;
for (const [u, v] of hash) {
ans += v;
}
return ans;
};
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,52 @@ func abs(x int) int {
}
```

### **JavaScript**

```js
var magnificentSets = function(n, edges) {
const graph = Array.from({length: n + 1}, () => new Set());
for (const [u, v] of edges) {
graph[u].add(v);
graph[v].add(u);
}
const hash = new Map();

// 2. BFS
for (let i = 1; i <= n; i++) {
let queue = [i];
const dis = Array(n + 1).fill(0);
dis[i] = 1;
let mx = 1, mn = n;
while (queue.length) {
let next = [];
for (let u of queue) {
mn = Math.min(mn, u);
for (const v of graph[u]) {
if (!dis[v]) {
dis[v] = dis[u] + 1;
mx = Math.max(mx, dis[v]);
next.push(v);
}
if (Math.abs(dis[u] - dis[v]) != 1) {
return -1;
}
}
}
queue = next;
}
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
}

let ans = 0;
for (const [u, v] of hash) {
ans += v;
}
return ans;
};
```


### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var magnificentSets = function(n, edges) {
const graph = Array.from({length: n + 1}, () => new Set());
for (const [u, v] of edges) {
graph[u].add(v);
graph[v].add(u);
}
const hash = new Map();

// 2. BFS
for (let i = 1; i <= n; i++) {
let queue = [i];
const dis = Array(n + 1).fill(0);
dis[i] = 1;
let mx = 1, mn = n;
while (queue.length) {
let next = [];
for (let u of queue) {
mn = Math.min(mn, u);
for (const v of graph[u]) {
if (!dis[v]) {
dis[v] = dis[u] + 1;
mx = Math.max(mx, dis[v]);
next.push(v);
}
if (Math.abs(dis[u] - dis[v]) != 1) {
return -1;
}
}
}
queue = next;
}
hash.set(mn, Math.max(mx, (hash.get(mn) || 0)));
}

let ans = 0;
for (const [u, v] of hash) {
ans += v;
}
return ans;
};

0 comments on commit 5810fbf

Please sign in to comment.