2185. Count Strings Containing a Given Prefix
Given an array of strings words
and a string pref
.
Returns the number of strings words
in 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 s
and t
. You can append any characters
to or in one operation .t
Returns the minimum number of steps* required to make s
and 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 i
first 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 i
if the tire of the tire is used continuously, the lap of the x
tire will take fi * ri(x-1)
seconds.
- For example, if
fi = 3
andri = 2
, and always, the same tire of this type is used, it takes that tire seconds1
to complete lap N , seconds to complete lap N , seconds to complete lap N , and so on.3
2
3 * 2 = 6
3
3 * 22 = 12
You are given both an integer changeTime
and an integer numLaps
.
The race consists of numLaps
laps 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 changeTime
seconds .
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