diff --git a/solution/2400-2499/2490.Circular Sentence/README.md b/solution/2400-2499/2490.Circular Sentence/README.md index d2e942fca5d0a..d89864816f3df 100644 --- a/solution/2400-2499/2490.Circular Sentence/README.md +++ b/solution/2400-2499/2490.Circular Sentence/README.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2490.Circular Sentence/README_EN.md b/solution/2400-2499/2490.Circular Sentence/README_EN.md index 00bf2ea3d907c..ed4ede2d2fffd 100644 --- a/solution/2400-2499/2490.Circular Sentence/README_EN.md +++ b/solution/2400-2499/2490.Circular Sentence/README_EN.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2490.Circular Sentence/Solution.js b/solution/2400-2499/2490.Circular Sentence/Solution.js new file mode 100644 index 0000000000000..f3b0eb9bf2a97 --- /dev/null +++ b/solution/2400-2499/2490.Circular Sentence/Solution.js @@ -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; +}; \ No newline at end of file diff --git a/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md index 3d49d28f2a9b1..d47b1a5ddf145 100644 --- a/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md +++ b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md index 7d33c82c077f3..dd7f57ff358cf 100644 --- a/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md +++ b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/README_EN.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/Solution.js b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/Solution.js new file mode 100644 index 0000000000000..2e35b37383d14 --- /dev/null +++ b/solution/2400-2499/2491.Divide Players Into Teams of Equal Skill/Solution.js @@ -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; +}; \ No newline at end of file diff --git a/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README.md b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README.md index a9831dd69f27c..1f578bce120dc 100644 --- a/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README.md +++ b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md index 3b78865773639..1e586fa07f3a3 100644 --- a/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md +++ b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/README_EN.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/Solution.js b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/Solution.js new file mode 100644 index 0000000000000..d76c2f0224c8e --- /dev/null +++ b/solution/2400-2499/2492.Minimum Score of a Path Between Two Cities/Solution.js @@ -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; +}; \ No newline at end of file diff --git a/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md index b6984ebbc12fd..0e8b142465eeb 100644 --- a/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md +++ b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README.md @@ -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; +}; +``` + ### **...** ``` diff --git a/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md index 4f834e9c4bc5d..e4d15b89e4263 100644 --- a/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md +++ b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/README_EN.md @@ -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; +}; +``` + + ### **...** ``` diff --git a/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/Solution.js b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/Solution.js new file mode 100644 index 0000000000000..2be990f615328 --- /dev/null +++ b/solution/2400-2499/2493.Divide Nodes Into the Maximum Number of Groups/Solution.js @@ -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; +}; \ No newline at end of file