This commit is contained in:
whai 2024-02-25 22:34:24 +08:00
parent b8be07776c
commit e918f47cca
7 changed files with 275 additions and 70 deletions

View File

@ -0,0 +1,44 @@
package cn.whaifree.leetCode.Greedy;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/25 22:21
* @注释
*/
public class LeetCode322 {
@Test
public void test() {
System.out.println(new Solution().maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
}
class Solution {
/**
* 随时可以买卖那么只要有跌的我都不要
* 上帝视角只要涨我就全要
*
* 7跌 1涨5 4涨6 跌4
*
* @param prices
* @return
*/
public int maxProfit(int[] prices) {
int maxProfit = 0;
for (int i = 0; i < prices.length; i++) {
if (i > 0 && prices[i] - prices[i - 1] > 0) {
maxProfit += prices[i] - prices[i - 1];
}
}
return maxProfit;
}
// TODO 动态规划
}
}

View File

@ -0,0 +1,45 @@
package cn.whaifree.leetCode.Greedy;
import jdk.internal.instrumentation.InstrumentationTarget;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/24 13:23
* @注释
*/
public class LeetCode376 {
@Test
public void test() {
// System.out.println(new Solution().wiggleMaxLength(new int[]{1, 7, 4, 9, 2, 5}));
System.out.println("========");
System.out.println(new Solution().wiggleMaxLength(new int[]{1,17,5,10,13,15,10,5,16,8}));
}
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
//当前差值
int curDiff = 0;
//上一个差值
int preDiff = 0;
int count = 1;
for (int i = 1; i < nums.length; i++) {
//得到当前差值
curDiff = nums[i] - nums[i - 1];
//如果当前差值和上一个差值为一正一负
//等于0的情况表示初始时的preDiff
if ((curDiff > 0 && preDiff <= 0) || (curDiff < 0 && preDiff >= 0)) {
count++;
preDiff = curDiff;
}
}
return count;
}
}
}

View File

@ -0,0 +1,59 @@
package cn.whaifree.leetCode.Greedy;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/24 13:04
* @注释
*/
public class LeetCode455 {
@Test
public void test() {
System.out.println(new Solution().findContentChildren(new int[]{1, 2, 3,1}, new int[]{2,3,1}));
}
class Solution {
/**
*
* @param g 孩子的胃口
* @param s 饼干
* @return
*/
public int findContentChildren(int[] g, int[] s) {
// 找到g中最大的分配s中最大的看看是否满足
Arrays.sort(g);
Arrays.sort(s);
int res = 0;
int gIndex = g.length-1;
int sIndex = s.length - 1;
while (sIndex >= 0 && gIndex >= 0) {
// 饼干可以分配就让两个指针--
if (s[sIndex] >= g[gIndex]) {
res++;
sIndex--;
gIndex--;
} else {
// 饼干不能分配就分配给更小的孩子
gIndex--;
}
}
return res;
}
}
}

View File

@ -0,0 +1,48 @@
package cn.whaifree.leetCode.Greedy;
import org.junit.Test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/2/25 10:53
* @注释
*/
public class LeetCode53 {
@Test
public void test() {
System.out.println(new LeetCode53().new Solution().maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
}
class Solution {
/**
* 遇到加上变为负数重新从0计算
* 因为前面那一串会拖累后面那串
* @param nums
* @return
*/
public int maxSubArray(int[] nums) {
int maxSum = Integer.MIN_VALUE;
int sum = 0;
for (int i = 0; i < nums.length; i++) {
// 加上某个数后是否会变为负数会则直接从0开始计算因为其只会拖累后面的串
sum += nums[i];
maxSum = Math.max(sum, maxSum);
if (sum < 0) {
sum = 0;
}
}
return maxSum;
}
}
}

View File

@ -1,70 +0,0 @@
package cn.whaifree.leetCode;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* 给定一个整数数组 nums 和一个整数目标值 target请你在该数组中找出 和为目标值 target  的那 两个 整数并返回它们的数组下标
*
* 你可以假设每种输入只会对应一个答案但是数组中同一个元素在答案里不能重复出现
*
* 你可以按任意顺序返回答案
*
* 来源力扣LeetCode
* 链接https://leetcode.cn/problems/two-sum
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*
*
*
* 输入nums = [2,7,11,15], target = 9
* 输出[0,1]
* 解释因为 nums[0] + nums[1] == 9 返回 [0, 1]
*
*
* @author whaifree
* @package cn.whaifree.leetCode
* @Date: 2022/9/13 20:54
*/
public class LeetCode001 {
/**
* map存储key为nums内的值value为下标
* @param nums
* @param target
* @return
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int key;
for (int i = 0; i < nums.length; i++) {
//如果包含匹配的元素即输出下标
key = target - nums[i];
if (map.containsKey(key)){
return new int[]{map.get(key),i};
}else {
map.put(nums[i],i);
}
//不包含匹配的元素则增加到map中
}
return null;
}
public static void main(String[] args) {
int[] ints = new LeetCode001().twoSum(new int[]{2, 7, 11, 15}, 13);
for (int anInt : ints) {
System.out.println(anInt);
}
}
}

View File

@ -0,0 +1,76 @@
package cn.whaifree.leetCode;
import cn.whaifree.leetCode.Tree.LeetCode94;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
*
* 给定一个整数数组 nums 和一个整数目标值 target请你在该数组中找出 和为目标值 target  的那 两个 整数并返回它们的数组下标
*
* 你可以假设每种输入只会对应一个答案但是数组中同一个元素在答案里不能重复出现
*
* 你可以按任意顺序返回答案
*
* 来源力扣LeetCode
* 链接https://leetcode.cn/problems/two-sum
* 著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*
*
*
* 输入nums = [2,7,11,15], target = 9
* 输出[0,1]
* 解释因为 nums[0] + nums[1] == 9 返回 [0, 1]
*
*
* @author whaifree
* @package cn.whaifree.leetCode
* @Date: 2022/9/13 20:54
*/
public class Test {
static {
num = 20;
// 防止在初始化前程序中访问默认值
}
private static int num = 10;
public static void main(String[] args) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
System.out.println(systemClassLoader);
ClassLoader extClassLoader = systemClassLoader.getParent();
System.out.println(extClassLoader);
// 获取不到引导类类加载器
ClassLoader bootClassLoader = extClassLoader.getParent();
System.out.println(bootClassLoader);
ClassLoader classLoader = Test.class.getClassLoader();
System.out.println(classLoader);
// 系统核心类库都是使用引导类BootStrapClassLoader进行加载都为null
ClassLoader stringLoader = String.class.getClassLoader();
System.out.println(stringLoader);
PriorityQueue<Integer> objects = new PriorityQueue<>();
objects.add(1);
objects.add(2);
objects.add(3);
objects.add(4);
System.out.println(objects.poll());
System.out.println(objects.poll());
}
}

View File

@ -14,6 +14,9 @@ import java.util.*;
*/
public class LeetCode94 {
@Test
public void test() {
TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4});