# 冲刺大厂每日算法&面试题，动态规划21天——第十八天

·  阅读 330

「这是我参与11月更文挑战的第16天，活动详情查看：2021最后一次更文挑战

# 21天动态规划入门

``````示例 1：

``````示例 2：

``````示例 3：

``````class Solution {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int maxans = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
maxans = Math.max(maxans, dp[i]);
}
return maxans;
}
}

# 面试题

``````package tree;

public class node {
public int val;
public node left;//左孩子
public node right;//右孩子
public node() {

}
public node(int val,node left,node right) {
this.val=val;
this.left=left;
this.right=right;
}
public node(int val, node left) {
super();
this.val = val;
this.left = left;
}

}

``````public class kTierNums {
//求第k层节点的个数
public int kNums(node root,int k) {
if(root==null||k<1)return 0;
if(k==1)return 1;
int leftNum=kNums(root.left,k-1);
int rightNum=kNums(root.right,k-1);
return leftNum+rightNum;
}

}

``````package tree;

public class AVL {
//判断是不是平衡二叉树
public int BalancedTree(node root) {
if(root==null)return 0;
int left=BalancedTree(root.left);
int right=BalancedTree(root.right);
if(left==-1||right==-1||Math.abs(left-right)>1) {
return -1;
}
return Math.max(left, right)+1;
}
public boolean isBalanced(node root) {
return BalancedTree(root)!=1;
}
}