From a21b1e8eb4b3ce85bb30c47467693ddf15cc1b37 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:37:18 +0530 Subject: [PATCH 01/25] Create Solution.js --- solution/Solution.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/Solution.js diff --git a/solution/Solution.js b/solution/Solution.js new file mode 100644 index 0000000000000..bc6eefb4efe2b --- /dev/null +++ b/solution/Solution.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function(s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = new Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.substring(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; From 324cc2b6c4d59e6dba76c3db790ecbd91ad0dafd Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:40:57 +0530 Subject: [PATCH 02/25] Create Solution.js --- .../Solution.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/2700-2799/2707.Extra Characters in a String/Solution.js diff --git a/solution/2700-2799/2707.Extra Characters in a String/Solution.js b/solution/2700-2799/2707.Extra Characters in a String/Solution.js new file mode 100644 index 0000000000000..bc6eefb4efe2b --- /dev/null +++ b/solution/2700-2799/2707.Extra Characters in a String/Solution.js @@ -0,0 +1,19 @@ +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function(s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = new Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.substring(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; From 80634a89a3aca30e6204332eab638bf0eff2362c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:22:47 +0800 Subject: [PATCH 03/25] Update Solution.js --- .../2707.Extra Characters in a String/Solution.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/2700-2799/2707.Extra Characters in a String/Solution.js b/solution/2700-2799/2707.Extra Characters in a String/Solution.js index bc6eefb4efe2b..078d05aab4f5f 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/Solution.js +++ b/solution/2700-2799/2707.Extra Characters in a String/Solution.js @@ -3,17 +3,17 @@ * @param {string[]} dictionary * @return {number} */ -var minExtraChar = function(s, dictionary) { +var minExtraChar = function (s, dictionary) { const ss = new Set(dictionary); const n = s.length; - const f = new Array(n + 1).fill(0); + const f = Array(n + 1).fill(0); for (let i = 1; i <= n; ++i) { f[i] = f[i - 1] + 1; for (let j = 0; j < i; ++j) { - if (ss.has(s.substring(j, i))) { + if (ss.has(s.slice(j, i))) { f[i] = Math.min(f[i], f[j]); } } } - return f[n]; + return f[n]; }; From 3680e8bf99babad73e9bac11b67764332e375a28 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:23:05 +0800 Subject: [PATCH 04/25] Delete solution/Solution.js --- solution/Solution.js | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 solution/Solution.js diff --git a/solution/Solution.js b/solution/Solution.js deleted file mode 100644 index bc6eefb4efe2b..0000000000000 --- a/solution/Solution.js +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @param {string} s - * @param {string[]} dictionary - * @return {number} - */ -var minExtraChar = function(s, dictionary) { - const ss = new Set(dictionary); - const n = s.length; - const f = new Array(n + 1).fill(0); - for (let i = 1; i <= n; ++i) { - f[i] = f[i - 1] + 1; - for (let j = 0; j < i; ++j) { - if (ss.has(s.substring(j, i))) { - f[i] = Math.min(f[i], f[j]); - } - } - } - return f[n]; -}; From 7a181216edc3dce766dbe4b95f2dd024cdfefdc1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:23:49 +0800 Subject: [PATCH 05/25] Update README.md --- .../README.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/solution/2700-2799/2707.Extra Characters in a String/README.md b/solution/2700-2799/2707.Extra Characters in a String/README.md index b2cff7fea73de..825e7da5463e6 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/README.md +++ b/solution/2700-2799/2707.Extra Characters in a String/README.md @@ -210,6 +210,30 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function (s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.slice(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; +``` + From 46a2880377f0b6d4f0b9ec021893456cf4b14a80 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 08:24:08 +0800 Subject: [PATCH 06/25] Update README_EN.md --- .../README_EN.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/solution/2700-2799/2707.Extra Characters in a String/README_EN.md b/solution/2700-2799/2707.Extra Characters in a String/README_EN.md index 13eb8c12b9d59..777bf14ca7383 100644 --- a/solution/2700-2799/2707.Extra Characters in a String/README_EN.md +++ b/solution/2700-2799/2707.Extra Characters in a String/README_EN.md @@ -211,6 +211,30 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {string} s + * @param {string[]} dictionary + * @return {number} + */ +var minExtraChar = function (s, dictionary) { + const ss = new Set(dictionary); + const n = s.length; + const f = Array(n + 1).fill(0); + for (let i = 1; i <= n; ++i) { + f[i] = f[i - 1] + 1; + for (let j = 0; j < i; ++j) { + if (ss.has(s.slice(j, i))) { + f[i] = Math.min(f[i], f[j]); + } + } + } + return f[n]; +}; +``` + From f4825dbb3d698ef1c9e47f2c7cc8a5f861fb0d93 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:34:48 +0530 Subject: [PATCH 07/25] Create Solution.js --- .../Solution.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js new file mode 100644 index 0000000000000..301d9fdbc553b --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -0,0 +1,24 @@ +var longestCommonPrefix = function(arr1, arr2) { + let set = new Set(); + + for (let x of arr1) { + while (x > 0) { + set.add(x); + x = Math.floor(x / 10); + } + } + + let ans = 0; + + for (let x of arr2) { + while (x > 0) { + if (set.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + break; + } + x = Math.floor(x / 10); + } + } + + return ans; +}; From 8c24319aace1c1d40bacdb113b95b5af884ccded Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Tue, 24 Sep 2024 10:36:25 +0530 Subject: [PATCH 08/25] Update Solution.ts --- .../Solution.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts index 88c02857fb7fe..153db70971f04 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -1,17 +1,24 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { - const s: Set = new Set(); + let set = new Set(); + for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + while (x > 0) { + set.add(x); + x = Math.floor(x / 10); } } - let ans: number = 0; + + let ans = 0; + for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + while (x > 0) { + if (set.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + break; } + x = Math.floor(x / 10); } } + return ans; } From 9223b1f51bdaf11619b29c3ea418d858c96cf089 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 24 Sep 2024 10:44:29 +0000 Subject: [PATCH 09/25] style: format code and docs with prettier --- .../Solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js index 301d9fdbc553b..abae6f729d795 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -1,4 +1,4 @@ -var longestCommonPrefix = function(arr1, arr2) { +var longestCommonPrefix = function (arr1, arr2) { let set = new Set(); for (let x of arr1) { From fa6703870dcd493e874b5289713805c03437a00f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:48:02 +0800 Subject: [PATCH 10/25] Update Solution.ts --- .../Solution.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts index 153db70971f04..f66c7309c52b2 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.ts @@ -1,24 +1,17 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { - let set = new Set(); - + const s: Set = new Set(); for (let x of arr1) { - while (x > 0) { - set.add(x); - x = Math.floor(x / 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } - - let ans = 0; - + let ans: number = 0; for (let x of arr2) { - while (x > 0) { - if (set.has(x)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); - break; } - x = Math.floor(x / 10); } } - return ans; } From cc15596492c4ed4f41c52cd79059f7e510b863c5 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:48:56 +0800 Subject: [PATCH 11/25] Update Solution.js --- .../Solution.js | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js index abae6f729d795..86762c5539377 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/Solution.js @@ -1,24 +1,22 @@ +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ var longestCommonPrefix = function (arr1, arr2) { - let set = new Set(); - + const s = new Set(); for (let x of arr1) { - while (x > 0) { - set.add(x); - x = Math.floor(x / 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } - let ans = 0; - for (let x of arr2) { - while (x > 0) { - if (set.has(x)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); - break; } - x = Math.floor(x / 10); } } - return ans; }; From 313f6b500334b0cb6ec37b220508b714ab982f4b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:50:05 +0800 Subject: [PATCH 12/25] Update README.md --- .../README.md | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md index b4d7746119489..a501e904cf3be 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md @@ -176,14 +176,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -192,6 +192,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` + From 63ef10d701f1f4ab872127ddb36361987ac285d4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 24 Sep 2024 18:50:17 +0800 Subject: [PATCH 13/25] Update README_EN.md --- .../README_EN.md | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md index 1ff77d43c30de..6dab65a426ad3 100644 --- a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md @@ -174,14 +174,14 @@ func longestCommonPrefix(arr1 []int, arr2 []int) (ans int) { function longestCommonPrefix(arr1: number[], arr2: number[]): number { const s: Set = new Set(); for (let x of arr1) { - for (; x; x = (x / 10) | 0) { - s.add(x % 10); + for (; x; x = Math.floor(x / 10)) { + s.add(x); } } let ans: number = 0; for (let x of arr2) { - for (; x; x = (x / 10) | 0) { - if (s.has(x % 10)) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); } } @@ -190,6 +190,33 @@ function longestCommonPrefix(arr1: number[], arr2: number[]): number { } ``` +#### JavaScript + +```js +/** + * @param {number[]} arr1 + * @param {number[]} arr2 + * @return {number} + */ +var longestCommonPrefix = function (arr1, arr2) { + const s = new Set(); + for (let x of arr1) { + for (; x; x = Math.floor(x / 10)) { + s.add(x); + } + } + let ans = 0; + for (let x of arr2) { + for (; x; x = Math.floor(x / 10)) { + if (s.has(x)) { + ans = Math.max(ans, Math.floor(Math.log10(x)) + 1); + } + } + } + return ans; +}; +``` + From 6ae183cda19d168adf9a45b9519538c67ae0bff8 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Wed, 25 Sep 2024 07:10:35 +0530 Subject: [PATCH 14/25] Create Solution.js --- .../Solution.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js new file mode 100644 index 0000000000000..f52323c48ba4e --- /dev/null +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js @@ -0,0 +1,42 @@ +class Trie { + constructor() { + this.children = {}; + this.cnt = 0; + } + + insert(w) { + let node = this; + for (const c of w) { + if (!node.children[c]) { + node.children[c] = new Trie(); + } + node = node.children[c]; + node.cnt++; + } + } + + search(w) { + let node = this; + let ans = 0; + for (const c of w) { + if (!node.children[c]) { + return ans; + } + node = node.children[c]; + ans += node.cnt; + } + return ans; + } +} + +/** + * @param {string[]} words + * @return {number[]} + */ +var sumPrefixScores = function(words) { + const trie = new Trie(); + for (const w of words) { + trie.insert(w); + } + return words.map(w => trie.search(w)); +}; From f77f588e5e0434d6cd6ee0d1fe1673df67be4e35 Mon Sep 17 00:00:00 2001 From: 07subhadip <07subhadip@users.noreply.github.com> Date: Wed, 25 Sep 2024 01:47:23 +0000 Subject: [PATCH 15/25] style: format code and docs with prettier --- .../2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js index f52323c48ba4e..944fd9f535995 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/Solution.js @@ -33,7 +33,7 @@ class Trie { * @param {string[]} words * @return {number[]} */ -var sumPrefixScores = function(words) { +var sumPrefixScores = function (words) { const trie = new Trie(); for (const w of words) { trie.insert(w); From 1b9ab6003dcdfe7de9b8f9daaadbd054e9c8c818 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 25 Sep 2024 10:05:11 +0800 Subject: [PATCH 16/25] Update README.md --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md index e0a0756216139..1274cc2d19475 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README.md @@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] { } ``` +#### JavaScript + +```js +class Trie { + constructor() { + this.children = {}; + this.cnt = 0; + } + + insert(w) { + let node = this; + for (const c of w) { + if (!node.children[c]) { + node.children[c] = new Trie(); + } + node = node.children[c]; + node.cnt++; + } + } + + search(w) { + let node = this; + let ans = 0; + for (const c of w) { + if (!node.children[c]) { + return ans; + } + node = node.children[c]; + ans += node.cnt; + } + return ans; + } +} + +/** + * @param {string[]} words + * @return {number[]} + */ +var sumPrefixScores = function (words) { + const trie = new Trie(); + for (const w of words) { + trie.insert(w); + } + return words.map(w => trie.search(w)); +}; +``` + From 8b4d469cc2d9f74f17e4f6349344a9fe802dd939 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 25 Sep 2024 10:05:26 +0800 Subject: [PATCH 17/25] Update README_EN.md --- .../README_EN.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md index 808ab3f98788c..e5cb5b9c96fc9 100644 --- a/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md +++ b/solution/2400-2499/2416.Sum of Prefix Scores of Strings/README_EN.md @@ -337,6 +337,53 @@ function sumPrefixScores(words: string[]): number[] { } ``` +#### JavaScript + +```js +class Trie { + constructor() { + this.children = {}; + this.cnt = 0; + } + + insert(w) { + let node = this; + for (const c of w) { + if (!node.children[c]) { + node.children[c] = new Trie(); + } + node = node.children[c]; + node.cnt++; + } + } + + search(w) { + let node = this; + let ans = 0; + for (const c of w) { + if (!node.children[c]) { + return ans; + } + node = node.children[c]; + ans += node.cnt; + } + return ans; + } +} + +/** + * @param {string[]} words + * @return {number[]} + */ +var sumPrefixScores = function (words) { + const trie = new Trie(); + for (const w of words) { + trie.insert(w); + } + return words.map(w => trie.search(w)); +}; +``` + From cb0c193f3c5cfe5ce4f733dd9c9ba52eba82af4e Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:08:08 +0530 Subject: [PATCH 18/25] Create Solution.js --- .../0700-0799/0729.My Calendar I/Solution.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 solution/0700-0799/0729.My Calendar I/Solution.js diff --git a/solution/0700-0799/0729.My Calendar I/Solution.js b/solution/0700-0799/0729.My Calendar I/Solution.js new file mode 100644 index 0000000000000..b2d8afb2ca9fb --- /dev/null +++ b/solution/0700-0799/0729.My Calendar I/Solution.js @@ -0,0 +1,26 @@ + +var MyCalendar = function() { + this.calendar = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendar.prototype.book = function(start, end) { + for (const item of this.calendar) { + if (end <= item[0] || item[1] <= start) { + continue; + } + return false; + } + this.calendar.push([start, end]); + return true; +}; + +/** + * Your MyCalendar object will be instantiated and called as such: + * var obj = new MyCalendar() + * var param_1 = obj.book(start,end) + */ From a4b0a17c5a2b70a52f9df8d9e3bd1864705eb4b5 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 26 Sep 2024 06:04:09 +0000 Subject: [PATCH 19/25] style: format code and docs with prettier --- solution/0700-0799/0729.My Calendar I/Solution.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solution/0700-0799/0729.My Calendar I/Solution.js b/solution/0700-0799/0729.My Calendar I/Solution.js index b2d8afb2ca9fb..a8be4d933e83b 100644 --- a/solution/0700-0799/0729.My Calendar I/Solution.js +++ b/solution/0700-0799/0729.My Calendar I/Solution.js @@ -1,14 +1,13 @@ - -var MyCalendar = function() { +var MyCalendar = function () { this.calendar = []; }; -/** - * @param {number} start +/** + * @param {number} start * @param {number} end * @return {boolean} */ -MyCalendar.prototype.book = function(start, end) { +MyCalendar.prototype.book = function (start, end) { for (const item of this.calendar) { if (end <= item[0] || item[1] <= start) { continue; @@ -19,7 +18,7 @@ MyCalendar.prototype.book = function(start, end) { return true; }; -/** +/** * Your MyCalendar object will be instantiated and called as such: * var obj = new MyCalendar() * var param_1 = obj.book(start,end) From a5b3b015d5c60986973dc016617edfea8f40707f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 26 Sep 2024 14:07:16 +0800 Subject: [PATCH 20/25] Update README.md --- .../0700-0799/0729.My Calendar I/README.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/solution/0700-0799/0729.My Calendar I/README.md b/solution/0700-0799/0729.My Calendar I/README.md index f825b3610b8fe..eab8a561dfad9 100644 --- a/solution/0700-0799/0729.My Calendar I/README.md +++ b/solution/0700-0799/0729.My Calendar I/README.md @@ -256,6 +256,36 @@ impl MyCalendar { } ``` +#### JavaScript + +```js +var MyCalendar = function () { + this.calendar = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendar.prototype.book = function (start, end) { + for (const item of this.calendar) { + if (end <= item[0] || item[1] <= start) { + continue; + } + return false; + } + this.calendar.push([start, end]); + return true; +}; + +/** + * Your MyCalendar object will be instantiated and called as such: + * var obj = new MyCalendar() + * var param_1 = obj.book(start,end) + */ +``` + From 4316b1e64e5d926db94457e896b756126d0c17c6 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 26 Sep 2024 14:07:30 +0800 Subject: [PATCH 21/25] Update README_EN.md --- .../0700-0799/0729.My Calendar I/README_EN.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/solution/0700-0799/0729.My Calendar I/README_EN.md b/solution/0700-0799/0729.My Calendar I/README_EN.md index 5f3dce96c5243..124a9044aa47b 100644 --- a/solution/0700-0799/0729.My Calendar I/README_EN.md +++ b/solution/0700-0799/0729.My Calendar I/README_EN.md @@ -254,6 +254,36 @@ impl MyCalendar { } ``` +#### JavaScript + +```js +var MyCalendar = function () { + this.calendar = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendar.prototype.book = function (start, end) { + for (const item of this.calendar) { + if (end <= item[0] || item[1] <= start) { + continue; + } + return false; + } + this.calendar.push([start, end]); + return true; +}; + +/** + * Your MyCalendar object will be instantiated and called as such: + * var obj = new MyCalendar() + * var param_1 = obj.book(start,end) + */ +``` + From f5ef0732a196c95f4bd53402eb04fb5b87fc1e8c Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Fri, 27 Sep 2024 09:19:55 +0530 Subject: [PATCH 22/25] Create Solution.js --- .../0700-0799/0731.My Calendar II/Solution.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 solution/0700-0799/0731.My Calendar II/Solution.js diff --git a/solution/0700-0799/0731.My Calendar II/Solution.js b/solution/0700-0799/0731.My Calendar II/Solution.js new file mode 100644 index 0000000000000..33fc78dabb0ba --- /dev/null +++ b/solution/0700-0799/0731.My Calendar II/Solution.js @@ -0,0 +1,33 @@ + +var MyCalendarTwo = function() { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function(start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ From 5ac3684ccd9fe34706090fb19cd6b77930aff1fc Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 27 Sep 2024 04:45:08 +0000 Subject: [PATCH 23/25] style: format code and docs with prettier --- solution/0700-0799/0731.My Calendar II/Solution.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/solution/0700-0799/0731.My Calendar II/Solution.js b/solution/0700-0799/0731.My Calendar II/Solution.js index 33fc78dabb0ba..53cfad370e6f7 100644 --- a/solution/0700-0799/0731.My Calendar II/Solution.js +++ b/solution/0700-0799/0731.My Calendar II/Solution.js @@ -1,15 +1,14 @@ - -var MyCalendarTwo = function() { +var MyCalendarTwo = function () { this.events = []; this.overlaps = []; }; -/** - * @param {number} start +/** + * @param {number} start * @param {number} end * @return {boolean} */ -MyCalendarTwo.prototype.book = function(start, end) { +MyCalendarTwo.prototype.book = function (start, end) { for (let [s, e] of this.overlaps) { if (Math.max(start, s) < Math.min(end, e)) { return false; @@ -26,7 +25,7 @@ MyCalendarTwo.prototype.book = function(start, end) { return true; }; -/** +/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(start,end) From 6a9029086245cdbad687ea9a3f5b743188e2309d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 27 Sep 2024 12:48:34 +0800 Subject: [PATCH 24/25] Update README.md --- .../0700-0799/0731.My Calendar II/README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/solution/0700-0799/0731.My Calendar II/README.md b/solution/0700-0799/0731.My Calendar II/README.md index 1971961290950..04d8becf7d485 100644 --- a/solution/0700-0799/0731.My Calendar II/README.md +++ b/solution/0700-0799/0731.My Calendar II/README.md @@ -208,6 +208,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function (start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ +``` + From 59d7872ab80772e3bf03bc8cbc8fe16f96b70031 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 27 Sep 2024 12:48:48 +0800 Subject: [PATCH 25/25] Update README_EN.md --- .../0731.My Calendar II/README_EN.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/solution/0700-0799/0731.My Calendar II/README_EN.md b/solution/0700-0799/0731.My Calendar II/README_EN.md index 5d3abb9bcf791..b3f664980ff95 100644 --- a/solution/0700-0799/0731.My Calendar II/README_EN.md +++ b/solution/0700-0799/0731.My Calendar II/README_EN.md @@ -206,6 +206,43 @@ func (this *MyCalendarTwo) Book(start int, end int) bool { */ ``` +#### JavaScript + +```js +var MyCalendarTwo = function () { + this.events = []; + this.overlaps = []; +}; + +/** + * @param {number} start + * @param {number} end + * @return {boolean} + */ +MyCalendarTwo.prototype.book = function (start, end) { + for (let [s, e] of this.overlaps) { + if (Math.max(start, s) < Math.min(end, e)) { + return false; + } + } + + for (let [s, e] of this.events) { + if (Math.max(start, s) < Math.min(end, e)) { + this.overlaps.push([Math.max(start, s), Math.min(end, e)]); + } + } + + this.events.push([start, end]); + return true; +}; + +/** + * Your MyCalendarTwo object will be instantiated and called as such: + * var obj = new MyCalendarTwo() + * var param_1 = obj.book(start,end) + */ +``` +