左耳听风ARTS第五周

Algorithms

121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
             Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Solution 1: leetcode上的思路—类似最大子数列问题,类似kadane’s algorithm算法。

class Solution {
     public int maxProfit(int[] prices) {
        int maxCur = 0;int maxSofar = 0;
         for(int i = 1;i < prices.length;i++){
             maxCur = Math.max(0,maxCur+= prices[i]-prices[i -1]);
             maxSofar = Math.max(maxCur,maxSofar);
         }
         return maxSofar;
    }
}

Review

ServerSide checklist
对于web应用来说,在发布产品之前,服务器端需要遵守的一系列规则,简单总结如下:
1、系统要有弹性:不能让应用局部发生的错误影响或者扩散到整个系统,系统在高负载的情况下不会挂掉。
2、部署对用户透明:应用添加节点的时候不会影响到当前在线的用户。
3、系统要有监控:http请求超时、500错误、服务重启、服务器资源耗尽。
4、系统测试:网络分区测试、压力测试。
5、数据备份:从备份数据恢复系统所有数据。
6、系统安全:TLS、OWASP TOP 10 Vulnerabilities、HTTP security headers。

Tips

1、网络协议第三层:网络层。网络层是无状态的,通过网关或者路由器等第三层设备,提供路由和寻址的功能。
2、ICMP协议和IP协议都属于第三层。ICMP报文有很多类型,常用的类型有主动请求和主动应答,ping就是一种主动请求,并且获取主动应答的ICMP协议。
3、TTL(time to live):数据包在网络中的生命周期。设计目的是防止数据包因不正确的路由造成无限的循环而无法送达及耗尽网络资源。
4、traceroute:设计目的是追踪数据包在IP网络中的路由路径。实现原理是设置特殊的TTL,选择一个不可能的值作为UDP端口号(大于30000),发送UDP数据报。目的主机会产生一份“端口不可达”的错误ICMP报文,如果数据报没有到达,则可能是超时。

Share

线程池的拒绝策略设计的简单理解。

猜你喜欢

转载自blog.csdn.net/wuweiwoshishei/article/details/84994650