From bdd083a91254533272981d02ffac051f77ca4218 Mon Sep 17 00:00:00 2001 From: whai Date: Wed, 27 Mar 2024 21:52:49 +0800 Subject: [PATCH] lc --- README.md | 56 +++++++--- .../cn/whaifree/leetCode/Dynamic/KaMa47.java | 56 ++++++++++ .../leetCode/Dynamic/LeetCode322.java | 47 ++++++++ .../leetCode/Dynamic/LeetCode377.java | 101 ++++++++++++++++++ .../cn/whaifree/test/DirectoryToMarkdown.java | 65 +++++++++++ 5 files changed, 313 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/KaMa47.java create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode322.java create mode 100644 src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode377.java create mode 100644 src/main/java/cn/whaifree/test/DirectoryToMarkdown.java diff --git a/README.md b/README.md index 8a627a5..554196b 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,47 @@ -- [Algorithm](#algorithm) - - [Array](#array) - - [String](#string) - - [Math](#math) - - [Tree](#tree) - - [Backtracking](#backtracking) - - [Bit Manipulation](#bit-manipulation) - - [Union Find](#union-find) - - [Heap](#heap) - - [Greedy](#greedy) - - [Sort](#sort) - - [Divide and Conquer]() +- [Algorithm](./src/main/java/cn/whaifree) + - [LeetCode](./src/main/java/cn/whaifree/leetCode) + - [Array 链表](./src/main/java/cn/whaifree/leetCode/Array) + - [BackTracking 回溯法](./src/main/java/cn/whaifree/leetCode/Array/BackTracking) + - [Dynamic 动态规划](./src/main/java/cn/whaifree/leetCode/Array/Dynamic) + - [Greedy 贪心算法](./src/main/java/cn/whaifree/leetCode/Array/Greedy) + - [Hash 哈希](./src/main/java/cn/whaifree/leetCode/Array/Hash) + - [Other 其他 ](./src/main/java/cn/whaifree/leetCode/Array/LeetCode) + - [LinkedList 链表](./src/main/java/cn/whaifree/leetCode/Array/LinkedList) + - [model 模型类](./src/main/java/cn/whaifree/leetCode/Array/model) + - [Stack 栈](./src/main/java/cn/whaifree/leetCode/Array/Stack) + - [String 字符串](./src/main/java/cn/whaifree/leetCode/Array/String) + - [Tree 树](./src/main/java/cn/whaifree/leetCode/Array/Tree) + - [utils 工具](./src/main/java/cn/whaifree/leetCode/Array/utils) + - [Offer](./src/main/java/cn/whaifree/leetCode) + - [redo](./src/main/java/cn/whaifree/redo) + + +# 自己瞎写的公众号与博客 + + + +- 👋 Hi, I’m @whaibetter +- 👀 I am currently studying the postgraduate class of *Computer Technology* in Zhejiang university of technology(ZJUT) of China + - I studied the undergraduate class of ***Data science and Big data technology\* in Zhejiang university of science and technology(ZUST)** of China +- 🌴 I’m currently learning java and big data +- 💕 +- 🐦 How to reach me: + - wechat:***whaihalo*** + - ✉️ email: ***whaifree@163.com*** + +------ + +- 👋嗨,我是@whaibetter +- 👁 我目前就为在 浙江工业大学 就读*计算机技术* 专业硕士研究生 + - 我本科毕业于**浙江科技学院\*数据科学与大数据技术专业\*** +- 🌴 目前正在学习java和大数据 +- 💕 联系我? + - 微信:***whaihalo*** + - ✉️:邮箱:***whaifree@163.com*** + +whai的个人博客 whaifree.top 欢迎留言! + + diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/KaMa47.java b/src/main/java/cn/whaifree/leetCode/Dynamic/KaMa47.java new file mode 100644 index 0000000..50b1cfb --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/KaMa47.java @@ -0,0 +1,56 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/27 12:45 + * @注释 + */ +public class KaMa47 { + + @Test + public void test() + { + + int i = new Solution().plt(3, 2); + System.out.println(i); + } + + /* https://kamacoder.com/problempage.php?pid=1067 + */ + class Solution{ + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int capacity = scanner.nextInt(); + int m = scanner.nextInt(); + System.out.println(plt(capacity, m)); + } + + /** + * @param capacity 需要n阶 + * @param m 每一步可以走几个 + * @return + */ + public static int plt(int capacity, int m) { + + // 排列 + int[] dp = new int[capacity + 1]; + + dp[0] = 1; + for (int j = 0; j <= capacity; j++) { + for (int i = 1; i <= m; i++) { + if (j >= i) { + dp[j] += dp[j - i]; + } + } + } + return dp[capacity]; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode322.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode322.java new file mode 100644 index 0000000..2406a4f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode322.java @@ -0,0 +1,47 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/27 12:58 + * @注释 + */ +public class LeetCode322 { + @Test + public void test() { + int[] coins = {1,2,5}; + int amount = 11; + int i = new Solution().coinChange(coins, amount); + System.out.println(i); + } + + class Solution { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + + dp[0] = 0; // 总和为0的方案为0 + for (int i = 0; i < coins.length; i++) { + for (int j = coins[i]; j <= amount ; j++) { + //只有dp[j-coins[i]]不是初始最大值时,该位才有选择的必要 + if (dp[j - coins[i]] != Integer.MAX_VALUE) { + //选择硬币数目最小的情况 + // 不放 和 放(加上本次方案1) + dp[j] = Math.min(dp[j], dp[j - coins[i]] + 1); + } + } + } + + /** + * 输入:coins = [2], amount = 3 + * 输出:-1 + */ + return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount]; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode377.java b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode377.java new file mode 100644 index 0000000..f709e3a --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Dynamic/LeetCode377.java @@ -0,0 +1,101 @@ +package cn.whaifree.leetCode.Dynamic; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/27 11:46 + * @注释 + */ +public class LeetCode377 { + + @Test + public void test() { + int[] nums = {1, 2, 3}; + int target = 4; + int res = new Solution1().combinationSum4(nums, target); + System.out.println(res); + } + + + /** + * 超时 + */ + class Solution { + int sum = 0; + int res = 0; + public int combinationSum4(int[] nums, int target) { + backTracking(nums, target); + return res; + } + + public void backTracking(int[] nums, int target) { + if (sum == target) { + res++; + return; + } + if (sum > target) { + return; + } + + for (int i = 0; i < nums.length; i++) { + sum += nums[i]; + backTracking(nums, target); + sum -= nums[i]; + } + + } + + } + + /** + * dp 完全背包 + */ + class Solution1 { + + /** + * 1 2 3随意取,使得总包围target + * @param nums + * @param target + * @return + */ + public int combinationSum4(int[] nums, int target) { + + + // dp[j] 表示 从0-i-1任取,使得满足包容量为j的可能性 + /** + * 0 1 2 3 4 + * [1, 1, 0, 0, 0] + * [1, 1, 2, 0, 0] + * [1, 1, 2, 4, 0] + * [1, 1, 2, 4, 7] + */ + int[] dp = new int[target + 1]; + dp[0] = 1; // 都不放入 情况有一种 + for (int j = 1; j < target + 1; j++) { + for (int i = 0; i < nums.length; i++) { + if (j >= nums[i]) { + dp[j] = dp[j] + dp[j - nums[i]]; + // dp[j] 不放 + // dp[j - nums[i]] 放,放也有多种情况 + } + } + /** + * 如果把遍历nums(物品)放在外循环, + * 遍历target的作为内循环的话,举一个例子: + * 计算dp[4]的时候,结果集只有 {1,3} 这样的集合,忽略了{3,1} + */ + } + /** + * 如果求 组合数 就是外层for循环遍历物品,内层for遍历背包。 + * 如果求 排列数 就是外层for遍历背包,内层for循环遍历物品。 + */ + return dp[target]; + } + + + } + + +} diff --git a/src/main/java/cn/whaifree/test/DirectoryToMarkdown.java b/src/main/java/cn/whaifree/test/DirectoryToMarkdown.java new file mode 100644 index 0000000..db161dc --- /dev/null +++ b/src/main/java/cn/whaifree/test/DirectoryToMarkdown.java @@ -0,0 +1,65 @@ +//package cn.whaifree.test; +// +//import java.io.File; +//import java.io.FileWriter; +//import java.io.IOException; +//import java.util.Arrays; +//import java.util.List; +//import java.util.stream.Collectors; +// +///** +// * @version 1.0 +// * @Author whai文海 +// * @Date 2024/3/26 16:52 +// * @注释 +// */ +//public class DirectoryToMarkdown { +// public static void main(String[] args) { +// // 指定你要读取的文件夹路径 +// String directoryPath = "D:\\project\\LeetCode\\src"; +// +// // 生成Markdown文件的内容 +// String markdownContent = generateMarkdownContent(directoryPath); +// +// +// System.out.println(markdownContent); +// // 将Markdown内容写入文件 +// File markdownFile = new File("D:\\project\\LeetCode\\目录.md"); +// try (FileWriter writer = new FileWriter(markdownFile)) { +// writer.write(markdownContent); +// System.out.println("Markdown文件已生成: " + markdownFile.getAbsolutePath()); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// +// private static String generateMarkdownContent(String directoryPath) { +// File directory = new File(directoryPath); +// if (!directory.exists() || !directory.isDirectory()) { +// return "指定的路径不存在或不是一个文件夹"; +// } +// +// // 获取文件夹中的所有文件和子文件夹 +// File[] files = directory.listFiles(); +// if (files == null) { +// return "文件夹为空"; +// } +// +// // 使用流来构建Markdown列表 +// List links = Arrays.stream(files) +// .map(file -> { +// String fileName = file.getName(); +// String filePath = directoryPath + "/" + fileName; +// // 转换为Markdown格式的链接 +// return "- [" + fileName + "](#" + fileName.replace(" ", "-").toLowerCase() + ")\n"; +// }) +// .collect(Collectors.toList()); +// +// // 构建完整的Markdown内容 +// StringBuilder markdownBuilder = new StringBuilder(); +// markdownBuilder.append("# 目录\n\n"); +// links.forEach(markdownBuilder::append); +// +// return markdownBuilder.toString(); +// } +//}