文章目录
前言
提示:这里为每天自己的学习内容心情总结;
Learn By Doing,Now or Never,Writing is organized thinking.
提示:以下是本篇文章正文内容
一、今天学习了什么?
- 写SQL语句;
- 动态规划算法
- 修改简历;
明天的任务是:
- 写完项目中的spi部分
- 写完股票问题的算法
- 继续写SQL语句
二、SQL
sql
delete from exam_record
where TIMESTAMPDIFF(minute, start_time, submit_time) < 5
and score < 60;
sql
DELETE FROM exam_record
WHERE submit_time IS NULL
OR TIMESTAMPDIFF(MINUTE, start_time, submit_time) < 5
ORDER BY start_time
LIMIT 3;
sql
truncate table exam_record
sql
CREATE TABLE IF NOT EXISTS user_info_vip
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
uid int(11) NOT NULL UNIQUE COMMENT '用户ID',
nick_name varchar(64) COMMENT '昵称',
achievement int(11) DEFAULT 0 COMMENT '成就值',
`level` int(11) COMMENT '用户等级',
job varchar(32) COMMENT '职业方向',
register_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间'
) default charset=UTF8;
java
ALTER TABLE user_info
ADD COLUMN school VARCHAR(15) AFTER `level` ;
ALTER TABLE user_info
CHANGE COLUMN job profession varchar(10);
ALTER TABLE user_info
CHANGE COLUMN achievement achievement int DEFAULT 0;
sql
DROP TABLE IF EXISTS exam_record_2011,
exam_record_2012,
exam_record_2013,
exam_record_2014;
sql
CREATE INDEX idx_duration ON examination_info (duration);
ALTER TABLE examination_info ADD UNIQUE uniq_idx_exam_id(exam_id);
create fulltext index full_idx_tag on examination_info(tag);
sql
alter table examination_info drop index uniq_idx_exam_id;
drop index full_idx_tag on examination_info;
sql
select
tag,
difficulty,
round((sum(score) - max(score) - min(score))/(count(*) - 2),1) as clip_avg_score
from exam_record e1
inner join examination_info e2
on e1.exam_id = e2.exam_id
where e2.tag='SQL' and e2.difficulty='hard' and e1.submit_time is not null;
sql
select
count(*) as total_pv,
count(case when score is not null then 1 else null end) as complete_pv,
count(distinct case when score is not null then exam_id else null end) as complete_exam_cnt
from exam_record ;
sql
select min(e_r.score) as min_score_over_avg
from exam_record e_r join examination_info e_i
on e_r.exam_id = e_i.exam_id
where e_i.tag = 'SQL'
and score >= (select avg(e1.score)
from exam_record e1 join examination_info e2
on e1.exam_id = e2.exam_id
where tag = 'SQL'
)
三、动态规划
- 377. 组合总和 Ⅳ(⭐⭐⭐)
java
public int combinationSum4(int[] nums, int target) {
/**
* 动态规划
* dp[i],在数组中凑出目标和为i的元素组合的个数
*/
int[] dp = new int[target + 1];
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int j = 0; j < nums.length; j++) {
if (i >= nums[j]) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
java
public int coinChange(int[] coins, int amount) {
/**
* 动态规划
* dp[i],凑出总金额为i所需要的最少的硬币个数
*/
int[] dp = new int[amount + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
// 组合数,背包容量放在里面
for (int i = 0; i < coins.length; i++) {
for (int j = coins[i]; j <= amount; j++) {
if (dp[j - coins[i]] != Integer.MAX_VALUE) {
dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1);
}
}
}
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
java
public int numSquares(int n) {
/**
* 完全平方数,组合数的话,物品在外层的for循环中,背包容量在内层的for循环中
* dp[i],代表和为i的完全平方数的最少数量
*/
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i * i <= n; i++) {
for (int j = i * i; j <= n; j++) {
if (dp[j - i * i] != Integer.MAX_VALUE) {
dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
}
}
}
return dp[n] == Integer.MAX_VALUE ? -1 : dp[n];
}
- 139. 单词拆分(⭐⭐⭐)
java
public boolean wordBreak(String s, List<String> wordDict) {
/**
* 单词拆分,可以重复使用字典中的单词,每次都可以重复使用
* - 每次都需要重新遍历字典中的单词进行判断
* dp[i]代表字符串s下标从[0,i]是否可以背字典拼接出
*/
// dp[i] : [0,i]位置的字符是否可以使用字典中的字符进行组合起来
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
// 先遍历背包(将字符串看作是背包),再去遍历字典(看作是物品)
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < wordDict.size(); j++) {
int length = wordDict.get(j).length();
if (i >= length &&
s.substring(i - length, i).equals(wordDict.get(j)) &&
dp[i - length]) {
dp[i] = true;
}
}
}
return dp[s.length()];
}
java
public int rob(int[] nums) {
int length = nums.length;
if (length < 2) {
return nums[0];
}
/**
* 如果能不能抢当前房屋,取决于之前的结果
* dp[i] 到达第i个房屋时,能偷窃的最大的金额
*/
int[] dp = new int[length];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
}
return dp[length - 1];
}
java
public int rob(int[] nums) {
int length = nums.length;
// base case
if (length == 1) {
return nums[0];
}
// 将大问题拆解成子问题,由于第一间房屋和最后一间房屋不能同时偷,所以拆解成两个子问题
// 第一:从第一间房屋和倒数第二间房屋能偷的最大价值
// 第二:从第二间房屋和倒数第一间房屋能偷的最大价值
return Math.max(get(nums, 0, nums.length - 2), get(nums, 1, nums.length - 1));
}
private int get(int[] nums, int start, int end) {
int length = end - start + 1;
if (length == 1) {
return nums[start];
}
int[] dp = new int[length];
dp[0] = nums[start];
dp[1] = Math.max(nums[start], nums[start + 1]);
for (int i = 2; i < length; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[start + i]);
}
return dp[end - start];
}
- 337. 打家劫舍 III(⭐⭐⭐⭐⭐)
java
public int rob(TreeNode root) {
/**
* 此函数是返回入口处能偷窃的最高金额,采用一个map防止重复计算
*/
if (root == null) {
return 0;
}
if (map.containsKey(root)) {
return map.get(root);
}
int money = root.val;
if (root.left != null) {
money += rob(root.left.left) + rob(root.left.right);
}
if (root.right != null) {
money += rob(root.right.left) + rob(root.right.right);
}
money = Math.max(money, rob(root.left) + rob(root.right));
map.put(root, money);
return money;
}
private Map<TreeNode, Integer> map = new HashMap<>();
总结
提示:这里对文章进行总结: