09 2024 档案

摘要:https://leetcode.cn/problems/koko-eating-bananas/description/ 二段性:速度有得完和不吃完两个段关键点是编写check函数,比较繁杂 class Solution { public int minEatingSpeed(int[] pile 阅读全文
posted @ 2024-09-27 15:40 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/find-peak-element/description/ class Solution { public int findPeakElement(int[] nums) { // 需要依据题意来证明一个定理 // i~n-1中切nums[ 阅读全文
posted @ 2024-09-27 02:05 风乐 阅读(16) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/split-array-largest-sum/description/ 比较难的二分,关键点在于看出二段性,段数越多最大值越小,段数越小最大值越大,二分最大值,然后就是最大值的合法性校验(判断段数<=k),用于二分的check class 阅读全文
posted @ 2024-09-26 17:42 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/trapping-rain-water/description/大厂经典题,接雨水暴力双指针->预处理优化->单调栈 暴力: class Solution { public int trap(int[] height) { // 暴力解法: 阅读全文
posted @ 2024-09-23 00:46 风乐 阅读(10) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/next-greater-element-ii/description/ class Solution { public int[] nextGreaterElements(int[] nums) { // 成环的单调栈题,思路1:将nums 阅读全文
posted @ 2024-09-16 03:45 风乐 阅读(15) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/next-greater-element-i/description/ 根据校验nums2中的元素是否存在于nums1中 的时机 分为不同做法 class Solution { public int[] nextGreaterElement( 阅读全文
posted @ 2024-09-15 17:17 风乐 阅读(11) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/daily-temperatures/description/ 经典单调栈,关键难点在于如何利用单调栈这个数据结构解题题意要求向右找到第一个比当前大的元素,若是暴力则是O(n^2),但是依据暴力的这个思想可以利用单调栈优化,因为求右边第一个比 阅读全文
posted @ 2024-09-15 16:45 风乐 阅读(23) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/all-paths-from-source-to-target/description/ class Solution { List<List<Integer>> res = new ArrayList<>(); int[][] g; // 阅读全文
posted @ 2024-09-14 01:32 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/daily-temperatures/ class Solution { public int[] dailyTemperatures(int[] temperatures) { // 经典单调栈 // 核心思想就是及时去除无用数据,保证栈中 阅读全文
posted @ 2024-09-14 00:02 风乐 阅读(17) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/longest-palindromic-subsequence/description/ class Solution { public int longestPalindromeSubseq(String s) { // f[i][j]表示 阅读全文
posted @ 2024-09-11 03:48 风乐 阅读(18) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/palindromic-substrings/ 经典题,本题有双指针和dp两种做法,dp的定义是f[i][j]表示s[i:j]是回文串容易联想到递推方程f[i][j]=f[i+1][j-1] && s[i]==s[j]又因为1个字符或者两个相 阅读全文
posted @ 2024-09-11 02:23 风乐 阅读(20) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/edit-distance/ class Solution { public int minDistance(String word1, String word2) { // 经典题编辑距离 // f[i][j]表示word1前i个字符中选择 阅读全文
posted @ 2024-09-10 22:18 风乐 阅读(31) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/delete-operation-for-two-strings/solutions/ 两种做法,1.直接dp 2.转换题意,思考成LCS class Solution { public int minDistance(String word 阅读全文
posted @ 2024-09-10 21:43 风乐 阅读(15) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/distinct-subsequences/submissions/563375885/ 这题比较有难度,具体不太好想到,需要以是否选择s[i]来划分子集这位描述的很清楚,不做过多赘述 class Solution { public int 阅读全文
posted @ 2024-09-10 16:21 风乐 阅读(9) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/is-subsequence/description/ class Solution { public boolean isSubsequence(String s, String t) { // 依据题意,可以判断是求最长公共子序列的特殊情 阅读全文
posted @ 2024-09-08 14:08 风乐 阅读(10) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/maximum-subarray/description/ class Solution { public int maxSubArray(int[] nums) { // f[i]表示以第i个数为结尾的最大连续子数组和 // 以是否选择第i 阅读全文
posted @ 2024-09-08 02:22 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/uncrossed-lines/ class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { // 依据题意,稍加思考可知,是求最长公共子序列 // f[ 阅读全文
posted @ 2024-09-08 02:08 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/longest-common-subsequence/description/经典题,老题回顾 class Solution { public int longestCommonSubsequence(String text1, String 阅读全文
posted @ 2024-09-07 15:29 风乐 阅读(21) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/maximum-length-of-repeated-subarray/ 难点是在于状态定义,需要考虑到以第i个数为结尾,以第j个数为结尾的最长重复子数组 这样的定义而递推就很简单,只需要发生重复时+1即可,和之前的一维的,即最长子数组一样 阅读全文
posted @ 2024-09-07 15:28 风乐 阅读(23) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/longest-continuous-increasing-subsequence/description/ class Solution { public int findLengthOfLCIS(int[] nums) { // f[i] 阅读全文
posted @ 2024-09-07 14:23 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/longest-increasing-subsequence/description/ class Solution { public int lengthOfLIS(int[] nums) { // f[i]表示以第i个数为结尾的最长严格上 阅读全文
posted @ 2024-09-07 14:04 风乐 阅读(7) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ class Solution { public int maxProfit(int[] prices, int fee) { // f 阅读全文
posted @ 2024-09-07 13:52 风乐 阅读(12) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ class Solution { public int maxProfit(int[] prices) { // f[i][ 阅读全文
posted @ 2024-09-07 03:59 风乐 阅读(19) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/ class Solution { public int maxProfit(int k, int[] prices) { // 由于可以交易k次,以多少次持有来划分状态 阅读全文
posted @ 2024-09-07 02:44 风乐 阅读(9) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/description/ 这一题较难,难点是状态比较多,需要考虑两笔交易,则共5个状态需要被记录,用当前是否持有股票来划分子集进行计算 class Solution { 阅读全文
posted @ 2024-09-06 01:53 风乐 阅读(13) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/ class Solution { public: int maxProfit(vector<int>& prices) { vector<int> 阅读全文
posted @ 2024-09-06 00:39 风乐 阅读(7) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/ 经典股票题,此题有贪心做法 class Solution { public int maxProfit(int[] prices) { int res = 0; int min 阅读全文
posted @ 2024-09-06 00:13 风乐 阅读(24) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/house-robber-iii/description/基础树形dp,要点是f的定义灵神讲的很好:https://www.bilibili.com/video/BV1vu4y1f7dn/?vd_source=1bb76d0400eba0d4 阅读全文
posted @ 2024-09-05 02:06 风乐 阅读(6) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/house-robber-ii/description/ 灵神题解: https://leetcode.cn/problems/house-robber-ii/solutions/2445622/jian-ji-xie-fa-zhi-jie- 阅读全文
posted @ 2024-09-04 19:49 风乐 阅读(22) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/word-break/description/ class Solution { public boolean wordBreak(String s, List<String> wordDict) { // 思路较为巧妙,和传统背包定义不同 阅读全文
posted @ 2024-09-04 19:14 风乐 阅读(21) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/perfect-squares/description/ 简单完全背包,需要注意的是由于求的是最小,因此初始化时需要把初始层f[0]全置为无穷大,用于保证一定能计算出min具体可以看灵神的解释 递归边界:dfs(0,0)=0,因为没有数可以选 阅读全文
posted @ 2024-09-04 02:48 风乐 阅读(32) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/coin-change/description/ 代码上比较麻烦的dp题,由于求的是最少数量,因此求答案时需要初始化无穷大来计算 class Solution { public int coinChange(int[] coins, int 阅读全文
posted @ 2024-09-03 21:01 风乐 阅读(14) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/combination-sum-iv/description/ 此篇题解解释了为什么不能直接用二维完全背包的方式做不过还是建议把这个题当成一个爬楼梯来做 class Solution { public: int combinationSum4 阅读全文
posted @ 2024-09-03 19:35 风乐 阅读(16) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/coin-change-ii/description/可以直接考虑用完全背包的传统二维做法,但是这里求的是组合数,需要注意 在求装满背包有几种方案的时候,认清遍历顺序是非常关键的。 如果求组合数就是外层for循环遍历物品,内层for遍历背包。 阅读全文
posted @ 2024-09-03 16:55 风乐 阅读(9) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/ones-and-zeroes/solutions/ 多重体积的01背包,关键是需要想到把构造这个最长子集 等价为 往一个背包里塞物品,求能塞最多的物品是多少?且这里有两层体积这样想就能转化为01背包了,即f[i][j][k]表示在前i个物品 阅读全文
posted @ 2024-09-03 03:19 风乐 阅读(27) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/target-sum/solutions/2119041/jiao-ni-yi-bu-bu-si-kao-dong-tai-gui-hua-s1cx/ 灵神的代码实现比我自己写的更好,可以多学习学习这道题的关键点在于想到 正数和+负数和=ta 阅读全文
posted @ 2024-09-03 02:15 风乐 阅读(29) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/last-stone-weight-ii/description/ 思路较为巧妙的dp题,关键点在于如何将问题转化为01背包,有点贪心的思想主要是划分为两堆尽可能相等的石碓,然后判断能否凑出这个偏小的石碓(若干石头中选,能否选出这个价值)这里 阅读全文
posted @ 2024-09-02 20:51 风乐 阅读(33) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/partition-equal-subset-sum/description/ 01背包问题,需要考虑到如何把这个问题转化成01背包问题转换成01背包问题后,如何定义f[i]状态来表示 这里有两种方式:1.按照传统01背包表示,即前i个物品中 阅读全文
posted @ 2024-09-01 20:22 风乐 阅读(16) 评论(0) 推荐(0)
摘要:https://leetcode.cn/problems/unique-binary-search-trees/solutions/329807/bu-tong-de-er-cha-sou-suo-shu-by-leetcode-solution/ 较为困难的一道DP题,需要分析各个情况,以及想到如 阅读全文
posted @ 2024-09-01 00:10 风乐 阅读(19) 评论(0) 推荐(0)