avatar
文章
590
标签
104
分类
17

首页
归档
标签
分类
友链
日志
byu_rself
搜索
首页
归档
标签
分类
友链
日志

byu_rself

LC.P416[分割等和子集]
发表于2025-04-07|更新于2025-04-07|LeetCode|DFS•动态规划•记忆化搜索•数组
LC.P416[分割等和子集] 方法一:记忆化搜索1234567891011121314151617181920212223242526272829303132class Solution { private int[][] cache; private int[] nums; private int n; public boolean canPartition(int[] nums) { int sum = 0; for (int num : nums) sum += num; if (sum % 2 != 0) { return false; } this.n = nums.length; this.nums = nums; cache = new int[n][sum / 2 + 1]; for (int[] c : cache) { Arrays.fi ...
LC.P2874[有序三元组中的最大值II]
发表于2025-04-02|更新于2025-04-02|LeetCode|数组•前缀和•贪心
LC.P2874[有序三元组中的最大值II] 方法一:贪心+前后缀数组1234567891011121314151617class Solution { public long maximumTripletValue(int[] nums) { int n = nums.length; int[] leftMax = new int[n]; // [0, i)的最大值 int[] rightMax = new int[n]; // (i, n)的最大值 for (int i = 1; i < n; ++i) { leftMax[i] = Math.max(leftMax[i - 1], nums[i - 1]); rightMax[n - i - 1] = Math.max(rightMax[n - i], nums[n - i]); } long ans = 0; for (int j = 1 ...
LC.P3128[直角三角形]
发表于2024-08-02|更新于2024-08-02|LeetCode|数组•枚举•计数
LC.P3128[直角三角形] 方法一:乘法原理12345678910111213141516171819202122class Solution { public long numberOfRightTriangles(int[][] grid) { int m = grid.length, n = grid[0].length; int[] rows = new int[m]; int[] cols = new int[n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { rows[i] += grid[i][j]; cols[j] += grid[i][j]; } } long ans = 0; for (int i = 0; i ...
LCP.P40[心算挑战]
发表于2024-08-01|更新于2024-08-01|LeetCodeLCP|数组•贪心•排序
LCP.P40[心算挑战] 方法一:排序+贪心123456789101112131415161718192021222324252627282930313233343536class Solution { int[] cards; int cnt, n; public int maxmiumScore(int[] cards, int cnt) { this.cards = cards; this.cnt = cnt; Arrays.sort(cards); this.n = cards.length; int sum = 0; for (int i = n - cnt; i < n; ++i) { sum += cards[i]; } if (sum % 2 == 0) return sum; int x = cards[n - cnt]; int an ...
LC.P3115[质数的最大距离]
发表于2024-07-02|更新于2024-07-02|LeetCode|数组
LC.P3115[质数的最大距离] 方法一:遍历1234567891011121314151617181920212223242526class Solution { public int maximumPrimeDifference(int[] nums) { int n = nums.length, left = 0, right = 0; for (int i = 0; i < n; ++i) { if (isPrime(nums[i])) { left = i; break; } } for (int i = n - 1; i >= 0; --i) { if (isPrime(nums[i])) { right = i; break; ...
LC.P2225[找出输掉零场或一场比赛的玩家]
发表于2024-05-22|更新于2024-05-22|LeetCode|数组•哈希表•排序
LC.P2225[找出输掉零场或一场比赛的玩家] 方法一:哈希表+排序12345678910111213141516171819class Solution { public List<List<Integer>> findWinners(int[][] matches) { Map<Integer, Integer> map = new HashMap<>(); for (int[] m : matches) { int a = m[0], b = m[1]; map.putIfAbsent(a, 0); map.merge(b, 1, Integer::sum); } List<Integer> a = new ArrayList<>(), b = new ArrayList<>(); for (Map.Entry< ...
LC.P826[安排工作以达到最大收益]
发表于2024-05-17|更新于2024-05-17|LeetCode|数组•双指针•排序
LC.P826[安排工作以达到最大收益] 方法一:排序+双指针12345678910111213141516171819class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { int n = difficulty.length; int[][] index = new int[n][2]; for (int i = 0; i < n; ++i) { index[i] = new int[]{difficulty[i], profit[i]}; } Arrays.sort(index, (a, b) -> a[0] - b[0]); Arrays.sort(worker); int ans = 0, j = 0, maxProfit = 0; f ...
LC.P994[腐烂的橘子]
发表于2024-05-13|更新于2024-05-13|LeetCode|数组•BFS
LC.P994[腐烂的橘子] 方法一:BFS12345678910111213141516171819202122232425262728293031323334class Solution { static final int dirs[][] = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; public int orangesRotting(int[][] grid) { int m = grid.length, n = grid[0].length, ans = 0, cnt = 0; Deque<int[]> queue = new ArrayDeque<>(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { ...
LC.P377[组合总和IV]
发表于2024-04-22|更新于2024-04-22|LeetCode|DFS•动态规划•记忆化搜索
LC.P377[组合总和IV] 方法一:记忆化搜索这道题本质是LC.P70[爬楼梯] 123456789101112131415161718192021222324class Solution { int[] cache; int[] nums; public int combinationSum4(int[] nums, int target) { cache = new int[target + 1]; this.nums = nums; Arrays.fill(cache, -1); return dfs(target); } private int dfs(int i) { if (i == 0) return 1; if (cache[i] != -1) return cache[i]; int ans = 0; for (int x : nums) { if (x ...
LC.P216[组合总和III]
发表于2024-04-22|更新于2024-04-22|LeetCode|DFS•回溯
LC.P216[组合总和III] 方法一:回溯+剪枝12345678910111213141516171819202122232425262728293031class Solution { List<List<Integer>> ans = new ArrayList<>(); List<Integer> path = new ArrayList<>(); int k; public List<List<Integer>> combinationSum3(int k, int n) { this.k = k; dfs(1, n); return ans; } private void dfs(int i, int s) { if (s == 0) { if (path.size() == k) { a ...
12…59
avatar
byu_rself
努力努力!
文章
590
标签
104
分类
17
Follow Me
最新文章
LC.P416[分割等和子集]2025-04-07
LC.P2874[有序三元组中的最大值II]2025-04-02
LC.P3128[直角三角形]2024-08-02
LCP.P40[心算挑战]2024-08-01
LC.P3115[质数的最大距离]2024-07-02
分类
  • LeetCode546
    • LCP4
    • LCR13
    • 剑指Offer13
    • 面试题2
  • Linux3
  • 后端9
    • CompletableFuture1
标签
GitDFSGolang动态规划记忆化搜索字符串栈数学数组哈希表滑动窗口链表递归图BFS多源BFS双指针树子数组前缀和前缀树字典树Trie子序列区间DP递推模拟枚举字符串哈希二分查找贪心排序负二进制回溯二叉树状态压缩子串迭代随机化后缀和
归档
  • 四月 20252
  • 八月 20242
  • 七月 20241
  • 五月 20243
  • 四月 20242
  • 三月 202410
  • 二月 202410
  • 一月 202414
网站资讯
文章数目 :
590
已运行时间 :
本站总字数 :
289.3k
本站访客数 :
本站总访问量 :
最后更新时间 :
©2023 - 2025 By byu_rself
框架 Hexo|主题 Butterfly
Hi, welcome to my blog!
搜索
数据库加载中