左耳听风ARTS第十一周

Algorithms

198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.
Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

Solution 1:leetcode上面的思路——递归(由顶至下)。首先找出递归条件,抢劫第n间房有两种情况,要么抢,要么不抢。抢的话,总共就是前面n-2间的总和加上第n间抢的钱。不抢的话,总共就是前面n-1间的总和。

class Solution {
    int max[];
    public int rob(int[] nums) {
        max = new int[nums.length + 1];
        Arrays.fill(max,-1);
        return rob(nums,nums.length-1);
    }
    
    private int rob(int[] nums,int size){
        
        if(size < 0){
            return 0;
        }
        if(max[size] >= 0){
            return max[size];
        }
        
        max[size] = Math.max(rob(nums,size - 2) + nums[size],rob(nums,size - 1));
        return max[size];
    }
}

Solution 2:leetcode上的思路:循环(由底至上)。优化点:通过2个全局变量和1个临时变量就可以实现数组的功能。

class Solution {
    
    public int rob(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int previous = 0;
        int next = 0;
        for(int i = 0;i < nums.length;i++){
            int temp = next;
            next = Math.max(next,previous + nums[i]);
            previous = temp;
        }
        return next;
    }
}

Review

Recommended Reading for Developers
1、Code Complete 2 ——complete your code
2、人月神话 —— 计算机技术会变,但是人不会变
3、Don’t make me think —— 如果你选择一本实用性的书,那就选择这一本
4、Programming Pearls —— 每个阶段的程序员都能从中收益
5、The Pragmatic Programmer —— 程序员修炼之道

Tips

网络协议基础知识:Socket
1、socket是一种操作系统提供的进程间通信机制。套接字对包含五元组(服务器ip地址和端口、客户端ip地址和端口、通信协议)。
2、基于TCP协议的服务,服务器器端socket包含监听socket和已连接socket。每个服务只有一个监听socket,用来监听所有的客户端连接。而已连接socket则有很多个,每个客户端连接后服务端都会返回一个已连接socket。
3、基于UDP协议的服务,由于不需要建立连接,服务器端socket只需要一个就可以和多个客户端通信。
3、在内核中,Socket是一个文件。内核中的socket结构包含一个发送队列和一个接收队列。在两个队列里面保存的是一个缓存sk_buff,这个缓存里面能够看到完整的包的结构。
在这里插入图片描述
4、多进程通信
在这里插入图片描述
父进程调用fork,返回了一个子进程,并复制了一份文件描述符和内存空间,文件描述符都指向整个内核统一的打开文件列表。

5、多线程方式
在这里插入图片描述
主线程和子线程共用一份文件描述符和内存空间。新的线程可以通过已连接Socket处理请求,从而达到并发处理的目的。

6、IO多路复用
在这里插入图片描述
调用epoll函数的时候会创建一个epoll对象,epoll对象也是一个文件,对应一个文件描述符,同样对应着打开文件列表中的一项。在这项里面有一个红黑树,在红黑树里,保存着这个epoll要监听的所有socket。当新添加一个socket的时候,其实就是加入这个红黑树,同时红黑树里面的节点指向一个结构,并将这个结构加入被监听的socket的事件列表中。当一个socket来了一个事件的时候,可以从这个列表中得到epoll对象,然后通过回调通知epoll对象。通过这种方式,一个单机服务能够监听的socket的上限就为系统定义的、进程打开的最大文件描述符个数,是解决多并发的利器。

Share

TcpNoDelay遇到Nagle算法

猜你喜欢

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