Problem Statement
You are about to go hiking. The trail consists of n segments. Some segments go uphill, others are level, and the remaining ones go downhill.
You are given a
You are concerned about long uphill climbs. Return the largest number of consecutive trail segments that go uphill.
Definition
- Class:
- HillClimber
- Method:
- longest
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int longest(int[] height)
- (be sure your method is public)
Constraints
- height will contain between 2 and 50 elements, inclusive.
- Each element of height will be between 0 and 10000, inclusive.
Examples
{ 1, 2, 3, 2, 2 }
Returns: 2
At the beginning of this trail there are two consecutive segments of uphill hiking: first we go from altitude 1 to altitude 2, and then we go from altitude 2 to altitude 3.
{ 1, 2, 2, 3 }
Returns: 1
This trail has two uphill segments (1-2 and 2-3) but they are not consecutive.
{ 1, 8, 9, 12 }
Returns: 3
The altitudes that correspond to the answer don't necessarily have to form a proper arithmetic sequence. As long as they are increasing, it is an uphill hike.
{ 10, 4, 4, 2 }
Returns: 0
We never actually have any uphill segments here.
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
Returns: 9
{ 10, 8, 4, 9, 11, 14, 15, 3, 7, 8, 10, 6 }
Returns: 4
{5322,3732,5525,7249,1308,5761,9512,247,9167,8918,8474,1249,8681,9622,6047,4126,5141,9868,2702,5702,7723,8803,3188,9332,3288,6587,6854,1473,7332,2735,7015,8761,5723,2579,4938,5575,4152,4703,7747,819,3962,793,6}
Returns: 3
{379,9698,367,8081,9877,7134,855,575,7798,6401,771,6520,544,6193,3961,4722,8908,8038,2916,6432,1903,9714,5526,6102,4684,471,9975,437,2121,1882,1739}
Returns: 2
{8797,7125,8953,3590,6220,8773,1597,197,1880,6440,962,9323,1807,8418,5031,7960,742,3024,1912,5925,7877,3574,4161,665,4171,4265,3397,9405,7160,417,8454,5916,4661,3841,7723}
Returns: 2
{7240,370,8503,134,6440,2718,4551,5463,8751,6204,4294,6266,5441,2014,9695,9097,9970,5814}
Returns: 3
{5222,2964,8288,9419,1954,5376,2193,3038,1462,7986,5046,4661,7967,695,1611,720,372,4522,1778,9486,5159,9024,8698,1914,7968,9972,7132,3963,5146,722,9358,5946,8847,4789,2501,455,8953,951,6549,4759,4247,9681,3089,7477,3704,1542}
Returns: 2
{5570,3829,1294,9323,4574,8192,6329,3893,6199,4266,6373,1451,1982,8244,8230,7892,739,8629,9101,5814,3177,8195,1155,1768,9422,7974,7019,5239,5479,5934,1220,8211,6426}
Returns: 2
{1561,133,3852,1400,4783,7754,2518,895,6302,6190,2580,9501,4566,6515,6017,1788,2349,8849,4604}
Returns: 2
{3150,7376,2673,9527,9901,6601,1173,1090,8414,264,3045,8162,5025,8338,6683,8096,4040,9340,3845,1346,3296,2282,1892,4966,9188,9922,3407,504,9222,3691}
Returns: 3
{2372,3606,697,494,4185,7460,4326,1499,8183,6581,1427,2481,6037,3144,4418,4475,6228,9832,7921}
Returns: 4
{3010,5004,3258,2431,868,5484,7708,338,9980,3194,668,1868,4723,6680,673,4556,4328,7844,2498,636,9715,7840,5663,4765,1868,9801,5855,1822,4770,5447,7179,672,1301,5943,574,7369,348,8266,9626}
Returns: 3
{9753,3987,1520,4989,5912,9560,7759,9358,2471,9862,147,8385,7334,7285,8034,5171,9341,8493,562,1067,9629,5885,5568,4533,6015,9997,567,8780,6105,3305,4208,6062,8218,3796,883,5727,6320,674,8853,2309,2042,6498,9351,4909}
Returns: 3
{8795,3338,4017,7910,5404,8028,1855,7703,8559,9073,3867,8420,6987,8974,5916,8992,8979,3252,9167,5654,4085,166,3537,6402,2306,8851,8667}
Returns: 3
{8254,9025,3130,6534,659,9234,7220,6659,6550,869,700,5277,2352,8359,9660,4724,7016,7308,9333,1700,2937,3318,3063,3246,184,9255,1214,8629,9801,7616,5604,6554,9361,2216,9450,9534,5943,7334,7760,7753,1754,9258,2441,8110,2551,3889,3302,5727,506}
Returns: 3
{9386,4999,9518,6967,6703,4936,5962,9885,6350,1404,8020,8592,6858,8935,5266,4147,5917,136,8909,125,8782,9683,9548,1580,9947,3679,876,4954,694,6296,5953,5487,8122,986,8230,605,7036,2394,5513,4514,3572,6058,983,6355,9734,3163,2399,3507,2843,8404}
Returns: 2
{3165,6819,380,32,9704,5451,7938,7020,3023,4059,1007,9190,54,5452,2533,469,5871,705,4660,7112,3522,4623,4394,6554,2723,8699,3192,1846,6784,1280,7370,2110,2290,8323,2787,8669,7741,954,8526,5154,9098,5374,2490,454,2564,6868,929,903,7201,2693}
Returns: 2
{2801,8183,1292,4450,8543,2424,3827,7310,1005,2258,6092,236,1743,6690,8897,6899,1731,2664,3864,2728,4917,2179,9506,8015,8149,2133,3939,3990,5855,7770,2289,4646,4090,4751,4568,3927,998,3873,4245,6244,3914,3645,7521,2896,3006,5137,746,3764,3667,9969}
Returns: 4
{8239,3336,9966,1612,622,8677,3278,6302,6409,8485,9837,1454,3235,9970,1445,6147,6992,2122,4969,5901,7858,8419,7621,8814,1272,4658,6610,5008,3011,1386,175,322,5886,8572,4050,1309,4124,3139,3945,5251,7721,7572,7837,7759,4077,2190,4286,2927,1560,8991}
Returns: 4
{1642,8698,4068,1340,9820,5715,1151,340,5965,2067,8270,3290,5022,8310,9515,5568,4664,5919,135,2022,3853,8178,9719,8180,286,582,9862,4736,9082,372,3683,3447,4020,7632,8534,1625,1817,7232,4230,5595,2972,2798,7981,3183,9452,4281,2814,9450,4563,6622}
Returns: 4
{1,193,333,361,500,698,767,946,1063,1084,1268,1385,1513,1538,1649,1816,1912,2097,2244,2439,2596,2671,2819,2852,2942,3123,3144,3147,3277,3451,3571,3726,3734,3737,3786,3801,3934,3974,4115,4269,4370,4422,4540,4663,4762,4956,5087,5117,5164,5259}
Returns: 49
{10000,9908,9808,9700,9507,9374,9317,9274,9168,9031,8873,8697,8632,8584,8441,8341,8312,8204,8080,7905,7809,7775,7720,7676,7483,7389,7285,7129,7104,6971,6876,6827,6637,6481,6447,6366,6347,6301,6287,6218,6027,5856,5829,5727,5545,5368,5332,5328,5153,5131}
Returns: 0
{1, 2, 1, 3 }
Returns: 1
{1, 2, 3 }
Returns: 2
{1, 2, 3, 4 }
Returns: 3
{1, 2, 3, 2, 2, 5, 6, 1, 2, 3, 4 }
Returns: 3
{0, 0 }
Returns: 0
{1, 2, 3, 4, 5 }
Returns: 4
{1, 2, 1, 2, 3 }
Returns: 2
{1, 8, 9, 12 }
Returns: 3
{1, 2, 3, 1, 2, 1, 2, 3, 4, 5 }
Returns: 4
{1, 2, 3, 4, 5, 0, 20 }
Returns: 4
{1, 2, 3, 2, 2 }
Returns: 2
{1, 2, 3, 4, 3, 4, 5, 6 }
Returns: 3
{1, 2, 2, 3 }
Returns: 1