pull/3/head
Leo 5 years ago
parent 11d7fb7c56
commit a210e12948

@ -3,6 +3,7 @@ package leo.class04_07;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
/**
* @author Leo
@ -153,6 +154,49 @@ class CoverMax2 {
}
class CoverMax3{
static class Line{
private int start;
private int end;
public Line(int s, int end) {
this.start = s;
this.end = end;
}
}
static class StartComparator implements Comparator<Line>{
@Override
public int compare(Line o1, Line o2) {
return o1.start - o2.start;
}
}
public static int maxCover(int[][] arr) {
if (arr.length == 0 || arr == null) {
return 0;
}
Line[] lines = new Line[arr.length];
for (int i = 0; i < arr.length; i++) {
lines[i] = new Line(arr[i][0], arr[i][1]);
}
Arrays.sort(lines, new StartComparator());
PriorityQueue<Integer> queue = new PriorityQueue<>();
int max = 0;
for (int i = 0; i < lines.length; i++) {
if (!queue.isEmpty() && queue.peek() <= lines[i].start) {
queue.poll();
}
queue.add(lines[i].end);
max = Math.max(max, queue.size());
}
return max;
}
}
class CoverMaxForTest{
public static int maxCover(int[][] arr) {
@ -189,7 +233,7 @@ class CoverMaxMain {
for (int i = 0; i < testTime; i++) {
int[][] lines = generateLines(sizeMax, L, R);
int max = CoverMax2.maxCover(lines);
int max = CoverMax3.maxCover(lines);
int maxForTest = CoverMaxForTest.maxCover(lines);
if (max != maxForTest) {
System.out.println("max : " + max + " maxForTest : " + maxForTest);

@ -0,0 +1,78 @@
package leo.class05_08;
import leo.util.ArrayUtil;
import sun.jvm.hotspot.oops.BranchData;
import java.util.Arrays;
/**
* @author Leo
* @ClassName CountSort
* @DATE 2020/12/2 2:03
* @Description ()
*/
public class CountSort {
public static void countSort(int[] arr) {
if (arr.length < 2 || arr == null) {
return;
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
int[] bucket = new int[max + 1];
for (int i = 0; i < arr.length; i++) {
bucket[arr[i]]++;
}
int i = 0;
for (int j = 0; j < bucket.length; j++) {
while (bucket[j]-- > 0) {
arr[i++] = j;
}
}
}
public static void main(String[] args){
int maxSize = 100;
int range = 100;
int testTime = 10000;
System.out.println("start!");
for (int i = 0; i < testTime; i++) {
int[] arr = randomArray(maxSize, range);
int[] copyArray = copyArray(arr);
countSort(arr);
Arrays.sort(copyArray);
if (!ArrayUtil.isEqual(arr, copyArray)) {
ArrayUtil.printArr(arr, "arr");
ArrayUtil.printArr(copyArray, "copyArray");
System.out.println("fuck!");
break;
}
}
System.out.println("end!");
}
public static int[] randomArray(int maxSize, int range) {
int[] arr = new int[(int) (maxSize * Math.random() + 1)];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (range * Math.random()) + 1;
}
return arr;
}
public static int[] copyArray(int[] arr) {
int[] copyArr = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
copyArr[i] = arr[i];
}
return copyArr;
}
}

