diff --git a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README.md b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README.md index a0d5762c4848e..7b04601f3ef24 100644 --- a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README.md +++ b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README.md @@ -114,6 +114,31 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {string} s + * @return {boolean} + */ + var checkZeroOnes = function(s) { + let max0 = 0, max1 = 0; + let t0 = 0, t1 = 0; + for (let char of s) { + if (char == '0') { + t0++; + t1 = 0; + } else { + t1++; + t0 = 0; + } + max0 = Math.max(max0, t0); + max1 = Math.max(max1, t1); + } + return max1 > max0; +}; +``` + ### **...** ``` diff --git a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README_EN.md b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README_EN.md index d353bf2289068..f3594508c13ee 100644 --- a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README_EN.md +++ b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/README_EN.md @@ -100,6 +100,31 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {string} s + * @return {boolean} + */ + var checkZeroOnes = function(s) { + let max0 = 0, max1 = 0; + let t0 = 0, t1 = 0; + for (let char of s) { + if (char == '0') { + t0++; + t1 = 0; + } else { + t1++; + t0 = 0; + } + max0 = Math.max(max0, t0); + max1 = Math.max(max1, t1); + } + return max1 > max0; +}; +``` + ### **...** ``` diff --git a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.js b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.js new file mode 100644 index 0000000000000..eca8b1ba27495 --- /dev/null +++ b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.js @@ -0,0 +1,20 @@ +/** + * @param {string} s + * @return {boolean} + */ + var checkZeroOnes = function(s) { + let max0 = 0, max1 = 0; + let t0 = 0, t1 = 0; + for (let char of s) { + if (char == '0') { + t0++; + t1 = 0; + } else { + t1++; + t0 = 0; + } + max0 = Math.max(max0, t0); + max1 = Math.max(max1, t1); + } + return max1 > max0; +}; \ No newline at end of file diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md index 17d46867c3785..7344ea56c1570 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README.md @@ -129,6 +129,42 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} dist + * @param {number} hour + * @return {number} + */ + var minSpeedOnTime = function(dist, hour) { + if (dist.length > Math.ceil(hour)) return -1; + let left = 1, right = 10 ** 7; + while (left < right) { + let mid = (left + right) >> 1; + if (arriveOnTime(dist, mid, hour)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + }; + + function arriveOnTime (dist, speed, hour) { + let res = 0.0; + let n = dist.length; + for (let i = 0; i < n; i++) { + let cost = parseFloat(dist[i]) / speed; + if (i != n - 1) { + cost = Math.ceil(cost); + } + res += cost; + } + return res <= hour; + } +``` + ### **...** ``` diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md index 43c8cc89a5261..da161a0e8d4a5 100644 --- a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/README_EN.md @@ -117,6 +117,42 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} dist + * @param {number} hour + * @return {number} + */ + var minSpeedOnTime = function(dist, hour) { + if (dist.length > Math.ceil(hour)) return -1; + let left = 1, right = 10 ** 7; + while (left < right) { + let mid = (left + right) >> 1; + if (arriveOnTime(dist, mid, hour)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + }; + + function arriveOnTime (dist, speed, hour) { + let res = 0.0; + let n = dist.length; + for (let i = 0; i < n; i++) { + let cost = parseFloat(dist[i]) / speed; + if (i != n - 1) { + cost = Math.ceil(cost); + } + res += cost; + } + return res <= hour; + } +``` + ### **...** ``` diff --git a/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.js b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.js new file mode 100644 index 0000000000000..57abc500b22e7 --- /dev/null +++ b/solution/1800-1899/1870.Minimum Speed to Arrive on Time/Solution.js @@ -0,0 +1,31 @@ +/** + * @param {number[]} dist + * @param {number} hour + * @return {number} + */ + var minSpeedOnTime = function(dist, hour) { + if (dist.length > Math.ceil(hour)) return -1; + let left = 1, right = 10 ** 7; + while (left < right) { + let mid = (left + right) >> 1; + if (arriveOnTime(dist, mid, hour)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + }; + + function arriveOnTime (dist, speed, hour) { + let res = 0.0; + let n = dist.length; + for (let i = 0; i < n; i++) { + let cost = parseFloat(dist[i]) / speed; + if (i != n - 1) { + cost = Math.ceil(cost); + } + res += cost; + } + return res <= hour; + } \ No newline at end of file