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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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/34] 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) + */ +``` + From 14bb4938a73e1348003716b7964407caf9c76154 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sat, 28 Sep 2024 08:22:54 +0530 Subject: [PATCH 26/34] Create Solution.js --- .../0641.Design Circular Deque/Solution.js | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 solution/0600-0699/0641.Design Circular Deque/Solution.js diff --git a/solution/0600-0699/0641.Design Circular Deque/Solution.js b/solution/0600-0699/0641.Design Circular Deque/Solution.js new file mode 100644 index 0000000000000..1e8d146872040 --- /dev/null +++ b/solution/0600-0699/0641.Design Circular Deque/Solution.js @@ -0,0 +1,97 @@ +/** + * @param {number} k + */ +var MyCircularDeque = function(k) { + this.size = k; + this.deque = new Array(k); + this.front = 0; + this.rear = 0; + this.count = 0; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertFront = function(value) { + if (this.isFull()) return false; + this.front = (this.front - 1 + this.size) % this.size; + this.deque[this.front] = value; + this.count++; + return true; +}; + +/** + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertLast = function(value) { + if (this.isFull()) return false; + this.deque[this.rear] = value; + this.rear = (this.rear + 1) % this.size; + this.count++; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteFront = function() { + if (this.isEmpty()) return false; + this.front = (this.front + 1) % this.size; + this.count--; + return true; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.deleteLast = function() { + if (this.isEmpty()) return false; + this.rear = (this.rear - 1 + this.size) % this.size; + this.count--; + return true; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getFront = function() { + if (this.isEmpty()) return -1; + return this.deque[this.front]; +}; + +/** + * @return {number} + */ +MyCircularDeque.prototype.getRear = function() { + if (this.isEmpty()) return -1; + return this.deque[(this.rear - 1 + this.size) % this.size]; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isEmpty = function() { + return this.count === 0; +}; + +/** + * @return {boolean} + */ +MyCircularDeque.prototype.isFull = function() { + return this.count === this.size; +}; + +/** + * Your MyCircularDeque object will be instantiated and called as such: + * var obj = new MyCircularDeque(k) + * var param_1 = obj.insertFront(value) + * var param_2 = obj.insertLast(value) + * var param_3 = obj.deleteFront() + * var param_4 = obj.deleteLast() + * var param_5 = obj.getFront() + * var param_6 = obj.getRear() + * var param_7 = obj.isEmpty() + * var param_8 = obj.isFull() + */ From fc6e992498c5477ed52e90ecab217855f46ed76d Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sat, 28 Sep 2024 08:37:40 +0530 Subject: [PATCH 27/34] Create Solution2.js --- .../Solution2.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js diff --git a/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js new file mode 100644 index 0000000000000..7ebb3a8d91a15 --- /dev/null +++ b/solution/1500-1599/1502.Can Make Arithmetic Progression From Sequence/Solution2.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} arr + * @return {boolean} + */ +var canMakeArithmeticProgression = function(arr) { + arr.sort((a,b)=>a-b) + + let val = arr[1] - arr [0] + + for(let i = 1 ; i < arr.length-1 ; i++){ + if(arr[i+1]-arr[i] !== val){ + return false + } + } + + return true +}; From 6d9e16234f2ec933fb4a47233ec64cefaf0fbb69 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sat, 28 Sep 2024 08:51:21 +0530 Subject: [PATCH 28/34] Create Solution.js --- .../0000-0099/0045.Jump Game II/Solution.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 solution/0000-0099/0045.Jump Game II/Solution.js diff --git a/solution/0000-0099/0045.Jump Game II/Solution.js b/solution/0000-0099/0045.Jump Game II/Solution.js new file mode 100644 index 0000000000000..5ab291e61126c --- /dev/null +++ b/solution/0000-0099/0045.Jump Game II/Solution.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var jump = function(nums) { + let jumps = 0; + let currentEnd = 0; + let farthest = 0; + + for (let i = 0; i < nums.length - 1; i++) { + farthest = Math.max(farthest, i + nums[i]); + + if (i === currentEnd) { + jumps++; + currentEnd = farthest; + } + } + + return jumps; +}; From e221a037b132019df1c94b6219c49ac007696e20 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sat, 28 Sep 2024 13:17:02 +0530 Subject: [PATCH 29/34] Create Solution.js --- .../0119.Pascal's Triangle II/Solution.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0100-0199/0119.Pascal's Triangle II/Solution.js diff --git a/solution/0100-0199/0119.Pascal's Triangle II/Solution.js b/solution/0100-0199/0119.Pascal's Triangle II/Solution.js new file mode 100644 index 0000000000000..079c79ffea45a --- /dev/null +++ b/solution/0100-0199/0119.Pascal's Triangle II/Solution.js @@ -0,0 +1,14 @@ +/** + * @param {number} rowIndex + * @return {number[]} + */ +var getRow = function(rowIndex) { + const f = Array(rowIndex + 1).fill(1); + for (let i = 2; i < rowIndex + 1; ++i) { + for (let j = i - 1; j > 0; --j) { + f[j] += f[j - 1]; + } + } + return f; + +}; From 4937851ff7763c14282bcb548da894687be10d82 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sun, 29 Sep 2024 07:50:19 +0530 Subject: [PATCH 30/34] Create Solution.js --- .../0432.All O`one Data Structure/Solution.js | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 solution/0400-0499/0432.All O`one Data Structure/Solution.js diff --git a/solution/0400-0499/0432.All O`one Data Structure/Solution.js b/solution/0400-0499/0432.All O`one Data Structure/Solution.js new file mode 100644 index 0000000000000..fb0c68c88e683 --- /dev/null +++ b/solution/0400-0499/0432.All O`one Data Structure/Solution.js @@ -0,0 +1,98 @@ +class Node { + constructor(key = '', cnt = 0) { + this.prev = null; + this.next = null; + this.cnt = cnt; + this.keys = new Set([key]); + } + + insert(node) { + node.prev = this; + node.next = this.next; + this.next.prev = node; + this.next = node; + return node; + } + + remove() { + this.prev.next = this.next; + this.next.prev = this.prev; + } +} + +var AllOne = function() { + this.root = new Node(); + this.root.next = this.root; + this.root.prev = this.root; + this.nodes = {}; +}; + +AllOne.prototype.inc = function(key) { + const root = this.root; + const nodes = this.nodes; + + if (!nodes[key]) { + if (root.next === root || root.next.cnt > 1) { + nodes[key] = root.insert(new Node(key, 1)); + } else { + root.next.keys.add(key); + nodes[key] = root.next; + } + } else { + const curr = nodes[key]; + const next = curr.next; + + if (next === root || next.cnt > curr.cnt + 1) { + nodes[key] = curr.insert(new Node(key, curr.cnt + 1)); + } else { + next.keys.add(key); + nodes[key] = next; + } + + curr.keys.delete(key); + if (curr.keys.size === 0) { + curr.remove(); + } + } +}; + +AllOne.prototype.dec = function(key) { + const root = this.root; + const nodes = this.nodes; + const curr = nodes[key]; + + if (curr.cnt === 1) { + delete nodes[key]; + } else { + const prev = curr.prev; + + if (prev === root || prev.cnt < curr.cnt - 1) { + nodes[key] = prev.insert(new Node(key, curr.cnt - 1)); + } else { + prev.keys.add(key); + nodes[key] = prev; + } + } + + curr.keys.delete(key); + if (curr.keys.size === 0) { + curr.remove(); + } +}; + +AllOne.prototype.getMaxKey = function() { + return this.root.prev === this.root ? '' : Array.from(this.root.prev.keys)[0]; +}; + +AllOne.prototype.getMinKey = function() { + return this.root.next === this.root ? '' : Array.from(this.root.next.keys)[0]; +}; + +/** + * Your AllOne object will be instantiated and called as such: + * var obj = new AllOne() + * obj.inc(key) + * obj.dec(key) + * var param_3 = obj.getMaxKey() + * var param_4 = obj.getMinKey() + */ From 59a5861dfe7f229d003bb5f7c8352c20910e70d7 Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:34:05 +0530 Subject: [PATCH 31/34] Create Solution.js --- solution/0200-0299/0274.H-Index/Solution.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 solution/0200-0299/0274.H-Index/Solution.js diff --git a/solution/0200-0299/0274.H-Index/Solution.js b/solution/0200-0299/0274.H-Index/Solution.js new file mode 100644 index 0000000000000..cae05d6579048 --- /dev/null +++ b/solution/0200-0299/0274.H-Index/Solution.js @@ -0,0 +1,13 @@ +/** + * @param {number[]} citations + * @return {number} + */ +var hIndex = function(citations) { + citations.sort((a, b) => b - a); + for (let h = citations.length; h; --h) { + if (citations[h - 1] >= h) { + return h; + } + } + return 0; +}; From a839b545a0e06cd5b3179ceba6efa09370fabf6b Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 30 Sep 2024 08:09:10 +0530 Subject: [PATCH 32/34] Create Solution.js --- .../Solution.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js diff --git a/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js new file mode 100644 index 0000000000000..87e3963d3df46 --- /dev/null +++ b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.js @@ -0,0 +1,52 @@ +/** + * @param {number} maxSize + */ +var CustomStack = function(maxSize) { + this.stk = Array(maxSize).fill(0); + this.add = Array(maxSize).fill(0); + this.i = 0; +}; + +/** + * @param {number} x + * @return {void} + */ +CustomStack.prototype.push = function(x) { + if (this.i < this.stk.length) { + this.stk[this.i++] = x; + } +}; + +/** + * @return {number} + */ +CustomStack.prototype.pop = function() { + if (this.i <= 0) { + return -1; + } + const ans = this.stk[--this.i] + this.add[this.i]; + if (this.i > 0) { + this.add[this.i - 1] += this.add[this.i]; + } + this.add[this.i] = 0; + return ans; +}; + +/** + * @param {number} k + * @param {number} val + * @return {void} + */ +CustomStack.prototype.increment = function(k, val) { + if (this.i > 0) { + this.add[Math.min(this.i, k) - 1] += val; + } +}; + +/** + * Your CustomStack object will be instantiated and called as such: + * var obj = new CustomStack(maxSize) + * obj.push(x) + * var param_2 = obj.pop() + * obj.increment(k,val) + */ From d9c4058f21446b02f83c2c1465d52bf02e0a369e Mon Sep 17 00:00:00 2001 From: 07subhadip <91666506+07subhadip@users.noreply.github.com> Date: Mon, 30 Sep 2024 08:16:01 +0530 Subject: [PATCH 33/34] Create Solution.js --- .../Solution.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js new file mode 100644 index 0000000000000..ac34f7a9b19a9 --- /dev/null +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.js @@ -0,0 +1,48 @@ +var RandomizedSet = function() { + this.d = new Map(); + this.q = []; +}; + +/** + * @param {number} val + * @return {boolean} + */ +RandomizedSet.prototype.insert = function(val) { + if (this.d.has(val)) { + return false; + } + this.d.set(val, this.q.length); + this.q.push(val); + return true; +}; + +/** + * @param {number} val + * @return {boolean} + */ +RandomizedSet.prototype.remove = function(val) { + if (!this.d.has(val)) { + return false; + } + const i = this.d.get(val); + this.d.set(this.q[this.q.length - 1], i); + this.q[i] = this.q[this.q.length - 1]; + this.q.pop(); + this.d.delete(val); + return true; +}; + +/** + * @return {number} + */ +RandomizedSet.prototype.getRandom = function() { + return this.q[Math.floor(Math.random() * this.q.length)]; +}; + +/** + * Your RandomizedSet object will be instantiated and called as such: + * var obj = new RandomizedSet() + * var param_1 = obj.insert(val) + * var param_2 = obj.remove(val) + * var param_3 = obj.getRandom() + */ From 4c7b66d96019cdf33e91accbe2c4634fcc4f0ac2 Mon Sep 17 00:00:00 2001 From: Subhadip Hensh <91666506+07subhadip@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:06:28 +0530 Subject: [PATCH 34/34] Create Solution.js --- .../Solution.js | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js new file mode 100644 index 0000000000000..02031e84633c6 --- /dev/null +++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.js @@ -0,0 +1,101 @@ +/** + * @param {number[][]} times + * @param {number} targetFriend + * @return {number} + */ +var smallestChair = function(times, targetFriend) { + const n = times.length; + + const availableChairs = new MinHeap(); + + const busy = new MinHeap((a, b) => a[0] - b[0]); + + for (let i = 0; i < n; ++i) { + times[i].push(i); + availableChairs.push(i); + } + + times.sort((a, b) => a[0] - b[0]); + + for (let t of times) { + const arrival = t[0], leaving = t[1], friendIndex = t[2]; + + while (!busy.isEmpty() && busy.peek()[0] <= arrival) { + availableChairs.push(busy.pop()[1]); + } + + const chair = availableChairs.pop(); + + if (friendIndex === targetFriend) { + return chair; + } + + busy.push([leaving, chair]); + } + + return -1; +}; + +class MinHeap { + constructor(compare = (a, b) => a - b) { + this.heap = []; + this.compare = compare; + } + + push(val) { + this.heap.push(val); + this.bubbleUp(this.heap.length - 1); + } + + pop() { + const top = this.heap[0]; + const bottom = this.heap.pop(); + if (this.heap.length > 0) { + this.heap[0] = bottom; + this.bubbleDown(0); + } + return top; + } + + peek() { + return this.heap[0]; + } + + isEmpty() { + return this.heap.length === 0; + } + + bubbleUp(index) { + while (index > 0) { + const parentIndex = Math.floor((index - 1) / 2); + if (this.compare(this.heap[index], this.heap[parentIndex]) < 0) { + [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]]; + index = parentIndex; + } else { + break; + } + } + } + + bubbleDown(index) { + const length = this.heap.length; + while (true) { + const leftIndex = 2 * index + 1; + const rightIndex = 2 * index + 2; + let smallest = index; + + if (leftIndex < length && this.compare(this.heap[leftIndex], this.heap[smallest]) < 0) { + smallest = leftIndex; + } + if (rightIndex < length && this.compare(this.heap[rightIndex], this.heap[smallest]) < 0) { + smallest = rightIndex; + } + if (smallest !== index) { + [this.heap[index], this.heap[smallest]] = [this.heap[smallest], this.heap[index]]; + index = smallest; + } else { + break; + } + } + } +}