modify code

pull/6/head
algorithmzuo 5 years ago
parent 4aa0bf04d8
commit 11a70b58ec

@ -8,6 +8,11 @@ public class DC3 {
public int[] height;
// 构造方法的约定:
// 数组叫nums如果你是字符串请转成整型数组nums
// 数组中,最小值>=1
// 如果不满足,处理成满足的,也不会影响使用
// max, nums里面最大值是多少
public DC3(int[] nums, int max) {
sa = sa(nums, max);
rank = rank();
@ -152,7 +157,7 @@ public class DC3 {
// 为了测试
public static void main(String[] args) {
int len = 3000000;
int len = 100000;
int maxValue = 100;
long start = System.currentTimeMillis();
new DC3(randomArray(len, maxValue), maxValue);

@ -1,6 +1,6 @@
package class44;
package class45;
public class Code02_InsertS2MakeMostAlphabeticalOrder {
public class Code01_InsertS2MakeMostAlphabeticalOrder {
// 暴力方法
public static String right(String s1, String s2) {

@ -1,7 +1,7 @@
package class44;
package class45;
// 测试链接: https://leetcode.com/problems/create-maximum-number/
public class Code03_CreateMaximumNumber {
public class Code02_CreateMaximumNumber {
public static int[] maxNumber1(int[] nums1, int[] nums2, int k) {
int len1 = nums1.length;

@ -1,4 +1,4 @@
package class44;
package class45;
// 最长公共子串问题是面试常见题目之一
// 假设str1长度Nstr2长度M
@ -9,7 +9,7 @@ package class44;
// 进而用sa数组去生成height数组
// 而且在生成的时候,还有一个不回退的优化,都非常不容易理解
// 这就是后缀数组在面试算法中的地位 : 德高望重的噩梦
public class Code04_LongestCommonSubstringConquerByHeight {
public class Code03_LongestCommonSubstringConquerByHeight {
public static int lcs1(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) {

@ -0,0 +1,100 @@
package class45;
import java.util.LinkedList;
// 给定一个数组arr和一个正数M
// 返回在子数组长度不大于M的情况下最大的子数组累加和
public class Code04_MaxSumLengthNoMore {
// O(N^2)的解法,暴力解,用作对数器
public static int test(int[] arr, int M) {
if (arr == null || arr.length == 0 || M < 1) {
return 0;
}
int N = arr.length;
int max = Integer.MIN_VALUE;
for (int L = 0; L < N; L++) {
int sum = 0;
for (int R = L; R < N; R++) {
if (R - L + 1 > M) {
break;
}
sum += arr[R];
max = Math.max(max, sum);
}
}
return max;
}
// O(N)的解法,最优解
public static int maxSum(int[] arr, int M) {
if (arr == null || arr.length == 0 || M < 1) {
return 0;
}
int N = arr.length;
int[] sum = new int[N];
sum[0] = arr[0];
for (int i = 1; i < N; i++) {
sum[i] = sum[i - 1] + arr[i];
}
LinkedList<Integer> qmax = new LinkedList<>();
int i = 0;
int end = Math.min(N, M);
for (; i < end; i++) {
while (!qmax.isEmpty() && sum[qmax.peekLast()] <= sum[i]) {
qmax.pollLast();
}
qmax.add(i);
}
int max = sum[qmax.peekFirst()];
int L = 0;
for (; i < N; L++, i++) {
if (qmax.peekFirst() == L) {
qmax.pollFirst();
}
while (!qmax.isEmpty() && sum[qmax.peekLast()] <= sum[i]) {
qmax.pollLast();
}
qmax.add(i);
max = Math.max(max, sum[qmax.peekFirst()] - sum[L]);
}
for (; L < N - 1; L++) {
if (qmax.peekFirst() == L) {
qmax.pollFirst();
}
max = Math.max(max, sum[qmax.peekFirst()] - sum[L]);
}
return max;
}
// 用作测试
public static int[] randomArray(int len, int max) {
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = (int) (Math.random() * max) - (int) (Math.random() * max);
}
return arr;
}
// 用作测试
public static void main(String[] args) {
int maxN = 50;
int maxValue = 100;
int testTime = 1000000;
System.out.println("测试开始");
for (int i = 0; i < testTime; i++) {
int N = (int) (Math.random() * maxN);
int M = (int) (Math.random() * maxN);
int[] arr = randomArray(N, maxValue);
int ans1 = test(arr, M);
int ans2 = maxSum(arr, M);
if (ans1 != ans2) {
System.out.println(ans1);
System.out.println(ans2);
System.out.println("Oops!");
}
}
System.out.println("测试结束");
}
}

@ -1,4 +1,4 @@
package class45;
package class46;
public class Code01_BurstBalloons {

@ -1,4 +1,4 @@
package class45;
package class46;
public class Code02_RemoveBoxes {

@ -1,4 +1,4 @@
package class45;
package class46;
// 如果一个字符相邻的位置没有相同字符,那么这个位置的字符出现不能被消掉
// 比如:"ab"其中a和b都不能被消掉

@ -1,4 +1,4 @@
package class46;
package class47;
public class Code01_StrangePrinter {

@ -1,4 +1,4 @@
package class46;
package class47;
// 整型数组arr长度为n(3 <= n <= 10^4),最初每个数字是<=200的正数且满足如下条件
// 1. 0位置的要求arr[0]<=arr[1]
Loading…
Cancel
Save