(5) Container With Most Water - LeetCode
Container With Most Water - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
접근 : 투포인터
부르트포스로 풀면 시간초과 발생
투포인터를 활용해 문제풀이
Javascript
let input = require("fs")
.readFileSync("input.txt") //"/dev/stdin"
.toString()
.split("\n")
.map((val) => val.trim());
function solution(input) {
//input
let height = input[0].split(" ").map((v) => +v);
//Submit
let left = 0;
let right = height.length - 1;
let max = -Infinity;
while (left < right) {
let width = right - left;
let vertical = Math.min(height[left], height[right]);
let water = width * vertical;
max = Math.max(max, water);
if (height[left] > height[right]) right--;
else left++;
}
console.log(max);
}
solution(input);
'코딩테스트(알고리즘) > leetCode' 카테고리의 다른 글
[leetCode] 3. Longest Substring Without Repeating Characters (Javascript) (0) | 2022.06.26 |
---|---|
[leetCode] 125. Valid Palindrome (Javascript) (0) | 2022.06.25 |
[leetCode] 1. Two Sum (Javascript) (0) | 2022.06.25 |
[leetCode] 13.Roman to Integer (Javascript) (0) | 2022.06.24 |
[leetCode] 6.Zigzag Conversion (Javascript) (0) | 2022.06.24 |