-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tianbin
committed
Nov 2, 2022
1 parent
a485181
commit 20f702a
Showing
21 changed files
with
341 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/test/algorithmzuo/b_体系学习班/c0300_贪心/Code01_Light.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/test/algorithmzuo/b_体系学习班/c0300_贪心/Code02_LessMoneySplitGold.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/test/algorithmzuo/b_体系学习班/c0300_贪心/Code03_BestArrange.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.