modify code on class

pull/6/head
左程云 5 years ago
parent 8ac241db52
commit f7f20b40d1

@ -18,16 +18,14 @@ public class Code01_Light {
if (index == str.length) { // 结束的时候 if (index == str.length) { // 结束的时候
for (int i = 0; i < str.length; i++) { for (int i = 0; i < str.length; i++) {
if (str[i] != 'X') { // 当前位置是点的话 if (str[i] != 'X') { // 当前位置是点的话
if (!lights.contains(i - 1) if (!lights.contains(i - 1) && !lights.contains(i) && !lights.contains(i + 1)) {
&& !lights.contains(i)
&& !lights.contains(i + 1)) {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
} }
} }
return lights.size(); return lights.size();
} else { // str还没结束 } else { // str还没结束
// i X . // i X .
int no = process(str, index + 1, lights); int no = process(str, index + 1, lights);
int yes = Integer.MAX_VALUE; int yes = Integer.MAX_VALUE;
if (str[index] == '.') { if (str[index] == '.') {
@ -41,20 +39,20 @@ public class Code01_Light {
public static int minLight2(String road) { public static int minLight2(String road) {
char[] str = road.toCharArray(); char[] str = road.toCharArray();
int index = 0; int i = 0;
int light = 0; int light = 0;
while (index < str.length) { while (i < str.length) {
if (str[index] == 'X') { if (str[i] == 'X') {
index++; i++;
} else { // i -> . } else {
light++; light++;
if (index + 1 == str.length) { if (i + 1 == str.length) {
break; break;
} else { } else { // 有i位置 i+ 1 X .
if (str[index + 1] == 'X') { if (str[i + 1] == 'X') {
index = index + 2; i = i + 2;
} else { } else {
index = index + 3; i = i + 3;
} }
} }
} }

@ -4,6 +4,7 @@ import java.util.PriorityQueue;
public class Code02_LessMoneySplitGold { public class Code02_LessMoneySplitGold {
// 纯暴力!
public static int lessMoney1(int[] arr) { public static int lessMoney1(int[] arr) {
if (arr == null || arr.length == 0) { if (arr == null || arr.length == 0) {
return 0; return 0;
@ -11,6 +12,8 @@ public class Code02_LessMoneySplitGold {
return process(arr, 0); return process(arr, 0);
} }
// 等待合并的数都在arr里pre之前的合并行为产生了多少总代价
// arr中只剩一个数字的时候停止合并返回最小的总代价
public static int process(int[] arr, int pre) { public static int process(int[] arr, int pre) {
if (arr.length == 1) { if (arr.length == 1) {
return pre; return pre;

@ -15,6 +15,7 @@ public class Code03_BestArrange {
} }
} }
// 暴力!所有情况都尝试!
public static int bestArrange1(Program[] programs) { public static int bestArrange1(Program[] programs) {
if (programs == null || programs.length == 0) { if (programs == null || programs.length == 0) {
return 0; return 0;
@ -22,8 +23,8 @@ public class Code03_BestArrange {
return process(programs, 0, 0); return process(programs, 0, 0);
} }
// 还剩什么会议都放在programs里 // 还剩下的会议都放在programs里
// done 之前已经安排了多少会议,数量 // done之前已经安排了多少会议的数量
// timeLine目前来到的时间点是什么 // timeLine目前来到的时间点是什么
// 目前来到timeLine的时间点已经安排了done多的会议剩下的会议programs可以自由安排 // 目前来到timeLine的时间点已经安排了done多的会议剩下的会议programs可以自由安排
@ -32,7 +33,7 @@ public class Code03_BestArrange {
if (programs.length == 0) { if (programs.length == 0) {
return done; return done;
} }
// 还有会议可以选择 // 还剩下会议
int max = done; int max = done;
// 当前安排的会议是什么会,每一个都枚举 // 当前安排的会议是什么会,每一个都枚举
for (int i = 0; i < programs.length; i++) { for (int i = 0; i < programs.length; i++) {
@ -55,10 +56,12 @@ public class Code03_BestArrange {
return ans; return ans;
} }
// 会议的开始时间和结束时间,都是数值,不会 < 0
public static int bestArrange2(Program[] programs) { public static int bestArrange2(Program[] programs) {
Arrays.sort(programs, new ProgramComparator()); Arrays.sort(programs, new ProgramComparator());
int timeLine = 0; int timeLine = 0;
int result = 0; int result = 0;
// 依次遍历每一个会议,结束时间早的会议先遍历
for (int i = 0; i < programs.length; i++) { for (int i = 0; i < programs.length; i++) {
if (timeLine <= programs[i].start) { if (timeLine <= programs[i].start) {
result++; result++;

@ -5,6 +5,10 @@ import java.util.PriorityQueue;
public class Code04_IPO { public class Code04_IPO {
// 最多K个项目
// W是初始资金
// Profits[] Capital[] 一定等长
// 返回最终最大的资金
public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) { public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator()); PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator()); PriorityQueue<Program> maxProfitQ = new PriorityQueue<>(new MaxProfitComparator());

@ -31,12 +31,14 @@ public class Code05_UnionFind {
} }
} }
// 给你一个节点,请你往上到不能再往上,把代表返回
public Node<V> findFather(Node<V> cur) { public Node<V> findFather(Node<V> cur) {
Stack<Node<V>> path = new Stack<>(); Stack<Node<V>> path = new Stack<>();
while (cur != parents.get(cur)) { while (cur != parents.get(cur)) {
path.push(cur); path.push(cur);
cur = parents.get(cur); cur = parents.get(cur);
} }
// cur == parents.get(cur)
while (!path.isEmpty()) { while (!path.isEmpty()) {
parents.put(path.pop(), cur); parents.put(path.pop(), cur);
} }

Loading…
Cancel
Save