From 72b7ca48bdf33645fced3675add21f900bc0c744 Mon Sep 17 00:00:00 2001 From: Felix Kwan Date: Thu, 12 Oct 2023 23:44:11 +0800 Subject: [PATCH] New javascript solution for problem 11 --- README.md | 2 +- .../containerWithMostWater.js | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 algorithms/javascript/containerWithMostWater/containerWithMostWater.js diff --git a/README.md b/README.md index 2bf93af3..76d5db72 100644 --- a/README.md +++ b/README.md @@ -551,7 +551,7 @@ LeetCode |14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [C++](./algorithms/cpp/longestCommonPrefix/longestCommonPrefix.cpp)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [C++](./algorithms/cpp/romanToInteger/romanToInteger.cpp)|Easy| |12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)| [C++](./algorithms/cpp/integerToRoman/integerToRoman.cpp)|Medium| -|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [C++](./algorithms/cpp/containerWithMostWater/containerWithMostWater.cpp), [Java](./algorithms/java/src/containerwithmostwater.java)|Medium| +|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [C++](./algorithms/cpp/containerWithMostWater/containerWithMostWater.cpp), [Java](./algorithms/java/src/containerwithmostwater.java), [Javascript](./algorithms/javascript/containerwithmostwater.js)|Medium| |10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [C++](./algorithms/cpp/regularExpressionMatching/regularExpressionMatching.cpp)|Hard| |9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)| [C++](./algorithms/cpp/palindromeNumber/palindromeNumber.cpp), [Java](./algorithms/java/src/palindromeNumber/PalindromeNumber.java)|Easy| |8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)| [C++](./algorithms/cpp/stringToIntegerAtoi/stringToIntegerAtoi.cpp)|Easy| diff --git a/algorithms/javascript/containerWithMostWater/containerWithMostWater.js b/algorithms/javascript/containerWithMostWater/containerWithMostWater.js new file mode 100644 index 00000000..f108e915 --- /dev/null +++ b/algorithms/javascript/containerWithMostWater/containerWithMostWater.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} height + * @return {number} + */ +var maxArea = function(height) { + let i = 0; + let j = height.length - 1; + let result = Number.MIN_SAFE_INTEGER; + + while (i < j) { + const volume = Math.min(height[i], height[j]) * (j - i); + result = Math.max(result, volume); + + if (height[i] > height[j]) { + j--; + } else { + i++; + } + } + + return result; +}; \ No newline at end of file