@ -0,0 +1,108 @@
package leo.class05_08;
import leo.util.ArrayUtil;
import java.util.Arrays;
/**
* @author Leo
* @ClassName RadixSort
* @DATE 2020/12/2 3:55
* @Description
* O(n*log10^max)
* O(n)
*/
public class RadixSort {
public static void radixSort(int[] arr) {
if (arr.length < 2 || arr == null) {
return;
}
radixSort(arr, 0, arr.length - 1);
}
private static void radixSort(int[] arr, int l, int r) {
if (l >= r) {
return;
}
int i, j = 0;
int bits = maxBits(arr, l, r);
final int radix = 10;
int[] help = new int[r - l + 1];
for (int b = 1; b <= bits; b++) {
int[] count = new int[radix];
for (i = l; i <= r; i++) {
j = getRemainder(arr[i], b);
count[j]++;
}
for (i = 1; i < count.length; i++) {
count[i] = count[i] + count[i - 1];
}
for (i = r; i >= l; i--) {
j = getRemainder(arr[i], b);
count[j]--;
help[count[j]] = arr[i];
}
for (i = 0; i <= r; i++) {
arr[i + l] = help[i];
}
}
}
private static int maxBits(int[] arr, int l, int r) {
int res = 0;
int max = Integer.MIN_VALUE;
for (int i = l; i <= r; i++) {
max = Math.max(max, arr[i]);
}
while (max != 0) {
max /= 10;
res++;
}
return res;
}
private static int getRemainder(int num, int b) {
return ((num / (int) Math.pow(10, b - 1)) % 10);
}
}
class RadixSortMain {
public static void main(String[] args){
int testTime = 1000;
int range = 1000;
int maxSize = 40;
System.out.println("start");
for (int i = 0; i < testTime; i++) {
int[] arr = randomArray(maxSize, range);
int[] copyArray = copyArray(arr);
Arrays.sort(copyArray);
RadixSort.radixSort(arr);
if (!ArrayUtil.isEqual(arr, copyArray)) {
System.out.println("fuck!");
}
}
System.out.println("end");
}
public static int[] randomArray(int maxSize, int range) {
int[] arr = new int[(int) (maxSize * Math.random()) + 1];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (range * Math.random()) + 1;
}
return arr;
}
public static int[] copyArray(int[] arr) {
int[] copyArr = new int[arr.length];
for (int i = 0; i < copyArr.length; i++) {
copyArr[i] = arr[i];
}
return copyArr;
}
}

