华为1.24秋招笔试题

华为1.24秋招笔试题

1.题目1

题目详情 - 2024.1.24-华为秋招笔试-第一题-计算积分 - CodeFun2000

1.1题解

java 复制代码
import java.util.Scanner;

class Main{
     public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        char[] ch=s.toCharArray();
        int res=0;
        int i=0;
        int n=ch.length;
        while(i<n){
            int start=i;
            if(ch[i]=='r')res+=1;
            else if(ch[i]=='g')res+=2;
            else if(ch[i]=='b')res+=3;
            i++;
            while(i<n && ch[i]==ch[i-1]){
                 if(ch[i]=='r')res+=1;
                else if(ch[i]=='g')res+=2;
                else if(ch[i]=='b')res+=3;
                res+=(i-start);
                i++;
            }
        }
        System.out.println(res);




 }


}

1.2循环数组模板

  • 适用场景:需要对分段多数组中的多个子数组分段处理,每一段处理逻辑相同

  • 核心思想

    • 外层负责遍历组之前的准备工作,记录开始位置,更新答案
    • 内层负责遍历,找出这一组最远在哪结束
java 复制代码
int n = nums.length;
int i = 0;
while (i < n){
    start = i//外层
  
       //内层 
       while (i < n && ...){
     i += 1;
  
    }
   
        }

2.题目2

题目详情 - 2024.1.24-华为秋招笔试-第二题-大模型训练 - CodeFun2000

2.1思路分析

  • 这道题与lc 410 分割数组最大值一样,只不过进行了包装而已
  • 贪心+二分
  • 我们这里使用二分搜索算力的最低值 UP
    • up值越小,段数越多,时间越长;反之越少,时间越短
    • 如果在up的情况下,贪心划分出的段数小于 T 说明还可以继续降低up
    • 否则 需要增加up

2.2代码

java 复制代码
import java.util.*;

class Main{
    static int t;
     public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        t=sc.nextInt();
        int[] task=new int[n];
        int sum=0;
        int l=0;
        for(int i=0;i<n;i++){
            task[i]=sc.nextInt();
            l=Math.max(task[i],l);
            sum+=task[i];
        }
        
        int r=sum;
        while(l<r){
            int mid=l+r>>1;
            if(check(task,mid)){
                r=mid;
            }else l=mid+1;
            
        }
        System.out.println(l);




 }
   //贪心划分模板可以记一下
 static boolean check(int[] nums,int up){
        int cnt=1;//至少可以划分为1份
        int sum=0;
        for(int num:nums){
            if(sum+num>up){
                cnt++;
                sum=num;
            }else{
                sum+=num;
            }
        }
        return cnt<=t;
        
    }


}
相关推荐
leluckys14 小时前
swift- Swift中常见的面试题
开发语言·汇编·swift
BUG_MeDe14 小时前
json格式字符串解析的简单使用 libjson-c
c语言·开发语言·json
CoderCodingNo14 小时前
【GESP】C++五级练习题 luogu-P1182 数列分段 Section II
开发语言·c++·算法
阿珍爱上了阿强,在一个有星星的夜晚14 小时前
node后端页面性能监测分析
java·学习方法
Java程序之猿14 小时前
SpringBoot + camel+IBM MQ实现消息队列处理
java·spring boot·mybatis
z_鑫15 小时前
SpringCloud FeignClient 中 Bean 重复注册冲突解决方案解析
java·spring boot·spring cloud
孫治AllenSun15 小时前
【线程池】优化等待队列和拒绝策略
java·spring boot·spring cloud
毕设源码-邱学长16 小时前
【开题答辩全过程】以 基于Spring Boot的体育场地预约管理系统为例,包含答辩的问题和答案
java·spring boot·后端