Skip to content

42. Trapping Rain Water / Hard / JavaScript #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yeongrok-jeong
Copy link
Contributor

설명

  • 양쪽 끝에서 leftright로 포인터 생성해서 시작
  • height[left]height[right]중에 작은 쪽의 포인터를 좁혀옮 (left++, right--)
  • 단, 좌측 우측에서 가장 높은 기둥의 값을 maxLeftHeight, maxRightHeight로 기억해두고
    포인터를 좁혀오면서, trap이 생긴다(= max___Height보다 낮은 기둥이 나온다)면
    max___Height에서 수직선을 그었을 때, 그 수직선으로부터 현 기둥까지의 높이차가 trap의 높이가 되므로
    그만큼을 totalWater에 더한다.
  • leftright보다 커지면 배열 전체를 확인한 것이므로 탐색을 중단하고,
    totalWater를 반환한다.

Time Complexity: O(n)

  • 포인터 두 개를 만들어서 배열을 한 번만 탐색함

Space Complexity: O(1)

  • 추가적인 공간 요하지 않음

@yeongrok-jeong yeongrok-jeong requested a review from lynn0506 May 22, 2023 17:36
@yeongrok-jeong yeongrok-jeong self-assigned this May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant