# 美團2018校園招聘內推筆試程式碼分享

### 首先是第一題：

[1,2,3,4,5]，整數K，滿足條件的子串是{5},{2,3},{1,2,3,4},{1,2,3,4,5}。

``````package com.nowcoder.java;
public class Meituan01 {
public static void main(String[] args) {
int[] arr={1,2,3};
int K = 5;
System.out.println(cal(arr,K));
}
public static int cal(int[] arr,int K){
if(arr.length==0)
return 0;
int max=0;
int[] dp = new int[arr.length];
dp[0] = arr[0]%K;
for(int i=1;i<dp.length;i  ){
dp[i]=(dp[i-1] arr[i])%K;
}
for(int i=0;i<dp.length;i  ){
if(dp[i]==0)
max=i 1;
}
for(int i=1;i<K;i  ){
int pre = 0;
int back = dp.length-1;
while(dp[pre]!=i && pre<back)
pre  ;
while(dp[back]!=i && back>pre)
back--;
int len=back-pre;
if(len>max)
max=len;
}
return max;
}
}
``````

### 第二題：

``````package com.nowcoder.java;
import java.util.Arrays;
public class Meituan02 {
public static void main(String[] args) {
int N=2;
int[] arr = new int[]{10,10,20};
System.out.println(check(arr,N));
}
public static boolean check(int[] arr,int N){
if(N<=1)
return false;
Arrays.sort(arr);
int sum = 0;
for(int i=0;i<N-1;i  ){
sum =arr[i];
}
if(sum>=arr[N-1])
return true;
else{
return false;
}
}
}
``````