From c7d33f5e4715e41be3bda4c1765aee3d15de1cc0 Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Sat, 16 Jul 2022 15:54:16 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=20II=20099.=20?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=B7=AF=E5=BE=84=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/gatsby/offerII/_99MinPathSum.java | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/com/gatsby/offerII/_99MinPathSum.java diff --git a/src/com/gatsby/offerII/_99MinPathSum.java b/src/com/gatsby/offerII/_99MinPathSum.java new file mode 100644 index 0000000..dd4ec34 --- /dev/null +++ b/src/com/gatsby/offerII/_99MinPathSum.java @@ -0,0 +1,46 @@ +package com.gatsby.offerII; + +/** + * @author -- gatsby + * @date -- 2022/7/16 + * @description -- 剑指 Offer II 099. 最小路径之和 + */ + + +public class _99MinPathSum { + public int minPathSum(int[][] grid) { + int row = grid.length; + int col = grid[0].length; + if (row == 1) { + int sum = 0; + for (int i : grid[0]) { + sum += i; + } + return sum; + } + + if (col == 1) { + int sum = 0; + for (int[] i : grid) { + sum += i[0]; + } + return sum; + } + + int[][] dp = new int[row][col]; + dp[0][0] = grid[0][0]; + for (int i = 1; i < row; ++i) { + dp[i][0] = grid[i][0] + dp[i - 1][0]; + } + for (int i = 1; i < col; ++i) { + dp[0][i] = dp[0][i - 1] + grid[0][i]; + } + + for (int i = 1; i < row; ++i) { + for (int j = 1; j < col; ++j) { + dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; + } + } + return dp[row - 1][col - 1]; + } +} \ No newline at end of file From 6b24ee75dddb4d4a1b6fa09e47e779a3771ce9a6 Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Sat, 16 Jul 2022 16:10:35 +0800 Subject: [PATCH 2/4] =?UTF-8?q?2171.=20=E6=8B=BF=E5=87=BA=E6=9C=80?= =?UTF-8?q?=E5=B0=91=E6=95=B0=E7=9B=AE=E7=9A=84=E9=AD=94=E6=B3=95=E8=B1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...171RemovingMinimumNumberOIfMagicBeans.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/com/gatsby/_2171RemovingMinimumNumberOIfMagicBeans.java diff --git a/src/com/gatsby/_2171RemovingMinimumNumberOIfMagicBeans.java b/src/com/gatsby/_2171RemovingMinimumNumberOIfMagicBeans.java new file mode 100644 index 0000000..87a4765 --- /dev/null +++ b/src/com/gatsby/_2171RemovingMinimumNumberOIfMagicBeans.java @@ -0,0 +1,33 @@ +package com.gatsby; + +import java.util.*; + +/** + * @author -- gatsby + * @date -- 2022/7/9 + * @description -- 2171. 拿出最少数目的魔法豆 + */ + + +public class _2171RemovingMinimumNumberOIfMagicBeans { + public long minimumRemoval(int[] beans) { + Arrays.sort(beans); + long res = Integer.MAX_VALUE; + long sum = 0; + // Set beanSet = new HashSet<>(); + for (int bean : beans) { + sum += bean; + // beanSet.add(bean); + } + int n = beans.length; + // for (int bean: beanSet) { + // res = Math.min(res, sum - ()) + // } + for (int i = 0; i < n; ++i) { + System.out.println(beans[i]); + if (i != n - 1 && beans[i] == beans[i + 1]) continue; + res = Math.min(res, sum - (long) beans[i] * (beans.length - i)); + } + return res; + } +} \ No newline at end of file From 5d66332120c593c5ee49a0432c8972032551596b Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Sat, 23 Jul 2022 00:05:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?1529.=20=E6=9C=80=E5=B0=91=E7=9A=84?= =?UTF-8?q?=E5=90=8E=E7=BC=80=E7=BF=BB=E8=BD=AC=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/gatsby/_1529MinimumSuffixFlips.java | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/com/gatsby/_1529MinimumSuffixFlips.java diff --git a/src/com/gatsby/_1529MinimumSuffixFlips.java b/src/com/gatsby/_1529MinimumSuffixFlips.java new file mode 100644 index 0000000..4b7547d --- /dev/null +++ b/src/com/gatsby/_1529MinimumSuffixFlips.java @@ -0,0 +1,24 @@ +package com.gatsby; + +/** + * @author -- gatsby + * @date -- 2022/7/22 + * @description -- leetcode 1529 最少的后缀翻转次数 + */ + + +public class _1529MinimumSuffixFlips { + public int minFlips(String target) { + if (target.length() == 0) return 0; + int count = 0; + char base = target.charAt(0); + if (base == '1') count++; + for (int i = 1; i < target.length(); ++i) { + if (target.charAt(i) != base) { + count++; + base = target.charAt(i); + } + } + return count; + } +} \ No newline at end of file From 23931a32606c0c32fadb3a53c737428f2ee974e6 Mon Sep 17 00:00:00 2001 From: BGMer7 Date: Fri, 29 Jul 2022 01:25:33 +0800 Subject: [PATCH 4/4] =?UTF-8?q?MergeSort=20=E5=BD=92=E5=B9=B6=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/gatsby/utils/SortUtil.java | 50 ++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/com/gatsby/utils/SortUtil.java b/src/com/gatsby/utils/SortUtil.java index 1547ccf..ecb393d 100644 --- a/src/com/gatsby/utils/SortUtil.java +++ b/src/com/gatsby/utils/SortUtil.java @@ -1,5 +1,7 @@ package com.gatsby.utils; +import java.util.Arrays; + /** * @ClassName: SortUtil * @Description: 排序类 @@ -8,13 +10,15 @@ */ public class SortUtil { + static int[] tmp; + /** - * @MethodName: quickSort + * @MethodName: quickSort * @Parameter: [nums, begin, end] * @Return void * @Description: 快速排序 * @author: Gatsby - * @date: 2022/7/25 15:20 + * @date: 2022/7/25 15:20 */ public static void quickSort(int[] nums, int begin, int end) { if (begin < end) { @@ -43,6 +47,48 @@ public static void quickSort(int[] nums, int begin, int end) { quickSort(nums, j + 1, end); } } + + + public static int[] mergeSort(int[] nums) { + tmp = new int[nums.length]; + merge(nums, 0, nums.length - 1); + return nums; + } + + private static void merge(int[] nums, int left, int right) { + if (left >= right) return; + + int mid = (right + left) >> 1; + merge(nums, left, mid); + merge(nums, mid + 1, right); + + int i = left; + // 如果数组只有一个那就不会操作了 + int j = mid + 1; + int count = 0; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) { + tmp[count++] = nums[i++]; + } else { + tmp[count++] = nums[j++]; + } + } + // System.out.println(Arrays.toString(tmp)); + if (i <= mid) { + System.arraycopy(nums, i, tmp, count, mid - i + 1); + } + + if (j <= right) { + System.arraycopy(nums, j, tmp, count, right - j + 1); + } + System.arraycopy(tmp, 0, nums, left, right - left + 1); + } + + public static void main(String[] args) { + int[] nums = {6, 2, 5, 3, 8, 3, 1, 6, 3, 4}; + nums = mergeSort(nums); + System.out.println(Arrays.toString(nums)); + } }