[See Weizhi Zhu for one question per day] Record a full AC in the weekly competition

2185. Count Strings Containing a Given Prefix

Given an array of strings wordsand a string pref.

Returns the number of strings wordsin a prefixedpref with .

The prefixs of the string is any leading consecutive string of .s

class Solution {
    
    
    public int prefixCount(String[] words, String pref) {
    
    
        int ans=0;
        for(String i:words){
    
    
            if(j(i,pref)){
    
    
                ans++;
            }
        }
        return ans;
    }
    
    public boolean j(String a,String b){
    
    
        for(int i=0;i<b.length();i++){
    
    
            if(i>=a.length()||a.charAt(i)!=b.charAt(i)){
    
    
                return false;
            }
        }
        return true;
    }
}

2186. Minimum steps to make two strings anagram\textcolor{orange}{2186. Minimum steps to make two strings anagram}2 1 8 6 . The minimum number of steps to make two character strings be anagrams of each other

Given two strings sand t. You can append any characters to or in one operation .t

Returns the minimum number of steps* required to make sand are anagramst of each other . *

An anagram is a string of letters that have the same letters but in a different (or identical) order.

class Solution {
    
    
    public int minSteps(String s, String t) {
    
    
        if(s.equals(t)){
    
    
            return 0;
        }
        int[] count=new int[26];
        for(char i:s.toCharArray()){
    
    
            count[(int)(i-'a')]++;
        }
        for(char i:t.toCharArray()){
    
    
            count[(int)(i-'a')]--;
        }
        int ans=0;
        for(int i:count){
    
    
            ans+=(int)Math.abs(i);
        }
        return ans;
    }
}

2187. Minimum time to complete the journey\textcolor{orange}{2187. Minimum time to complete the journey}2 1 8 7. Minimum time to complete the trip

Give you an array time, which time[i]represents the time it takes for the ifirst bus to complete a journey of **** .

Each bus can complete multiple journeys continuously , that is to say, after the current journey of a bus is completed, the next journey can be started immediately . Each bus operates independently , that is to say, multiple buses can be running at the same time without affecting each other.

You are given an integer representing the totaltotalTrips number of journeys that all buses need to complete. Please return the minimum time it takes to complete at least trips . totalTrips

class Solution {
    
    
    public long minimumTime(int[] time, int totalTrips) {
    
    
        int t=totalTrips;
        Arrays.sort(time);
        
        long r=(long)time[0]*t;
        long l=0;
        while(l<r){
    
    
            long mid=(r-l)/2+l;
            if(judge(time,mid,t)){
    
    
                r=mid;
            }
            else{
    
    
                l=mid+1;
            }
        }
        return l;
    }
    
    public boolean judge(int[] time,long T,int t){
    
    
        int res=0;
        for(int i=0;i<time.length;i++){
    
    
            res+=(int)(T/time[i]);
            if(res>=t){
    
    
                return true;
            }
        }
        return false;
    }
}

2188.Minimum time to complete the race\textcolor{red}{2188.Minimum time to complete the race}2 1 8 8 . Minimum time to complete the race

You are given a two-dimensional integer array with subscripts starting from 0tires , where tires[i] = [fi, ri]it means that iif the tire of the tire is used continuously, the lap of the xtire will take fi * ri(x-1)seconds.

  • For example, if fi = 3and ri = 2, and always, the same tire of this type is used, it takes that tire seconds 1to complete lap N , seconds to complete lap N , seconds to complete lap N , and so on.323 * 2 = 633 * 22 = 12

You are given both an integer changeTimeand an integer numLaps.

The race consists of numLapslaps in total, and you can choose any tire to start the race. There are countless tires of each kind . After each lap, you can choose to change to any type of tire (or a new tire of the current type) in changeTimeseconds .

Please return the minimum time required to complete the game .

class Solution {
    
    
    public int minimumFinishTime(int[][] tires, int changeTime, int numLaps) {
    
    
        int[] min=new int[numLaps+1];
        
        for(int i=1;i<=numLaps;i++){
    
    
            min[i]=Integer.MAX_VALUE;
            for(int k=0;k<tires.length;k++){
    
    
                if(tires[k][0]!=-1){
    
    
                    long f=(long)tires[k][0];
                    long r=(long)tires[k][1];
                    long sum=f*(1-(long)Math.pow(r,i))/(1-r);
                    if(sum<min[i]){
    
    
                        min[i]=(int)sum;
                    }
                    else if(sum>=Integer.MAX_VALUE){
    
    
                        tires[k][0]=-1;
                    }
                }
            }
        }
        
        for(int i=1;i<=numLaps;i++){
    
    
            for(int k=1;k<=i-k;k++){
    
    
                min[i]=Math.min(min[k]+min[i-k]+changeTime,min[i]);
            }
        }
        // for(int i=1;i<=numLaps;i++){
    
    
        //     System.out.println(min[i]);
        // }
        
        return min[numLaps];
    }
}

end

Question source: LeetCode link: https://leetcode-cn.com/problems

⭐️Follow the author, take you to brush the questions, and learn the most commonly used algorithm skills from simple algorithm questions (one question per day during the winter vacation) ⭐️Follow the
author to brush the questions—from simple to advanced, let you become a ruthless machine for brushing questions without knowing it , if you have any questions, please private message

Guess you like

Origin blog.csdn.net/caqjeryy/article/details/123195216