forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
12 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/Solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/Solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/Solution.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |