字节跳动-2018-笔试-01

为了不断优化推荐效果,今日头条每天要存储和处理海量数据。假设有这样一种场景:我们对用户按照它们的注册时间先后来标号,对于一类文章,每个用户都有不同的喜好值,我们会想知道某一段时间内注册的用户(标号相连的一批用户)中,有多少用户对这类文章喜好值为k。因为一些特殊的原因,不会出现一个查询的用户区间完全覆盖另一个查询的用户区间(不存在L1<=L2<=R2<=R1)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import  java.util.*;
 
public  class  Main {
     public  static  void  main(String[] args) {
         Scanner sc =  new  Scanner(System.in);
         int  userTotal = sc.nextInt();
         HashMap<Integer, List<Integer>> like =  new  HashMap<>();
         for  ( int  i= 1 ; i<=userTotal; i++) {
             int  k = sc.nextInt();
             if  (like.containsKey(k)) {
                 List<Integer> list = like.get(k);
                 list.add(i);
             else  {
                 List<Integer> list =  new  ArrayList<>();
                 list.add(i);
                 like.put(k, list);
             }
         }
         int  groupTotal = sc.nextInt();
         List<Integer> result =  new  ArrayList<>();
         for ( int  i= 0 ; i<groupTotal; i++) {
             int  low = sc.nextInt();
             int  high = sc.nextInt();
             int  k = sc.nextInt();
             int  total =  0 ;
             List<Integer> list = like.get(k);
             if  (list !=  null ) {
                 for  (Integer integer : list) {
                     if  (integer >= low && integer <= high) total++;
                 }
             }
             result.add(total);
         }
         for  (Integer integer:result) {
             System.out.println(integer);
         }
     }
}



作为一个手串艺人,有金主向你订购了一条包含n个杂色串珠的手串——每个串珠要么无色,要么涂了若干种颜色。为了使手串的色彩看起来不那么单调,金主要求,手串上的任意一种颜色(不包含无色),在任意连续的m个串珠里至多出现一次(注意这里手串是一个环形)。手串上的颜色一共有c种。现在按顺时针序告诉你n个串珠的手串上,每个串珠用所包含的颜色分别有哪些。请你判断该手串上有多少种颜色不符合要求。即询问有多少种颜色在任意连续m个串珠中出现了至少两次。

import java.util.*;
public class Main{
public static void main(String args[]){

Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int c=sc.nextInt();
int arr[][]= new int[n][];
int arrCount[]= new int[n];
HashMap<Integer, List<Integer>> ma=new HashMap<>();
for(int i=0;i<n;i++){
int iCount=sc.nextInt();
arrCount[i]=iCount;
if(iCount==0){
;
}else{
for(int j=0;j<iCount;j++){
int x=sc.nextInt();
if(ma.containsKey(x)){
List<Integer> li= ma.get(x);

// List<Integer> list = like.get(k);
li.add(i);


}else{
List<Integer> li=new ArrayList<>();
li.add(i);
ma.put(x,li);
}
}
}


}
int flag=0;

//遍历map中的值
for (List<Integer> li: ma.values()) {
Collections.sort(li);
int f=li.get(0);
for(int k=1;k<li.size();k++){
if((f+m)>li.get(k)){
flag++;
break;
}
f=li.get(k);
}
//最后一位


}
System.out.println(flag);
}
}

字符串S由小写字母构成,长度为n。定义一种操作,每次都可以挑选字符串中任意的两个相邻字母进行交换。询问在至多交换m次之后,字符串中最多有多少个连续的位置上的字母相同?

猜你喜欢

转载自www.cnblogs.com/wen-/p/12670358.html
今日推荐