From 13a1a09aafce7c3fdfa1d76084eb0433ee98f683 Mon Sep 17 00:00:00 2001 From: fanxb Date: Thu, 11 Apr 2024 23:26:28 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Test.java | 64 +++++++++++++++++++ 5.leetcode/src/com/fanxb/common/TreeNode.java | 4 +- .../src/{ => com/fanxb/common/p100}/Q17.java | 2 +- 5.leetcode/src/com/fanxb/common/p100/Q50.java | 26 ++++++++ 5.leetcode/src/com/fanxb/common/p100/Q70.java | 12 ++++ .../com/fanxb/common/{ => p1000}/Q909.java | 2 +- .../com/fanxb/common/{ => p1000}/Q918.java | 2 +- .../com/fanxb/common/{ => p1000}/Q930.java | 4 +- .../com/fanxb/common/{ => p1100}/Q1078.java | 40 ++++++------ .../src/com/fanxb/common/p200/Q139.java | 46 +++++++++++++ .../src/com/fanxb/common/p200/Q149.java | 44 +++++++++++++ .../src/com/fanxb/common/p200/Q198.java | 12 ++++ .../com/fanxb/common/{ => p2000}/F1710.java | 5 +- .../com/fanxb/common/{ => p2000}/Q1449.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1576.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1663.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1688.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1705.java | 3 +- .../com/fanxb/common/{ => p2000}/Q1711.java | 4 +- .../com/fanxb/common/{ => p2000}/Q1744.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1833.java | 2 +- .../com/fanxb/common/{ => p2000}/Q1846.java | 2 +- .../src/com/fanxb/common/p300/Q332.java | 25 ++++++++ .../src/com/fanxb/common/p400/Q300.java | 31 +++++++++ .../src/com/fanxb/common/{ => p800}/Q739.java | 3 +- .../src/com/fanxb/common/{ => p800}/Q763.java | 2 +- .../src/com/fanxb/common/{ => p800}/Q783.java | 4 +- .../src/com/fanxb/common/{ => p900}/Q825.java | 6 +- .../src/com/fanxb/common/{ => p900}/Q852.java | 2 +- .../src/com/fanxb/common/{ => p900}/Q877.java | 2 +- 30 files changed, 302 insertions(+), 57 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Test.java rename 5.leetcode/src/{ => com/fanxb/common/p100}/Q17.java (96%) create mode 100644 5.leetcode/src/com/fanxb/common/p100/Q50.java rename 5.leetcode/src/com/fanxb/common/{ => p1000}/Q909.java (93%) rename 5.leetcode/src/com/fanxb/common/{ => p1000}/Q918.java (94%) rename 5.leetcode/src/com/fanxb/common/{ => p1000}/Q930.java (92%) rename 5.leetcode/src/com/fanxb/common/{ => p1100}/Q1078.java (91%) create mode 100644 5.leetcode/src/com/fanxb/common/p200/Q139.java rename 5.leetcode/src/com/fanxb/common/{ => p2000}/F1710.java (92%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1449.java (97%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1576.java (97%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1663.java (98%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1688.java (95%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1705.java (97%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1711.java (92%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1744.java (98%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1833.java (94%) rename 5.leetcode/src/com/fanxb/common/{ => p2000}/Q1846.java (96%) create mode 100644 5.leetcode/src/com/fanxb/common/p300/Q332.java rename 5.leetcode/src/com/fanxb/common/{ => p800}/Q739.java (94%) rename 5.leetcode/src/com/fanxb/common/{ => p800}/Q763.java (97%) rename 5.leetcode/src/com/fanxb/common/{ => p800}/Q783.java (97%) rename 5.leetcode/src/com/fanxb/common/{ => p900}/Q825.java (88%) rename 5.leetcode/src/com/fanxb/common/{ => p900}/Q852.java (96%) rename 5.leetcode/src/com/fanxb/common/{ => p900}/Q877.java (96%) diff --git a/5.leetcode/src/com/fanxb/common/Test.java b/5.leetcode/src/com/fanxb/common/Test.java new file mode 100644 index 0000000..9cd4c00 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Test.java @@ -0,0 +1,64 @@ +package com.fanxb.common; + +import java.util.Arrays; + +public class Test { + + private void quickSort(int[] array, int start, int end) { + if (start >= end) return; + int base = array[start]; + int l = start, r = end; + while (l < r) { + while (array[r] >= base && r > l) r--; + while (array[l] <= base && l < r) l++; + if (l < r) swap(array, l, r); + } + swap(array, start, l); + quickSort(array, start, l - 1); + quickSort(array, l + 1, end); + } + + private void bubble(int[] array) { + int n = array.length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (array[j + 1] < array[j]) swap(array, j + 1, j); + } + } + } + + private void insert(int[] array) { + int n = array.length; + for (int i = 1; i < n; i++) { + for (int j = i; j > 0 && array[j] < array[j - 1]; j--) swap(array, j, j - 1); + } + } + + private void swap(int[] array, int a, int b) { + if (a == b) return; + int temp = array[b]; + array[b] = array[a]; + array[a] = temp; + } + + private void shell(int[] array, int a) { + if (a <= 0) return; + int n = array.length; + for (int i = 0; i < a; i++) { + for (int j = i + a; j < n; j += a) { + for (int k = j; k > i && array[k] < array[k - a]; k -= a) swap(array, k, k - a); + } + } + shell(array, a / 2); + } + + public static void main(String[] args) { + Test test = new Test(); + int[] arr = new int[]{1, 53, 12, 4344, 112, -22, 333211, 3333, 444, 55511, 121}; +// test.quickSort(arr, 0, arr.length - 1); +// test.bubble(arr); + test.insert(arr); +// test.shell(arr, arr.length / 2); + System.out.println(Arrays.toString(arr)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/TreeNode.java b/5.leetcode/src/com/fanxb/common/TreeNode.java index 0b831bb..75d5594 100644 --- a/5.leetcode/src/com/fanxb/common/TreeNode.java +++ b/5.leetcode/src/com/fanxb/common/TreeNode.java @@ -8,11 +8,11 @@ public class TreeNode { TreeNode() { } - TreeNode(int val) { + public TreeNode(int val) { this.val = val; } - TreeNode(int val, TreeNode left, TreeNode right) { + public TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; diff --git a/5.leetcode/src/Q17.java b/5.leetcode/src/com/fanxb/common/p100/Q17.java similarity index 96% rename from 5.leetcode/src/Q17.java rename to 5.leetcode/src/com/fanxb/common/p100/Q17.java index 83c2986..20928d0 100644 --- a/5.leetcode/src/Q17.java +++ b/5.leetcode/src/com/fanxb/common/p100/Q17.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p100; import java.util.ArrayList; import java.util.HashSet; diff --git a/5.leetcode/src/com/fanxb/common/p100/Q50.java b/5.leetcode/src/com/fanxb/common/p100/Q50.java new file mode 100644 index 0000000..945a4df --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/p100/Q50.java @@ -0,0 +1,26 @@ +package com.fanxb.common.p100; + +public class Q50 { + public double myPow(double x, int n) { + if (n < 0) return pow(1 / x, -n); + return pow(x, n); + } + + public double pow(double x, long n) { + if (n == 0) return 1; + if (n % 2 == 1) return pow(x, n - 1) * x; + double temp = pow(x, n / 2); + return temp * temp; + } + + public double pow1(double x, long n) { + double res = x; + while (n > 0) { + if ((n & 1) == 1) + res = res * x; + x = x * x; + n = n >>> 1; + } + return res; + } +} diff --git a/5.leetcode/src/com/fanxb/common/p100/Q70.java b/5.leetcode/src/com/fanxb/common/p100/Q70.java index 2965cc4..9355239 100644 --- a/5.leetcode/src/com/fanxb/common/p100/Q70.java +++ b/5.leetcode/src/com/fanxb/common/p100/Q70.java @@ -20,6 +20,18 @@ public class Q70 { return res; } + + public int climbStairs1(int n) { + if (n == 1) return 1; + int a = 1, b = 2; + for (int i = 3; i <= n; i++) { + int c = a+b; + a = b; + b = c; + } + return b; + } + public static void main(String[] args) { System.out.println(new Q70().climbStairs(5)); } diff --git a/5.leetcode/src/com/fanxb/common/Q909.java b/5.leetcode/src/com/fanxb/common/p1000/Q909.java similarity index 93% rename from 5.leetcode/src/com/fanxb/common/Q909.java rename to 5.leetcode/src/com/fanxb/common/p1000/Q909.java index 16df77c..38d8ede 100644 --- a/5.leetcode/src/com/fanxb/common/Q909.java +++ b/5.leetcode/src/com/fanxb/common/p1000/Q909.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p1000; import java.util.LinkedList; import java.util.Queue; diff --git a/5.leetcode/src/com/fanxb/common/Q918.java b/5.leetcode/src/com/fanxb/common/p1000/Q918.java similarity index 94% rename from 5.leetcode/src/com/fanxb/common/Q918.java rename to 5.leetcode/src/com/fanxb/common/p1000/Q918.java index f2c258e..75767cc 100644 --- a/5.leetcode/src/com/fanxb/common/Q918.java +++ b/5.leetcode/src/com/fanxb/common/p1000/Q918.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p1000; public class Q918 { public int maxSubarraySumCircular(int[] nums) { diff --git a/5.leetcode/src/com/fanxb/common/Q930.java b/5.leetcode/src/com/fanxb/common/p1000/Q930.java similarity index 92% rename from 5.leetcode/src/com/fanxb/common/Q930.java rename to 5.leetcode/src/com/fanxb/common/p1000/Q930.java index 3c6355b..a3d0bc0 100644 --- a/5.leetcode/src/com/fanxb/common/Q930.java +++ b/5.leetcode/src/com/fanxb/common/p1000/Q930.java @@ -1,8 +1,6 @@ -package com.fanxb.common; +package com.fanxb.common.p1000; -import java.util.Arrays; import java.util.HashMap; -import java.util.LinkedList; import java.util.Map; /** diff --git a/5.leetcode/src/com/fanxb/common/Q1078.java b/5.leetcode/src/com/fanxb/common/p1100/Q1078.java similarity index 91% rename from 5.leetcode/src/com/fanxb/common/Q1078.java rename to 5.leetcode/src/com/fanxb/common/p1100/Q1078.java index 9efd518..333cf92 100644 --- a/5.leetcode/src/com/fanxb/common/Q1078.java +++ b/5.leetcode/src/com/fanxb/common/p1100/Q1078.java @@ -1,20 +1,20 @@ -package com.fanxb.common; - -import java.util.LinkedList; - -/** - * @author fanxb - * @date 2021年12月26日 21:54 - */ -public class Q1078 { - public String[] findOcurrences(String text, String first, String second) { - LinkedList res = new LinkedList<>(); - String[] strs = text.split(" "); - for (int i = 0; i < strs.length - 2; i++) { - if (strs[i].equals(first) && strs[i + 1].equals(second)) { - res.add(strs[i + 2]); - } - } - return res.toArray(new String[0]); - } -} +package com.fanxb.common.p1100; + +import java.util.LinkedList; + +/** + * @author fanxb + * @date 2021年12月26日 21:54 + */ +public class Q1078 { + public String[] findOcurrences(String text, String first, String second) { + LinkedList res = new LinkedList<>(); + String[] strs = text.split(" "); + for (int i = 0; i < strs.length - 2; i++) { + if (strs[i].equals(first) && strs[i + 1].equals(second)) { + res.add(strs[i + 2]); + } + } + return res.toArray(new String[0]); + } +} diff --git a/5.leetcode/src/com/fanxb/common/p200/Q139.java b/5.leetcode/src/com/fanxb/common/p200/Q139.java new file mode 100644 index 0000000..5d9723c --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/p200/Q139.java @@ -0,0 +1,46 @@ +package com.fanxb.common.p200; + +import java.util.*; + +public class Q139 { + public boolean wordBreak(String s, List wordDict) { + boolean[] cache = new boolean[s.length()]; + return bfs(s, 0, new HashSet<>(wordDict), cache); + } + + private boolean bfs(String s, int i, Set wordSet, boolean[] cache) { + if (i >= s.length()) return true; + if (cache[i]) return false; + StringBuilder stringBuilder = new StringBuilder(); + for (int j = i; j < s.length(); j++) { + stringBuilder.append(s.charAt(j)); + if (!wordSet.contains(stringBuilder.toString())) continue; + boolean exist = bfs(s, j + 1, wordSet, cache); + if (exist) return true; + cache[j + 1] = true; + } + cache[i] = true; + return false; + } + + + public boolean wordBreak1(String s, List wordDict) { + int n = s.length(); + Set wordSet = new HashSet<>(wordDict); + boolean[] dp = new boolean[n + 1]; + dp[0] = true; + for (int i = 1; i <= n; i++) { + for (int j = 0; j < i; j++) { + if (dp[j] && wordSet.contains(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } + + public static void main(String[] args) { + System.out.println(new Q139().wordBreak("leetcode", Arrays.asList("leet", "code"))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/p200/Q149.java b/5.leetcode/src/com/fanxb/common/p200/Q149.java index 0905d7f..6cca15e 100644 --- a/5.leetcode/src/com/fanxb/common/p200/Q149.java +++ b/5.leetcode/src/com/fanxb/common/p200/Q149.java @@ -1,5 +1,8 @@ package com.fanxb.common.p200; +import java.util.HashMap; +import java.util.Map; + /** * Created with IntelliJ IDEA * 寻找旋转排序数组中的最小值 @@ -31,6 +34,47 @@ public class Q149 { return max; } + public int maxPoints1(int[][] points) { + int n = points.length; + if (n < 3) return points.length; + int res = 0; + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + int temp = 2; + for (int k = j + 1; k < n; k++) { + if ((points[k][0] - points[i][0]) * (points[j][1] - points[i][1]) == (points[k][1] - points[i][1]) * (points[j][0] - points[i][0])) + temp++; + } + res = Math.max(res, temp); + } + } + return res; + } + + public int maxPoints2(int[][] points) { + int n = points.length; + if (n < 3) return n; + int ans = 0; + for (int i = 0; i < n; i++) { + int max = 0; + Map map = new HashMap<>(); + for (int j = i + 1; j < n; j++) { + int x = points[j][0] - points[i][0], y = points[j][1] - points[i][1]; + int gcd = gcd(x, y); + String key = (x / gcd) + "/" + (y / gcd); + int count = map.getOrDefault(key, 1) + 1; + map.put(key, count); + max = Math.max(max, count); + } + ans = Math.max(ans, max); + } + return ans; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } + public static void main(String[] args) { System.out.println(new Q149().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}})); } diff --git a/5.leetcode/src/com/fanxb/common/p200/Q198.java b/5.leetcode/src/com/fanxb/common/p200/Q198.java index dcb720b..90399ff 100644 --- a/5.leetcode/src/com/fanxb/common/p200/Q198.java +++ b/5.leetcode/src/com/fanxb/common/p200/Q198.java @@ -17,4 +17,16 @@ public class Q198 { return res; } + public int rob1(int[] nums) { + int n = nums.length; + if (n == 1) return nums[0]; + int a = nums[0], b = Math.max(nums[0], nums[1]); + for (int i = 2; i < n; i++) { + int c = Math.max(b, a + nums[i]); + a = b; + b = c; + } + return b; + } + } diff --git a/5.leetcode/src/com/fanxb/common/F1710.java b/5.leetcode/src/com/fanxb/common/p2000/F1710.java similarity index 92% rename from 5.leetcode/src/com/fanxb/common/F1710.java rename to 5.leetcode/src/com/fanxb/common/p2000/F1710.java index fa04d08..2e19bbc 100644 --- a/5.leetcode/src/com/fanxb/common/F1710.java +++ b/5.leetcode/src/com/fanxb/common/p2000/F1710.java @@ -1,7 +1,4 @@ -package com.fanxb.common; - -import java.util.HashMap; -import java.util.Map; +package com.fanxb.common.p2000; /** * 两数相加 diff --git a/5.leetcode/src/com/fanxb/common/Q1449.java b/5.leetcode/src/com/fanxb/common/p2000/Q1449.java similarity index 97% rename from 5.leetcode/src/com/fanxb/common/Q1449.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1449.java index 79565ca..f77ee94 100644 --- a/5.leetcode/src/com/fanxb/common/Q1449.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1449.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; import java.util.Arrays; diff --git a/5.leetcode/src/com/fanxb/common/Q1576.java b/5.leetcode/src/com/fanxb/common/p2000/Q1576.java similarity index 97% rename from 5.leetcode/src/com/fanxb/common/Q1576.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1576.java index 54633c3..4fbf971 100644 --- a/5.leetcode/src/com/fanxb/common/Q1576.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1576.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; public class Q1576 { public String modifyString(String s) { diff --git a/5.leetcode/src/com/fanxb/common/Q1663.java b/5.leetcode/src/com/fanxb/common/p2000/Q1663.java similarity index 98% rename from 5.leetcode/src/com/fanxb/common/Q1663.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1663.java index 14e39fc..5187267 100644 --- a/5.leetcode/src/com/fanxb/common/Q1663.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1663.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; /** * 具有给定数值的最小字符串 diff --git a/5.leetcode/src/com/fanxb/common/Q1688.java b/5.leetcode/src/com/fanxb/common/p2000/Q1688.java similarity index 95% rename from 5.leetcode/src/com/fanxb/common/Q1688.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1688.java index 88d6799..c1d602a 100644 --- a/5.leetcode/src/com/fanxb/common/Q1688.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1688.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; public class Q1688 { private static int[] res = new int[201]; diff --git a/5.leetcode/src/com/fanxb/common/Q1705.java b/5.leetcode/src/com/fanxb/common/p2000/Q1705.java similarity index 97% rename from 5.leetcode/src/com/fanxb/common/Q1705.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1705.java index 5123788..29d32fd 100644 --- a/5.leetcode/src/com/fanxb/common/Q1705.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1705.java @@ -1,6 +1,5 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; -import java.util.Arrays; import java.util.Comparator; import java.util.PriorityQueue; diff --git a/5.leetcode/src/com/fanxb/common/Q1711.java b/5.leetcode/src/com/fanxb/common/p2000/Q1711.java similarity index 92% rename from 5.leetcode/src/com/fanxb/common/Q1711.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1711.java index f2e7bb2..b1d8c24 100644 --- a/5.leetcode/src/com/fanxb/common/Q1711.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1711.java @@ -1,9 +1,7 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; /** * 两数相加 diff --git a/5.leetcode/src/com/fanxb/common/Q1744.java b/5.leetcode/src/com/fanxb/common/p2000/Q1744.java similarity index 98% rename from 5.leetcode/src/com/fanxb/common/Q1744.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1744.java index 6530bca..e933de7 100644 --- a/5.leetcode/src/com/fanxb/common/Q1744.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1744.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; import java.util.Arrays; diff --git a/5.leetcode/src/com/fanxb/common/Q1833.java b/5.leetcode/src/com/fanxb/common/p2000/Q1833.java similarity index 94% rename from 5.leetcode/src/com/fanxb/common/Q1833.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1833.java index 8991c6a..7b3bcae 100644 --- a/5.leetcode/src/com/fanxb/common/Q1833.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1833.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; import java.util.*; diff --git a/5.leetcode/src/com/fanxb/common/Q1846.java b/5.leetcode/src/com/fanxb/common/p2000/Q1846.java similarity index 96% rename from 5.leetcode/src/com/fanxb/common/Q1846.java rename to 5.leetcode/src/com/fanxb/common/p2000/Q1846.java index e20aa90..b5c4767 100644 --- a/5.leetcode/src/com/fanxb/common/Q1846.java +++ b/5.leetcode/src/com/fanxb/common/p2000/Q1846.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p2000; import java.util.Arrays; diff --git a/5.leetcode/src/com/fanxb/common/p300/Q332.java b/5.leetcode/src/com/fanxb/common/p300/Q332.java new file mode 100644 index 0000000..83d5f23 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/p300/Q332.java @@ -0,0 +1,25 @@ +package com.fanxb.common.p300; + +import java.util.*; + +public class Q332 { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, -1); + Set set = new HashSet<>(); + for (int coin : coins) { + set.add(coin); + } + dp[0] = 0; + for (int i = 1; i <= amount; i++) { + int min = Integer.MAX_VALUE; + for (int j = 0; j < coins.length; j++) { + if (amount - coins[j] > 0 && dp[amount - coins[j]] >= 0) { + min = Math.min(dp[amount - coins[j]] + 1, min); + } + } + if (min != Integer.MAX_VALUE) dp[i] = min; + } + return dp[amount]; + } +} diff --git a/5.leetcode/src/com/fanxb/common/p400/Q300.java b/5.leetcode/src/com/fanxb/common/p400/Q300.java index a34aa99..567612b 100644 --- a/5.leetcode/src/com/fanxb/common/p400/Q300.java +++ b/5.leetcode/src/com/fanxb/common/p400/Q300.java @@ -1,5 +1,7 @@ package com.fanxb.common.p400; +import java.util.Arrays; + /** * 定义dp[i]为以nums[i]结尾的子序列最大长度 * @@ -39,6 +41,35 @@ public class Q300 { return res; } + public int better(int[] nums) { + int[] tails = new int[nums.length]; + int res = 0; + for (int num : nums) { + int l = 0, r = res; + while (l < r) { + int m = (l + r) / 2; + if (tails[m] < num) l = m + 1; + else r = m; + } + if (l == res) res++; + } + return res; + } + + public int bad(int[] nums) { + int[] dp = new int[nums.length]; + Arrays.fill(dp,1); + int res = 0; + for (int i = 1; i < nums.length; i++) { + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) dp[i] = Math.max(dp[j] + 1, dp[i]); + } + res = Math.max(res, dp[i]); + } + return res; + } + + public static void main(String[] args) { System.out.println(new Q300().lengthOfLIS(new int[]{0, 1, 0, 3, 2, 3})); System.out.println(new Q300().another(new int[]{0, 1, 0, 3, 2, 3})); diff --git a/5.leetcode/src/com/fanxb/common/Q739.java b/5.leetcode/src/com/fanxb/common/p800/Q739.java similarity index 94% rename from 5.leetcode/src/com/fanxb/common/Q739.java rename to 5.leetcode/src/com/fanxb/common/p800/Q739.java index 621efbb..8fb3557 100644 --- a/5.leetcode/src/com/fanxb/common/Q739.java +++ b/5.leetcode/src/com/fanxb/common/p800/Q739.java @@ -1,7 +1,6 @@ -package com.fanxb.common; +package com.fanxb.common.p800; import java.util.Arrays; -import java.util.Iterator; import java.util.LinkedList; /** diff --git a/5.leetcode/src/com/fanxb/common/Q763.java b/5.leetcode/src/com/fanxb/common/p800/Q763.java similarity index 97% rename from 5.leetcode/src/com/fanxb/common/Q763.java rename to 5.leetcode/src/com/fanxb/common/p800/Q763.java index d5ead8d..82140bc 100644 --- a/5.leetcode/src/com/fanxb/common/Q763.java +++ b/5.leetcode/src/com/fanxb/common/p800/Q763.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p800; import java.util.*; diff --git a/5.leetcode/src/com/fanxb/common/Q783.java b/5.leetcode/src/com/fanxb/common/p800/Q783.java similarity index 97% rename from 5.leetcode/src/com/fanxb/common/Q783.java rename to 5.leetcode/src/com/fanxb/common/p800/Q783.java index 679f008..22e7841 100644 --- a/5.leetcode/src/com/fanxb/common/Q783.java +++ b/5.leetcode/src/com/fanxb/common/p800/Q783.java @@ -1,6 +1,4 @@ -package com.fanxb.common; - -import java.util.Arrays; +package com.fanxb.common.p800; /** * Created with IntelliJ IDEA diff --git a/5.leetcode/src/com/fanxb/common/Q825.java b/5.leetcode/src/com/fanxb/common/p900/Q825.java similarity index 88% rename from 5.leetcode/src/com/fanxb/common/Q825.java rename to 5.leetcode/src/com/fanxb/common/p900/Q825.java index 64e6e12..79db8d3 100644 --- a/5.leetcode/src/com/fanxb/common/Q825.java +++ b/5.leetcode/src/com/fanxb/common/p900/Q825.java @@ -1,8 +1,4 @@ -package com.fanxb.common; - -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; +package com.fanxb.common.p900; public class Q825 { public int numFriendRequests(int[] ages) { diff --git a/5.leetcode/src/com/fanxb/common/Q852.java b/5.leetcode/src/com/fanxb/common/p900/Q852.java similarity index 96% rename from 5.leetcode/src/com/fanxb/common/Q852.java rename to 5.leetcode/src/com/fanxb/common/p900/Q852.java index 8140aba..6c4c5ef 100644 --- a/5.leetcode/src/com/fanxb/common/Q852.java +++ b/5.leetcode/src/com/fanxb/common/p900/Q852.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p900; /** * 两数相加 diff --git a/5.leetcode/src/com/fanxb/common/Q877.java b/5.leetcode/src/com/fanxb/common/p900/Q877.java similarity index 96% rename from 5.leetcode/src/com/fanxb/common/Q877.java rename to 5.leetcode/src/com/fanxb/common/p900/Q877.java index 02fb30a..d9d902e 100644 --- a/5.leetcode/src/com/fanxb/common/Q877.java +++ b/5.leetcode/src/com/fanxb/common/p900/Q877.java @@ -1,4 +1,4 @@ -package com.fanxb.common; +package com.fanxb.common.p900; /** * 两数相加