In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use C# to achieve the container that holds the most water". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The container that holds the most water
Difficulty: medium
I will give you n non-negative integers, a _ 1, a _ 2, and a _ 1, where each number represents a point in coordinates (I, ai). Draw n vertical lines in the coordinates. The two endpoints of the vertical line I are (I, ai) and (I, 0), respectively. Find two of the lines so that the container composed of them and the x-axis can hold the most water.
Note: you cannot tilt the container, and the value of n is at least 2.
The vertical line in the picture represents the input array [1, 8, 6, 2, 5, 5, 4, 8, 3, 7]. In this case, the maximum value that the container can hold water (represented as the blue part) is 49.
Example:
Input: [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Answer to the question:
The first solution: violence
Public int MaxArea (int [] height) {/ / solution 1: int res = 0; for (int I = 0; I
< height.Length; i++) { for (int j = i + 1; j < height.Length; j++) { int thisArea = (j - i) * Math.Min(height[i], height[j]); res = Math.Max(res, thisArea); } } return res; } 第二种解法:双指针(左指针大于右指针,left++) public int MaxArea(int[] height) { int res = 0; int left = 0; int right = height.Length - 1; while (left < right) { int thisArea = (right - left) * Math.Min(height[left], height[right]); res = Math.Max(res, thisArea); if (height[left] >Height [right]) right--; else left++;} return res;}
The third solution: double pointer optimization (left pointer less than or equal to minimum height, left++)
Public int MaxArea (int [] height) {int res = 0; int left = 0; int right = height.Length-1; while (left < right) {int minHeight = Math.Min (height [left], height [right]); int thisArea = (right-left) * minHeight; res = Math.Max (res, thisArea); if (left < right & & height [left])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.