Skip to content

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