Skip to content

Commit

Permalink
feat 贪心算法
Browse files Browse the repository at this point in the history
  • Loading branch information
tianbin committed Nov 2, 2022
1 parent a485181 commit 20f702a
Show file tree
Hide file tree
Showing 21 changed files with 341 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/main/java/common/util/SysRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ public static int[] randomArrNaturalNum(int arrLength) {
}
return a;
}

public static int[] randomArrNaturalNum(int arrLength, int maxValue) {
int[] a = new int[arrLength];
for (int i = 0; i < a.length; i++) {
a[i] = randomInt(0, maxValue);
}
return a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private int merge(int[] arr, int left, int mid, int right) {
return smallSum;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(int[] arr) {
if (arr == null || arr.length < 2) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private int merge(int[] arr, int left, int mid, int right) {
return reversePairCount;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(int[] arr) {
int ans = 0;
for (int i = 0; i < arr.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private int merge(int[] arr, int left, int mid, int right) {
return res;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(int[] arr) {
int ans = 0;
for (int i = 0; i < arr.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void sortArrayDistanceLessK(int[] arr, int k) {
}
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private static int[] randomArrayNoMoveMoreK(int maxSize, int maxValue, int K) {
int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private int maxCover(int[][] lines) {
return ans;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(int[][] lines) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static ListNode midOrDownMidPreLinkedNode(ListNode head) {
return slow;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
public static ListNode right1(ListNode head) {
if (head == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private int maxWidth(TreeNode head) {
return 0;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(TreeNode head) {
if (head == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private class Info {
public boolean isBalance;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private boolean comparator(TreeNode head) {
boolean[] ans = new boolean[1];
ans[0] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private class Info {
public boolean isBST;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private boolean comparator(TreeNode head) {
if (head == null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private class Info {
public int maxDistance;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int comparator(TreeNode head) {
if (head == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected int maxSubBSTSize(TreeNode head) {
private class Info {
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int maxSubBSTSize1(TreeNode head) {
if (head == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private class Info {
public int height;
}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private boolean comparator(TreeNode head) {
if (head == null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private class Info {

}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private TreeNode maxSubBSTHead1(TreeNode head) {
if (head == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private class Info {
}


//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private TreeNode comparator(TreeNode head, TreeNode a, TreeNode b) {
if (head == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private class Info {

}

//-------------------------- 对数器 --------------------------//
//-------------------------- 比较器 --------------------------//
private int maxHappy1(Employee boss) {
if (boss == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package algorithmzuo.b_体系学习班.c0300_贪心;

import common.CommonConstants;
import common.util.SysRandom;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashSet;

/**
* Created by nibnait on 2022/11/02
*/
public class Code01_Light {

@Test
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) {
testCase();
}
}

@Test
public void testCase() {
int len = SysRandom.randomInt(0, 50);
String road = randomString(len);

int result = minLight(road);
Assert.assertEquals(comparator(road), result);
}

/**
* 最少放几盏灯
*/
private int minLight(String road) {
return 0;
}

//-------------------------- 比较器 --------------------------//
private String randomString(int len) {
char[] res = new char[(int) (Math.random() * len) + 1];
for (int i = 0; i < res.length; i++) {
res[i] = Math.random() < 0.5 ? 'X' : '.';
}
return String.valueOf(res);
}

private int comparator(String road) {
if (road == null || road.length() == 0) {
return 0;
}
return process(road.toCharArray(), 0, new HashSet<>());
}

// str[index....]位置,自由选择放灯还是不放灯
// str[0..index-1]位置呢?已经做完决定了,那些放了灯的位置,存在lights里
// 要求选出能照亮所有.的方案,并且在这些有效的方案中,返回最少需要几个灯
private int process(char[] str, int index, HashSet<Integer> lights) {
if (index == str.length) { // 结束的时候
for (int i = 0; i < str.length; i++) {
if (str[i] != 'X') { // 当前位置是点的话
if (!lights.contains(i - 1) && !lights.contains(i) && !lights.contains(i + 1)) {
return Integer.MAX_VALUE;
}
}
}
return lights.size();
} else { // str还没结束
// i X .
int no = process(str, index + 1, lights);
int yes = Integer.MAX_VALUE;
if (str[index] == '.') {
lights.add(index);
yes = process(str, index + 1, lights);
lights.remove(index);
}
return Math.min(no, yes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package algorithmzuo.b_体系学习班.c0300_贪心;

import common.CommonConstants;
import common.util.SysRandom;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by nibnait on 2022/11/02
*/
public class Code02_LessMoneySplitGold {

@Test
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) {
testCase();
}
}

@Test
public void testCase() {
int arrLen = SysRandom.random(20);
int[] arr = SysRandom.randomArrNaturalNum(arrLen, 1000);

int result = lessMoney(arr);
Assert.assertEquals(comparator(arr), result);
}

/**
* 花最少的钱 去分割金条
*/
private int lessMoney(int[] arr) {
return 0;
}

//-------------------------- 比较器 --------------------------//
public static int comparator(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
return process(arr, 0);
}

// 等待合并的数都在arr里,pre之前的合并行为产生了多少总代价
// arr中只剩一个数字的时候,停止合并,返回最小的总代价
public static int process(int[] arr, int pre) {
if (arr.length == 1) {
return pre;
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
ans = Math.min(ans, process(copyAndMergeTwo(arr, i, j), pre + arr[i] + arr[j]));
}
}
return ans;
}

public static int[] copyAndMergeTwo(int[] arr, int i, int j) {
int[] ans = new int[arr.length - 1];
int ansi = 0;
for (int arri = 0; arri < arr.length; arri++) {
if (arri != i && arri != j) {
ans[ansi++] = arr[arri];
}
}
ans[ansi] = arr[i] + arr[j];
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package algorithmzuo.b_体系学习班.c0300_贪心;

import common.CommonConstants;
import common.util.SysRandom;
import lombok.AllArgsConstructor;
import org.junit.Assert;
import org.junit.Test;

/**
* Created by nibnait on 2022/11/02
*/
public class Code03_BestArrange {

@Test
public void loopTestCase() {
for (int i = 0; i < CommonConstants.TEST_CASE_COUNT_1000; i++) {
testCase();
}
}

@Test
public void testCase() {
int programSize = SysRandom.randomInt(5, 20);
int timeMax = SysRandom.randomInt(10, 30);
Program[] programs = generatePrograms(programSize, timeMax);

int result = bestArrange(programs);
Assert.assertEquals(comparator(programs), result);
}

@AllArgsConstructor
private static class Program {
public int start;
public int end;
}

/**
* 最多可以安排几场会议?
*/
private int bestArrange(Program[] programs) {
return 0;
}

//-------------------------- 比较器 --------------------------//
public static Program[] generatePrograms(int programSize, int timeMax) {
Program[] ans = new Program[(int) (Math.random() * (programSize + 1))];
for (int i = 0; i < ans.length; i++) {
int r1 = (int) (Math.random() * (timeMax + 1));
int r2 = (int) (Math.random() * (timeMax + 1));
if (r1 == r2) {
ans[i] = new Program(r1, r1 + 1);
} else {
ans[i] = new Program(Math.min(r1, r2), Math.max(r1, r2));
}
}
return ans;
}

// 暴力!所有情况都尝试!
public static int comparator(Program[] programs) {
if (programs == null || programs.length == 0) {
return 0;
}
return process(programs, 0, 0);
}

// 还剩下的会议都放在programs里
// done之前已经安排了多少会议的数量
// timeLine目前来到的时间点是什么

// 目前来到timeLine的时间点,已经安排了done多的会议,剩下的会议programs可以自由安排
// 返回能安排的最多会议数量
public static int process(Program[] programs, int done, int timeLine) {
if (programs.length == 0) {
return done;
}
// 还剩下会议
int max = done;
// 当前安排的会议是什么会,每一个都枚举
for (int i = 0; i < programs.length; i++) {
if (programs[i].start >= timeLine) {
Program[] next = copyButExcept(programs, i);
max = Math.max(max, process(next, done + 1, programs[i].end));
}
}
return max;
}

public static Program[] copyButExcept(Program[] programs, int i) {
Program[] ans = new Program[programs.length - 1];
int index = 0;
for (int k = 0; k < programs.length; k++) {
if (k != i) {
ans[index++] = programs[k];
}
}
return ans;
}

}
Loading

0 comments on commit 20f702a

Please sign in to comment.