@ -1,5 +1,7 @@
package leo.class05_08;
import java.util.HashMap;
/**
* @author Leo
* @ClassName TrieTree
@ -103,3 +105,117 @@ class TrieTree {
}
}
class TrieTree_Test{
private HashMap<String,Integer> map;
public TrieTree_Test(){
this.map = new HashMap<>();
}
public void insert(String word) {
if (word.trim().length() == 0 || word == null) {
return;
}
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
}else{
map.put(word, 1);
}
}
public void delete(String word) {
if (this.search(word) == 0) {
return;
}
if (this.map.get(word) == 1) {
map.remove(word);
}else{
this.map.put(word, this.map.get(word) - 1);
}
}
public int search(String word) {
if (word.trim().length() == 0 || word == null) {
return 0;
}
if (map.containsKey(word)) {
return map.get(word);
}
return 0;
}
public int prefixNumber(String word) {
if (word.trim().length() == 0 || word == null) {
return 0;
}
int count = 0;
for (String str : this.map.keySet()) {
if (str.startsWith(word)) {
count += this.map.get(str);
}
}
return count;
}
}
class TrieTree_Main {
public static void main(String[] args){
int strArrayLen = 100;
int strLen = 50;
int testTime = 10000;
System.out.println("start");
for (int i = 0; i < testTime; i++) {
TrieTree trieTree = new TrieTree();
TrieTree_Test test = new TrieTree_Test();
String[] strings = generateRandomString(strArrayLen, strLen);
for (int j = 0; j < strings.length; j++) {
int random = (int) Math.random();
if (random < 0.25) {
trieTree.insert(strings[j]);
test.insert(strings[j]);
}else if (random<0.5){
trieTree.delete(strings[j]);
test.delete(strings[j]);
}else if(random<0.75){
int mapCount = trieTree.search(strings[j]);
int testCount = test.search(strings[j]);
if (mapCount != testCount) {
System.out.println("mapCount : " + mapCount + " testCount : " + testCount);
System.out.println("count fuck!");
break;
}
}else{
int mapPreCount = trieTree.prefixNumber(strings[j]);
int testPreCount = test.prefixNumber(strings[j]);
if (mapPreCount != testPreCount) {
System.out.println("mapPreCount : " + mapPreCount + " testPreCount : " + testPreCount);
System.out.println("pre count fuck!");
break;
}
}
}
}
System.out.println("end");
}
public static String[] generateRandomString(int strArrayLen, int strLen) {
String[] strArray = new String[(int) (strArrayLen * Math.random() + 1)];
for (int i = 0; i < strArray.length; i++) {
strArray[i] = generateRandomString(strLen);
}
return strArray;
}
private static String generateRandomString(int strLen) {
char[] chars = new char[(int) (Math.random() * strLen + 1)];
for (int i = 0; i < chars.length; i++) {
int value = (int) (Math.random() * 6);
chars[i] = (char) (97 + value);
}
return String.valueOf(chars);
}
}

@ -0,0 +1,222 @@
package leo.class05_08;
import java.util.HashMap;
/**
* @author Leo
* @ClassName TrieTree_Map
* @DATE 2020/12/2 10:33
* @Description next map
*/
public class TrieTree_Map {
private class Node {
private int pass;
private int end;
private HashMap<Integer,Node> next;
public Node() {
this.pass = 0;
this.end = 0;
this.next = new HashMap<>();
}
}
private Node root;
public TrieTree_Map() {
this.root = new Node();
}
public void insert(String word) {
if (word == null || "".equals(word.trim())) {
return;
}
Node node = this.root;
root.pass++;
int path;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
path = chars[i];
if (node.next.get(path) == null) {
node.next.put(path, new Node());
}
node = node.next.get(path);
node.pass++;
}
node.end++;
}
public void delete(String word) {
if (search(word) == 0) {
return;
}
Node node = this.root;
node.pass--;
int path;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
path = chars[i];
if (--node.next.get(path).pass == 0) {
node.next.remove(path);
return;
}
node = node.next.get(path);
}
node.end--;
}
public int search(String word) {
if (word == null || "".equals(word.trim())) {
return 0;
}
Node node = this.root;
int path;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
path = chars[i];
if (node.next.get(path) == null) {
return 0;
}
node = node.next.get(path);
}
return node.end;
}
public int prefixNumber(String word) {
if (word == null || "".equals(word.trim())) {
return 0;
}
Node node = this.root;
char[] chars = word.toCharArray();
int path;
for (int i = 0; i < chars.length; i++) {
path = chars[i];
if (node.next.get(path) == null) {
return 0;
}
node = node.next.get(path);
}
return node.pass;
}
}
class TrieTree_Map_Test {
private HashMap<String,Integer> map;
public TrieTree_Map_Test(){
this.map = new HashMap<>();
}
public void insert(String word) {
if (word.trim().length() == 0 || word == null) {
return;
}
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
}else{
map.put(word, 1);
}
}
public void delete(String word) {
if (this.search(word) == 0) {
return;
}
if (this.map.get(word) == 1) {
map.remove(word);
}else{
this.map.put(word, this.map.get(word) - 1);
}
}
public int search(String word) {
if (word.trim().length() == 0 || word == null) {
return 0;
}
if (map.containsKey(word)) {
return map.get(word);
}
return 0;
}
public int prefixNumber(String word) {
if (word.trim().length() == 0 || word == null) {
return 0;
}
int count = 0;
for (String str : this.map.keySet()) {
if (str.startsWith(word)) {
count += this.map.get(str);
}
}
return count;
}
}
class TrieTree_MapMain{
public static void main(String[] args){
int strArrayLen = 100;
int strLen = 50;
int testTime = 10000;
System.out.println("start");
for (int i = 0; i < testTime; i++) {
TrieTree_Map trieTree_map = new TrieTree_Map();
TrieTree_Map_Test test = new TrieTree_Map_Test();
String[] strings = generateRandomString(strArrayLen, strLen);
for (int j = 0; j < strings.length; j++) {
int random = (int) Math.random();
if (random < 0.25) {
trieTree_map.insert(strings[j]);
test.insert(strings[j]);
}else if (random<0.5){
trieTree_map.delete(strings[j]);
test.delete(strings[j]);
}else if(random<0.75){
int mapCount = trieTree_map.search(strings[j]);
int testCount = test.search(strings[j]);
if (mapCount != testCount) {
System.out.println("mapCount : " + mapCount + " testCount : " + testCount);
System.out.println("count fuck!");
break;
}
}else{
int mapPreCount = trieTree_map.prefixNumber(strings[j]);
int testPreCount = test.prefixNumber(strings[j]);
if (mapPreCount != testPreCount) {
System.out.println("mapPreCount : " + mapPreCount + " testPreCount : " + testPreCount);
System.out.println("pre count fuck!");
break;
}
}
}
}
System.out.println("end");
}
public static String[] generateRandomString(int strArrayLen, int strLen) {
String[] strArray = new String[(int) (strArrayLen * Math.random() + 1)];
for (int i = 0; i < strArray.length; i++) {
strArray[i] = generateRandomString(strLen);
}
return strArray;
}
private static String generateRandomString(int strLen) {
char[] chars = new char[(int) (Math.random() * strLen + 1)];
for (int i = 0; i < chars.length; i++) {
int value = (int) (Math.random() * 6);
chars[i] = (char) (97 + value);
}
return String.valueOf(chars);
}
}
Loading…
Cancel
Save