Jerry's Blog

Recording what I learned everyday

View on GitHub


4 August 2019

Database(2) -- Install MySQL and Configuration

by Jerry Zhang

LeetCode Day 30: P198. House Robber (Easy)

题目:

你是一个专业的小偷。沿着一条街,要偷一排房子。每户藏的金钱不同。如果连续两间房子被偷,就会触发报警装置。

求:可以偷到的最大金额。

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.

我的思路:

动态规划的题。从第一间房子开始,每间屋子偷还是不偷,取决于从每一间房子开始,最后拿到的总数最多还是从第二间房子开始, 最后拿到的总数最多。

答案:

public class E_198_HouseRobber {
    public int rob(int[] nums) {
        if(nums.length==0) return 0;
        if(nums.length==1) return nums[0];

        //Initialize an arrays to store the money
        int[] mark = new int[nums.length];

        //We can infer the formula from problem:mark[i]=max(num[i]+mark[i-2],mark[i-1])
        //so initialize two nums at first.
        mark[0] = nums[0];
        mark[1] = Math.max(nums[0], nums[1]);

        //Using Dynamic Programming to mark the max money in loop.
        for(int i=2;i<nums.length;i++){
            mark[i] = Math.max(nums[i]+mark[i-2], mark[i-1]);
        }
        return mark[nums.length-1];
    }
}

每个房子偷还是不偷,取决于前i-2个房子的最大值加上当前的房子总金额更大还是不要当前这个,前i-1个房子总金额更大。 我的思路错误在于,我觉得拿不拿是由“将来”决定的。其实每一步如果都确保是最大的,最后自然就是最大的,不用考虑将来。 所以拿不拿还是“历史”决定的。

Database:

Disable SELinux

vi /etc/selinux/config

SELINUX=disabled

Then reboot linux.

Install MySQL

yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
yum install mysql-community-server -y

Change the Access Permissions and initialize

chmod -R 777 /var/lib/mysql/
mysqld --initialize
chmod -R 777 /var/lib/mysql/*

Start mysql

service mysql start

temporary password:

grep 'temporary password' /var/log/mysqld.log

login mysql:

mysql -u root -p

Configurations:

Change password:

alter user user() identified by "root";

Remote control:

UPDATE user SET host='%' WHERE user='root';
FLUSH PRIVILIGES;

Change configuration

vi /etc/my.cnf
character_set_server=utf8
bind-address=0.0.0.0
service mysqld restart

firewall:

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

Finally, we can login mysql with NaviCat using the ip address of the virtual machine.

tags: Database, - Linux