# 一道知乎的编程题

0

3
Computer 3 3
English 20 1
Math 3 2
3
Computer 3 3
English 6 3
Math 6 3

Computer
Math
English
3
Computer
English
Math

(Hint：

luoyin500 | 初学一级 | 园豆：89

Shendu.cc 1年前

@Shendu.cc: 看到标题“一道XXX的编程题”我就猜到是他了...

0

···java
package cnblogs.bestcourseseq;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class BestCourseSeq
{
public String bestSeq(int[][] cs)
{
int n = cs.length;
StringBuilder[] allSeq = allSeq(n);
int max = Integer.MIN_VALUE;
List<String> results = new ArrayList<>();

``````    for(StringBuilder sb: allSeq)
{
boolean[] complete = new boolean[n];
int[][] courses = new int[cs.length][cs[0].length];
for(int x = 0; x < cs.length; x++)
{
for(int y = 0; y < cs[0].length; y++)
{
courses[x][y] = cs[x][y];
}
}
for(int i = 0; i < sb.length(); i++)
{
int temp = Integer.parseInt(String.valueOf(sb.charAt(i)));
for(int j = 0; j < courses.length; j++)
{
if(!complete[j])
{
courses[j][0] -= courses[temp-1][1];
}
}

complete[temp - 1] = true;
}

int tempDays = 0;
for(int i = 0; i < courses.length; i++)
{
if(courses[i][0] < 0)
{
tempDays += courses[i][0];
}
}

if(tempDays >= max)
{
if(tempDays > max)
{
max = tempDays;
results.clear();
}
else
{
}
}
}

int min = Integer.MAX_VALUE;
for(int i = 0; i < results.size(); i++)
{
if(Integer.parseInt(results.get(i)) < min)
{
min = Integer.parseInt(results.get(i));
}
}

return String.valueOf(min);
}

public StringBuilder[] allSeq(int n)
{
if(n == 1)
{
return new StringBuilder[]{new StringBuilder(n+"")};
}

StringBuilder[] nseq = new StringBuilder[factorial(n)];
StringBuilder[] nminus1seq = allSeq(n-1);
int k = 0;

for(int i = 0; i < nminus1seq.length; i++)
{
for(int j = 0; j < n; j++)
{
StringBuilder temp = new StringBuilder(nminus1seq[i].toString());
temp.insert(j, n+"");
nseq[k++] = temp;
}
}

return nseq;
}

public int factorial(int n)
{
if(n == 1)
{
return 1;
}

return factorial(n - 1) * n;
}

@Test
public void test()
{
String[] courses = {"course1","course2","course3"};
int[][] arr = {{3,3},{6,3},{6,3}};
String seq = bestSeq(arr);
for(int i = 0; i < seq.length(); i++)
{
int temp = Integer.parseInt(String.valueOf(seq.charAt(i)));
System.out.println(courses[temp-1]);
}
}``````

}
```

您需要登录以后才能回答，未注册用户请先注册