id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

bryce.hulett@hotmail.com > water
prev  |  next  |  chance

Write a method named water that accepts an array of vertical bar heights as its parameter and returns the largest rectangular area of water that could be held in a contiguous region between those bars. For example, if the bar heights are {2, 9, 6, 2, 4, 5, 8, 3, 7, 4}, the bars could be thought of as follows:

  #
  #         #
  #         #   #
  # #       #   #
  # #     # #   #
  # #   # # #   # #
  # #   # # # # # #
# # # # # # # # # #
# # # # # # # # # #
2 9 6 2 4 5 8 3 7 4
The largest area of water that can be made within these bars has an area of 49 (7*7 for height*(idx 8 - idx 1)-assuming the region between was empty).

If the array contains fewer than 2 bars, return 0.

water([2, 9, 6, 2, 4, 5, 8, 3, 7, 4]) → 49
water([2, 1, 5, 6, 2, 1, 1]) → 8
water([1, 4, 5, 1, 6, 5, 2, 7, 2, 1, 1, 2]) → 25

...Save, Compile, Run (ctrl-enter)

public int water(int[] arr){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy