ARTS #035
ARTS #035
ARTS 035
Algorithme
11. Récipient contenant le plus d’eau
Difficulté : Moyenne
Étant donné n entiers non négatifs a<sub style=“display: inline;”>1</sub>, a<sub style=“display: inline;”>2</sub>, …, a<sub style=“display: inline;”>n </sub>, où chacun représente un point à la coordonnée (i, a<sub style=“display: inline;”>i</sub>). n lignes verticales sont tracées de telle sorte que les deux extrémités de la ligne i soient à (i, a<sub style=“display: inline;”>i</sub>) et (i, 0). Trouvez deux lignes qui, avec l’axe des x, forment un récipient, de telle sorte que le récipient contienne le plus d’eau.
**Remarque : **Vous ne pouvez pas incliner le conteneur et n est au moins 2.

<small style=“display: inline;”>Les lignes verticales ci-dessus sont représentées par le tableau [1,8,6,2,5,4,8,3,7]. Dans ce cas, la surface maximale d’eau (section bleue) que le conteneur peut contenir est de 49 .</small>
Exemple :
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
Solution
Langue : Swift
最容易想出来的解决方案应该就是两层循环,不出意外会超时,一般层循环的在leetcode都会超时。
class Solution {
func maxArea(_ height: [Int]) -> Int {
var maxArea = 0
for i in 0..<height.count {
let left = height[i]
for j in (i + 1)..<height.count {
let right = height[j]
let rightIndex = j
let tem = min(left, right) * (rightIndex - i)
maxArea = max(maxArea, tem)
}
}
return maxArea
}
}
看了别人的提示后,发现了O(N)时间复杂度的解决办法,从数组的两端向中间移动。具体解决方案如下:
class Solution {
func maxArea(_ height: [Int]) -> Int {
var maxArea = 0
var left = 0
var right = height.count - 1
var leftValue = 0
var rightValue = 0
while left < right {
leftValue = height[left]
rightValue = height[right]
maxArea = max(maxArea, min(leftValue, rightValue) * (right - left))
if leftValue > rightValue {
right -= 1
}
else{
left += 1
}
}
return maxArea
}
}
Avis
Offre HR : https://dandan2009.github.io/2019/12/10/job-negotiation-for-programmers-the-basic-principles/
Conseils
-
Astuces1 : alias zsh 怎么加参数?
我们知道alias可以简化命令,比如下面
alias ios='cd /Users/dan/Documents/wanan/ios/iv && pod install --no-repo-update && open iv.xcworkspace'在终端输入
ios就可以执行三条命令。
那么怎么alias 命令怎么带参数呢,答案就是用函数。
deletefile() { rm -rf $1 echo rm -rf $1 } alias dele=deletefileIl s’agit du fichier 2.txt, mais aussi du fichier 2.txt.
rm -rf 2.txt echo rm -rf 2.txt -
Astuces 2 : iOS pour iOS, et NSDecimalNumber est utilisé.
@property(nonatomic, strong) NSDecimalNumber* price;//< 单位:元Il s’agit du NSNumber类型, et du numéro 9. 8, pour l’AFN 9.800000000000001, et pour l’AFN,
Il s’agit d’un ensemble de paramètres NSDecimalNumberHandler.
- (void)setPrice:(NSDecimalNumber *)price{ NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO]; _price = [price decimalNumberByRoundingAccordingToBehavior:handler]; }; RoundingMode: NSRoundDown只舍不入 scale : 2 保留两位小数 -
Astuces 3 : clé ssh Mac
Dans ~/.ssh la configuration est terminée.
config signifie :
# github 的ssh-key Host github.com User xxxx@gmail.com PreferredAuthentications publickey IdentityFile ~/.ssh/github # coding.net的ssh-key Host gitlab.coding.net User xxxx@qq.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa
Partager
你怎么看待华为251事件?
What to read next
Want more posts about ARTS?
Posts in the same category are usually the best next step for reading more on this topic.
View same categoryWant to keep following #iOS?
Tags are useful for related tools, specific problems, and similar troubleshooting notes.
View same tagWant to explore another direction?
If you are not sure what to read next, return to the homepage and start from categories, topics, or latest updates.
Back home