From c539d36bf7e666157667393c8ea4dc2480a27b9d Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Mon, 17 May 2021 23:41:31 +0800 Subject: [PATCH] feat: add javascript solution to lcci problem: No.1864.Minimum Number of Swaps to Make the Binary String Alternating --- .../README.md | 33 +++++++++++++++++++ .../README_EN.md | 33 +++++++++++++++++++ .../Solution.js | 28 ++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/Solution.js diff --git a/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README.md b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README.md index 0765c73638f03..3dc41a67d57d3 100644 --- a/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README.md +++ b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README.md @@ -69,6 +69,39 @@ ``` +### **JavaScript** + +```js +/** + * @param {string} s + * @return {number} + */ + var minSwaps = function(s) { + let n = s.length; + let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0); + let n0 = n - n1; + let count = Infinity; + let half = n / 2; + // 101、1010 + if (n1 == Math.ceil(half) && n0 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '1') cur++; + } + count = Math.min(count, cur); + } + // 010、0101 + if (n0 == Math.ceil(half) && n1 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '0') cur++; + } + count = Math.min(count, cur); + } + return count == Infinity ? -1 : count; +}; +``` + ### **...** ``` diff --git a/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README_EN.md b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README_EN.md index 86d4544d60727..b23a313f7e225 100644 --- a/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README_EN.md +++ b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/README_EN.md @@ -60,6 +60,39 @@ The string is now alternating. ``` +### **JavaScript** + +```js +/** + * @param {string} s + * @return {number} + */ + var minSwaps = function(s) { + let n = s.length; + let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0); + let n0 = n - n1; + let count = Infinity; + let half = n / 2; + // 101、1010 + if (n1 == Math.ceil(half) && n0 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '1') cur++; + } + count = Math.min(count, cur); + } + // 010、0101 + if (n0 == Math.ceil(half) && n1 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '0') cur++; + } + count = Math.min(count, cur); + } + return count == Infinity ? -1 : count; +}; +``` + ### **...** ``` diff --git a/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/Solution.js b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/Solution.js new file mode 100644 index 0000000000000..5ec0aee156a7c --- /dev/null +++ b/solution/1800-1899/1864.Minimum Number of Swaps to Make the Binary String Alternating/Solution.js @@ -0,0 +1,28 @@ +/** + * @param {string} s + * @return {number} + */ + var minSwaps = function(s) { + let n = s.length; + let n1 = [...s].reduce((a, c) => parseInt(c) + a, 0); + let n0 = n - n1; + let count = Infinity; + let half = n / 2; + // 101、1010 + if (n1 == Math.ceil(half) && n0 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '1') cur++; + } + count = Math.min(count, cur); + } + // 010、0101 + if (n0 == Math.ceil(half) && n1 == Math.floor(half)) { + let cur = 0; + for (let i = 0; i < n; i++) { + if (i % 2 == 0 && s.charAt(i) != '0') cur++; + } + count = Math.min(count, cur); + } + return count == Infinity ? -1 : count; +}; \ No newline at end of file