SQL50
1789
学习使用 union,union 有自动去重的功能,如果要保留重复,使用 union all
select employee_id,department_id
from Employee
group by employee_id
having count(*) = 1
union
select employee_id,department_id
from Employee
where primary_flag = 'Y'
610
select x,y,z,
IF(x+y>z and x+z>y and y+z>x,'Yes','No') as triangle
from Triangle
180
select distinct l1.num as ConsecutiveNums
from Logs l1,Logs l2, Logs l3
where l1.Num = l2.Num and l2.Num = l3.Num
and l1.Id+1 = l2.Id and l2.Id + 1 = l3.Id
dp50基础
646. 最长数对链
class Solution {
public int findLongestChain(int[][] pairs) {
int n = pairs.length;
int dp[] = new int[n];
int max = 1;
for(int i=0;i<n;i++)dp[i] = 1;
Arrays.sort(pairs,(a,b)->{
if(a[0]!=b[0])return a[0]-b[0];
else return a[1] - b[1];
});
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
if(pairs[j][1]<pairs[i][0]){
dp[i] = Math.max(dp[i],dp[j]+1);
}
max = Math.max(max,dp[i]);
}
}
return max;
}
}
1218.最长定差子序列
public int longestSubsequence(int[] arr, int difference) {
int n = arr.length;
int max = 1;
Map<Integer,Integer> dp = new HashMap<>();
for(int i=0;i<n;i++){
int v = dp.getOrDefault(arr[i]-difference,0)+1;
max = Math.max(max,v);
dp.put(arr[i],v);
}
return max;
}
1027. 最长等差数列
class Solution {
public int longestArithSeqLength(int[] nums) {
int max = 1;
int n = nums.length;
Map<Integer,Map<Integer,Integer>> dp = new HashMap<>();
dp.put(0,new HashMap<>());
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
int diff = nums[i] - nums[j];
Map dpj = dp.get(j);
Map dpi = dp.getOrDefault(i,new HashMap<>());
int cnt = 1;
if(dpj.get(diff)!=null){
cnt = Math.max((int) dpj.get(diff)+1, (int)dpi.getOrDefault(diff,1));
}else{
cnt = Math.max(2,(int)dpi.getOrDefault(diff,1));
}
dpi.put(diff,cnt);
max = Math.max(max,cnt);
dp.put(i,dpi);
}
}
return max;
}
}