二叉树的最大路径和
给定一个二叉树,请计算节点值之和最大的路径的节点值之和是多少。
这个路径的开始节点和结束节点可以是二叉树中的任意节点
private int maxVal = Integer.MIN_VALUE;
public int maxPathSum (TreeNode root) {
// write code here
if(root == null){
return 0;
}
helper(root);
return maxVal;
}
public int helper(TreeNode root){
if(root == null){
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
int sum = root.val;
if(left > 0){
sum += left;
}
if(right > 0){
sum += right;
}
// 更新最大值
if(maxVal < sum){
maxVal = sum;
}
return Math.max(left,right) > 0 ? Math.max(left,right) + root.val : root.val;
}
任意选若干个不相邻的数得到的和最大
/*
动态规划
*/
public static int getNum(int []nums){
if (nums == null || nums.length < 1){
return 0;
}else if (nums.length < 2){
return nums[0];
}
int n = nums.length;
int []dp = new int[n];
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);
for (int i = 2; i < n; i++){
dp[i] = Math.max(dp[i-1], nums[i] + dp[i-2]);
}
return dp[n-1];
}
最长的可整合数组长度
先给出可整合数组的定义:如果一个数组在排序之后,每相邻两个数的差的绝对值都为1,或者该数组长度为1,则该数组为可整合数组。
例如,[5, 3, 4, 6, 2]排序后为[2, 3, 4, 5, 6],符合每相邻两个数差的绝对值都为1,所以这个数组为可整合数组
给定一个数组arr, 请返回其中最大可整合子数组的长度。例如,[5, 5, 3, 2, 6, 4, 3]的最大可整合子数组为[5, 3, 2, 6, 4],所以请返回5
/*
思路:可整合数组满足:最大值-最小值+1=长度
*/
public static int getArrsLength(int []nums, int n){
int len = 1;
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++){
int max = nums[i];
int min = nums[i];
for (int j = i; j < n; j++){
if (set.contains(nums[j])){
continue;
}else{
set.add(nums[j]);
}
max = Math.max(nums[j], max);
min = Math.min(nums[j], min);
if (max - min + 1 == set.size()){
len = Math.max(len, set.size());
}
}
set.clear();
}
return len;
}
最长公共子序列
给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
示例 :
输入:text1 = “abcde”, text2 = “ace”
输出:3
解释:最长公共子序列是 “ace” ,它的长度为 3 。
输入:text1 = “abc”, text2 = “abc”
输出:3
解释:最长公共子序列是 “abc” ,它的长度为 3 。
输入:text1 = “abc”, text2 = “def”
输出:0
解释:两个字符串没有公共子序列,返回 0 。
/*
动态规划
*/
public int longestCommonSubsequence(String text1, String text2) {
int n1 = text1.length(), n2 = text2.length();
int [][]dp = new int[n1+1][n2+1];
for(int i = 1; i <= n1; i++){
char c1 = text1.charAt(i-1);
for(int j = 1; j <= n2; j++){
char c2 = text2.charAt(j-1);
if(c1 == c2){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
}
return dp[n1][n2];
}
森林中的兔子
森林中,每个兔子都有颜色。其中一些兔子(可能是全部)告诉你还有多少其他的兔子和自己有相同的颜色。我们将这些回答放在 answers 数组里。
返回森林中兔子的最少数量。
示例:
输入: answers = [1, 1, 2]
输出: 5
/*
一般地,如果有 x 只兔子都回答 y,则至少有 x/(y+1)
种不同的颜色,且每种颜色有 y+1 只兔子,
因此兔子数至少为x/(y+1)
可以用哈希表统计 中各个元素的出现次数,
对每个元素套用上述公式计算,并将计算结果累加,即为最终答案。
*/
public int numRabbits(int[] answers) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int y : answers) {
map.put(y, map.getOrDefault(y, 0) + 1);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int y = entry.getKey(), x = entry.getValue();
res += (x + y) / (y + 1) * (y + 1);
}
return res;
}
笨的阶乘
正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。
我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。
例如,clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。
另外,使用的除法是地板除法(floor division),所以 10 * 9 / 8 等于 11。这保证结果是一个整数。
实现上面定义的笨函数:给定一个整数 N,它返回 N 的笨阶乘。
示例 :
输入:4
输出:7
解释:7 = 4 * 3 / 2 + 1
输入:10
输出:12
解释:12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
public int clumsy(int N) {
// 用栈来模拟
Stack<Integer> stack = new Stack<>();
int flag = 0;//乘除加减
stack.push(N);
for(int i = N - 1; i > 0; i--){
if(flag == 0){
stack.push(stack.pop()*i);
flag = 1;
}else if(flag == 1){
stack.push(stack.pop()/i);
flag = 2;
}else if(flag == 2){
stack.push(i);
flag = 3;
}else{
stack.push(-i);
flag = 0;
}
}
int sum = 0;
while(!stack.isEmpty()){
sum += stack.pop();
}
return sum;
}
用一个栈实现另一个排序栈
一个栈中元素的类型为整型,现在想将该栈从顶到底按从大到小的顺序排序,只许申请一个栈。除此之外,可以申请新的变量,但不能申请额外的数据结构。如何完成排序?
import java.util.*;
public class Main{
private static Stack<Integer> stack = new Stack<>();
public static void sortStack(){
Stack<Integer> help = new Stack<>();
while(!stack.isEmpty()){
int tmp = stack.pop();
while(!help.isEmpty() && help.peek() > tmp){
stack.push(help.pop());
}
help.push(tmp);
}
stack = help;
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []nums = new int[n];
for(int i = 0; i < n; i++){
stack.push(sc.nextInt());
}
sortStack();
while(!stack.isEmpty()){
System.out.print(stack.pop() + " ");
}
}
}
滑动窗口的最大值
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
示例:
输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7]
/*
// 超时
public static int[] getSlideArr(int []nums, int k){
int n = nums.length;
int []res = new int[n - k + 1];
for(int i = 0; i < n - k + 1; i++){
int max = nums[i];
for(int j = i + 1; j < n && j < k + i; j++){
if(nums[j] > max){
max = nums[j];
}
}
res[i] = max;
}
return res;
}
*/
/*
双端队列
*/
public static int[] getSlideArr(int []nums, int k){
if(nums.length == 0 || k == 0) {
return new int[0];
}
int n = nums.length;
int []res = new int[n - k + 1];
Deque<Integer> deque = new LinkedList<>();
for (int j = 0, i = 1 - k; j < n; j++, i++){
// 删除前一个,即删除 deque 中对应的 nums[i-1]
if (i > 0 && deque.peekFirst() == nums[i-1]){
deque.removeFirst();
}
// 删除小的,保持递减
while (!deque.isEmpty() && deque.peekLast() < nums[j]){
deque.removeLast();
}
deque.addLast(nums[j]);
if (i >= 0){
res[i] = deque.peekFirst();
}
}
return res;
}