[11] Container With Most Water
Easy array two pointers
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
Notice that you may not slant the container.
Example2:
Input: height = [1,1]
Output: 1
Example3:
Input: height = [4,3,2,1,4]
Output: 16
Example4:
Input: height = [1,2,1]
Output: 2
解答 本题可以采用常规的方法和基于 hashtab 的方式进行处理(题目描述中已经定义不存在重复的数据)
func maxArea(height []int) int {
max := 0
for i := 0; i < len(height); i++ {
for j := i + 1; j < len(height); j++ {
minVal, interval := height[i], j-i
if height[j] < minVal {
minVal = height[j]
}
total := minVal * interval
if total > max {
max = total
}
}
}
return max
}
func maxArea(height []int) int {
max, left, right := 0, 0, len(height)-1
for left < right {
minVal, interval := height[left], right-left
if height[right] < minVal {
minVal = height[right]
}
total := minVal * interval
if total > max {
max = total
}
if height[left] < height[right] {
left++
} else {
right--
}
}
return max
}
Advice to Myself When Starting Out as as Software Developer
- Take the time to read two books per year on software engineering
- Learn the language you use at work in-depth, to the very bottom
(1). The more languages you know, the more you can evaluate their strengths and weaknesses.
- Pair with other developers more often
- Write unit tests and run them against a CI
- Make refactoring a habit and master refactoring tools
- Know that good software engineering is experience. Get lots of it.
(2). Look for opportunities to work on different stacks, different domains, and challenging projects.
- Teach what you learn
这周看了一些有关 Web IM 方面有关消息推送的资料,但是看的还不深入,通知方案主要有以下几种: 在线客服系统最重要的就是通知,用户发送的消息如何通知客服,客服发送的消息又如何通知用户?
- 轮询:客户端定时刷新,采用 ajax 方式在后台定时获取数据。
优点:实现简单,无需做过多的更改
缺点:轮询的间隔过长,会导致用户不能及时接收到更新的数据;轮询的间隔过短,会导致查询请求过多,增加服务器端的负担
- 长连接:客户端向服务器端发起请求的时候,服务端会阻塞请求,知道服务器端读取到消息才返回
优点:比 polling 做了优化,有较好的时效性
缺点:保持连接会消耗资源;服务器没有返回有效数据,程序超时。
- websocket: 服务器端可以主动推送消息给客户端
支持双向通信,实时性更强
可以发送文本,也可以发送二进制数据
减少通信量:只要建立起WebSocket连接,就希望一直保持连接状态。和HTTP相比,不但每次连接时的总开销减少,而且由于WebSocket的首部信息很小,通信量也相应减少了
My Reading & Listening List 这篇文章分享了有关软件开发和管理方面的一些书籍,特别是 Podcasts 这部分,里面有些非常好的 podcasts 频道资源。