177. Nth Highest Salary

177. Nth Highest Salary Solution CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare m int; set m = N - 1; RETURN ( # Write your MySQL query statement below. select distinct salary from Employee order by salary desc limit 1 offset m ); END Summary The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

178. Rank Scores

178. Rank Scores Solution 1 SELECT Score, @rank := @rank + (@prev <> (@prev := Score)) Rank FROM Scores, (SELECT @rank := 0, @prev := -1) init ORDER BY Score desc solution 2 SELECT Score, (SELECT COUNT(DISTINCT Score) FROM Scores as s1 WHERE s1.Score >= s2.Score) As rank FROM Scores as s2 ORDER BY rank; 一直不知道还能这么嵌套写法 Summary

178. Rank Scores

178. Rank Scores Solution 1 SELECT Score, @rank := @rank + (@prev <> (@prev := Score)) Rank FROM Scores, (SELECT @rank := 0, @prev := -1) init ORDER BY Score desc solution 2 SELECT Score, (SELECT COUNT(DISTINCT Score) FROM Scores as s1 WHERE s1.Score >= s2.Score) As rank FROM Scores as s2 ORDER BY rank; 一直不知道还能这么嵌套写法 Summary

AI: Learning from observations

Decision tree learning Problem: decide whether to wait for a table at a restaurant, based on the following attributes: Alternate: is there an alternative restaurant nearby? Bar: is there a comfortable bar area to wait in? Fri/Sat: is today Friday or Saturday? Hungry: are we hungry? Patrons: number of people in the restaurant (None, Some, Full) Price: price range ($, $$, $$$) Raining: is it raining outside? Reservation: have we made a reservation?

AI: Constraint Satisfaction Problems

CSPs are a special kind of search problem States defined by values of a fixed set of variables from domain Goal test defined by constraints on variable values acceptable solutions are complete and consistent binary CSP: relates two variables; constraint graph: nodes are variables, arc are constraits. Discrete variable(n) with finit domain(d): O(d^n) complete assignments Discrete variable with infinit domain need a constraint Varieties of constraints: involve one varible(unary), two(binary), three(higher-order) Backtracking depth-first search with one variable assigned at each level the basic uninformed algorithm for CSP {% codeblock %} function BacktrackingSearch(csp) return solution or failure return Backtrack({}, csp)