Day 54 - tree problem, SQL

Posted by austrainer on Thu, 17 Feb 2022 17:46:15 +0100

It's a little smoother to brush the questions today than yesterday. I actually brushed three tree questions in one breath. Although I still have to refer to the problem solution from time to time, I can also think about how the backtracking is realized. It seems that it is useful to brush more questions. I hope to maintain high learning efficiency. It's better to have time to exercise and read in the evening.

Today's progress:
1. Insist on listening to online classes and brushing questions
2. Keep exercising
3. Keep recording the little prince
4. Keep records

Study notes:
1. Timestamp, check the regret medicine of business problems, create_time: data creation time, update_time: data update time

2. Backtracking method adopts the idea of trial and error. It tries to solve a problem step by step. In the process of solving the problem step by step, when it finds that the existing step-by-step answer can not get an effective and correct answer, it will cancel the calculation of the previous step or even the previous steps, and then try to find the answer again through other possible step-by-step answers

3. Pruning strategy is to use filtering conditions to cut out the search path that does not need to be considered at all (it has been judged that there is no optimal solution) in the search process, so as to avoid some unnecessary search, greatly optimize the solution speed of the algorithm and ensure the correctness of the results.

4. Structured query language is abbreviated as SQL (pronunciation:/ ˈ es kju ː ˈ el / "S-Q-L"), a database query and programming language, is used to access data and query, update and manage relational database systems; It is also the extension of the database script file.

5. Structured query language includes six parts:
1: DQL:Data Query Language:
Its statement, also known as "data retrieval statement", is used to obtain data from the table and determine how the data is given in the application. The reserved word SELECT is the verb most used by DQL (and all SQL). Other reserved words commonly used by DQL include WHERE, ORDER BY, GROUP BY and HAVING. These DQL reserved words are often used with other types of SQL statements.
2: Data Manipulation Language (DML):
Its statements include the verbs INSERT, UPDATE and DELETE. They are used to add, modify and DELETE rows in the table respectively. Also known as action query language.
3: Transaction processing language (TPL):
Its statement can ensure that all rows of the table affected by DML statement are updated in time. TPL statements include BEGIN TRANSACTION, COMMIT, and ROLLBACK.
4: Data control language (DCL):
Its statement is licensed through GRANT or REVOKE to determine the access of individual users and user groups to database objects. Some RDBMS can use GRANT or REVOKE to control access to form columns.
5: Data definition language (DDL):
Its statements include the verbs CREATE and DROP. CREATE a new table or delete a table (CREATE table or DROP TABLE) in the database; Adding indexes to tables, etc. DDL includes many reserved words related to the data obtained in the human database directory. It is also part of the action query.
6: Pointer control language (CCL):
Its statements, such as DECLARE CURSOR, FETCH INTO and UPDATE WHERE CURRENT, are used for single line operations on one or more forms.

6. Five data types in structured query language: character type, text type, numerical type, logical type and date type.

7.SQL can create new databases and their objects (tables, indexes, views, stored procedures, functions and triggers).
SQL can modify the structure of an existing database.
Objects can be deleted from the SQL database.
SQL can TRUNCATE (delete) all records in the table.
SQL can COMMENT on the data dictionary.
SQL can RENAME an object.
SQL can select (retrieve) data from the database.
SQL can insert data into tables.
SQL can update existing data in a table.
SQL can delete records from database tables.
SQL can set GRANT and REVOKE permissions of users in the database.

8.SQL constraints are rules that are enforced on data columns on a table. They are used to limit the types of data that can enter the table. This ensures the accuracy and reliability of the data in the database. Constraints can be column level or table level. Column level constraints apply to only one column, while table level constraints apply to the entire table. The following are some of the most common constraints available in sql −:
NOT NULL constraint: ensure that the data in the column cannot have null value
DEFAULT constraint: provides the DEFAULT value used when the column data is not specified
UNIQUE constraint: ensure that all data in the column is different
Primary key constraint: uniquely identifies rows / records in the data table
Foreign key constraint: uniquely identifies a row / record in other tables
CHECK constraint: this constraint ensures that all values in the column meet a certain condition
Index: used to quickly create or retrieve data in the database

