力扣 第492场周赛(A~D)
A:101010. 容量最小的箱子

简单题,遍历即可。

 1 class Solution {
 2 public:
 3     int minimumIndex(vector<int>& ca, int it) {
 4         int min_c=INT_MAX;
 5         int id=-1;
 6         for(int i=0;i<ca.size();i++){
 7             if(ca[i]>=it && ca[i]<min_c){
 8                 min_c=ca[i];
 9                 id=i;
10             }
11         }
12         return id;
13     }
14 };

B:3862. 找出最小平衡下标

前后缀。用C++需要注意会超longlong范围,因为和是不可能超过1e14的,所以当积超过1e14时,可以直接赋值。

 1 typedef unsigned long long LL;
 2 class Solution {
 3 public:
 4     int smallestBalancedIndex(vector<int>& nums) {
 5         LL n=nums.size();
 6         vector<LL> pre(n+2,0),suf(n+2,0);
 7         suf[n+1]=1;
 8         for(LL i=1;i<=n;i++)
 9             pre[i]=pre[i-1]+nums[i-1];
10         const LL inf=1e14+1000;
11         for(LL i=n;i>=1;i--){
12             if(suf[i+1] > inf/nums[i-1])
13                 suf[i]=inf;
14             else
15                 suf[i]=suf[i+1]*nums[i-1];
16         }
17         for(LL i=1;i<=n;i++){
18             if(pre[i-1]==suf[i+1])
19                 return i-1;
20         }
21         return -1;
22     }
23 };

 C:3863. 将一个字符串排序的最小操作次数

分类讨论题。

 1 /*
 2 从上往下为依次判断
 3 1、如果当前序列已经有序,操作次数为0
 4 2、如果长度为2,那么不可能满足条件,返回-1
 5 3、如果最小值在s[0]或最大值在s.back(),操作次数为1
 6 4、如果中间部分有最大值或者最大值,操作次数为2
 7 5、如果只有一个最大值一个最小值,且最大值在s[0],最小值在s.back(),操作次数为3
 8 */
 9 class Solution {
10 public:
11     int minOperations(string s) {
12         auto inorder=[](string& s){
13             for(int i=1;i<s.size();i++){
14                 if(s[i]<s[i-1])
15                     return false;
16             }
17             return true;
18         };
19         char mx=*max_element(s.begin(),s.end());
20         char mn=*min_element(s.begin(),s.end());
21         if(inorder(s))
22             return 0;
23         if(s.size()==2)
24             return -1;
25         if(s[0]==mn || s.back()==mx)
26             return 1;
27         bool flag=false;
28         for(int i=1;i<s.size()-1;i++){
29             if(s[i]==mx || s[i]==mn){
30                 flag=true;
31                 break;
32             }
33         }
34         if(flag)
35             return 2;
36         return 3;
37     }
38 };

D:3864. 划分二进制字符串的最小费用

语文题,按照题意模拟一遍就可以了,复杂度为O(n)。

 1 typedef long long LL;
 2 class Solution {
 3 public:
 4     vector<int> pre;
 5     LL fun(int l,int r,string& s,int& e,int& f){
 6         LL L=r-l+1;
 7         LL X=pre[r+1]-pre[l];
 8         LL c;
 9         if(X==0)
10             c=f;
11         else
12             c=L*X*e;
13         if((r-l+1)%2==0){
14             int mid=l+r>>1;
15             return min(c,fun(l,mid,s,e,f)+fun(mid+1,r,s,e,f));
16         }else{
17             return c;
18         }
19     }
20     long long minCost(string s, int e, int f) {
21         int n=s.size();
22         pre.resize(n+1);
23         for(int i=1;i<=n;i++){
24             pre[i]=pre[i-1]+s[i-1]-'0';
25         }
26         return fun(0,s.size()-1,s,e,f);
27     }
28 };

 

posted on 2026-03-08 20:56  greenofyu  阅读(16)  评论(0)    收藏  举报