问题描述
小M在分析员工的工作时间。他认为当某一天的工作小时数超过 8 小时时,这一天就是「劳累的一天」。他想找出最长的一段连续工作日,在这段时间内,「劳累的天数」严格大于「不劳累的天数」。你需要返回这个「表现良好时间段」的最大长度。
测试样例
样例1:
输入:
hours = [9,9,6,0,6,6,9]
输出:3
样例2:
输入:
hours = [6,6,6,8]
输出:0
样例3:
输入:
hours = [10,10,10,0,0,9]
输出:6
import java.util.Arrays;
public class Main {
public static int solution(int[] hours) {
// PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
// write code here
int len=hours.length,i,j;
int [] arr=new int[len];
int[][] graph=new int[len][len];
for(i=0;i<len;i++){
if(hours[i]>8){
arr[i]=1;
graph[i][i]=1;
}else {
arr[i]=-1;
graph[i][i]=-1;
}
}
for(i=0;i<len;i++){
for(j=i+1;j<len;j++){
graph[i][j]+=arr[j]+graph[i][j-1];
}
}
int max=Integer.MIN_VALUE;
for(i=0;i<len;i++){
j=len-1;
while(graph[i][j]<=0){
j--;
if(j<i){
break;
}
}
max=Math.max(max,j-i+1);
}
System.out.println(max);
return max;
}
public static void main(String[] args) {
System.out.println(solution(new int[]{9, 9, 6, 0, 6, 6, 9}) == 3);
System.out.println(solution(new int[]{6, 6, 6, 8}) == 0);
System.out.println(solution(new int[]{10, 10, 10, 0, 0, 9}) == 6);
}
}