9.46. Full arrangement
Given an array nums without duplicate numbers, all possible permutations are returned. You can return answers in any order.

Input: num = [1,2,3]
Output: [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]

Problem solving idea: use the array used to store the state. If accessed, it is true, otherwise it is false. Use dfs to explore the possibility of all combinations. After obtaining a solution, add it to the result set, trace back to the previous node, delete the last element, and change the userd state to false until all cases are traversed.

import java.util.ArrayList;
class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    boolean[] used;
    public List<List<Integer>> permute(int[] nums) {
        if(nums.length==0){
            return res;
        }
        ArrayList<Integer> p = new ArrayList<Integer>();
        used = new boolean[nums.length];
        Arrays.fill(used, false);
        generateP(nums, 0, p);
        return res;
    }
    public void generateP(int[] nums, int index, ArrayList<Integer> p){
        if(index==nums.length){
            res.add((ArrayList<Integer>)p.clone());
            return;
        }
        for(int i=0; i<nums.length;i++){
            if(!used[i]){
                p.add(nums[i]);
                used[i] = true;
                generateP(nums, index+1, p);
                p.remove(p.size()-1);
                used[i] = false;
            }
        }
        return;
    }
}

10.47. Full arrangement II
Given a sequence nums that can contain repeated numbers, return all non repeated full permutations in any order.

Input: num = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]

Solution idea: use the array used to store the state. If not accessed, it is false. If accessed, it is true. Use dfs to traverse each case. If a case is found, add it to the HashSet result set to ensure that there are no duplicate cases. Then go back to the previous node, delete the last element, change the state to false, and continue to search for the next case until all cases are explored.

class Solution {
    HashSet<List<Integer>> h = new HashSet<List<Integer>>();
    boolean[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        used = new boolean[nums.length];
        ArrayList<Integer> p = new ArrayList<Integer>();
        dfs(nums, 0, p);
        Arrays.fill(used, false);
        List<List<Integer>> res = new ArrayList<List<Integer>>(h);
        return (res);
    }
    public void dfs(int[] nums, int index, ArrayList<Integer> p){
        if(index==nums.length){
            h.add((ArrayList<Integer>)p.clone());
        }
        for(int i=0; i<nums.length; i++){
            if(!used[i]){
                p.add(nums[i]);
                used[i] = true;
                dfs(nums, index+1, p);
                p.remove(p.size()-1);
                used[i] = false;
            }
        }
    }
}

11.77. combination
Given two integers n and k, returns the combination of all possible k numbers in the range [1, n].
You can return answers in any order.

Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

Problem solving idea: use dfs to traverse each case. After selecting enough K numbers, go back to the previous node until all cases are obtained. I < = len - (k-p.size()) + 1 is used for pruning. If the number of remaining elements is less than the remaining target selection, there is no need to continue.

class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    int len;
    public List<List<Integer>> combine(int n, int k) {
        len=n;
        ArrayList<Integer> p = new ArrayList<Integer>();
        if(k>n){
            return res;
        }
        generateC(k, 1, p);
        return res;
    }
    public void generateC(int k, int start, ArrayList<Integer> p){
        if(p.size()==k){
            res.add((ArrayList<Integer>)p.clone());
            return;
        }
        for(int i=start; i<=len-(k-p.size())+1; i++){
            p.add(i);
            generateC(k, i+1, p);
            p.remove(p.size()-1);
        }
    }
}

Today's content is quite a lot. I don't know why. I sleep more and more late at night. In fact, seven hours of sleep is not guaranteed. Continue to refuel tomorrow!

Topics: Database SQL