Leetcode Biweekly Contest 145

楼主: oin1104 (是oin的说)   2024-12-08 00:55:47
干 我第三题差一点写出来
我就错在用string纪录的数字
4位数变成的3位数不能变回4位数
干你娘
我快哭了
差点进1000名
不过这次2500
大概是+0分

第一题
题目看超久 超难看懂
反正就暴力
```cpp
class Solution {
public:
int minOperations(vector<int>& nums, int k)
{
int n = nums.size();
for(int i : nums)if(i < k)return -1;
vector<int> paper(101,0);
for(int i = 0 ; i < n ; i ++)
{
paper[nums[i]] ++;
}
int res = 0;
for(int i = k+1 ; i <= 100 ; i ++)
{
if(paper[i] > 0)res ++;
}
return res;
}
};
```
第二题
用n!的方法检查每种组合
```cpp
class Solution {
public:
vector<int> lock ;
int n ;
int mintime;
int k ;
void find( vector<int> &save , int now , int time , int x )
{
if(now == n)
{
mintime = min(mintime , time);
}
for(int i = 0 ; i < n ; i ++)
{
if(save[i] == 0)
{
save[i] = 1;
int cost = lock[i]/x;
if(lock[i]%x > 0)cost ++;
find(save , now + 1 , time + cost , x+k );
save[i] = 0;
}
}
}
int findMinimumTime(vector<int>& strength, int K)
{
n = strength.size();
lock = strength;
mintime = INT_MAX;
k = K;
vector<int> saa(n,0);
find(saa , 0 , 0 , 1);
return mintime;
}
};
```
第三题
用bfs+pq
其实可以把他当成在数字做成的n维空间中
用每次的cost来Dijkstra
然后记得不做质数就好
class Solution {
public:
bool isprime(int num)
{
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3 ; i * i <= num ; i += 2)
{
if (num % i == 0) return false;
}
return true;
}
int minOperations(int n, int m)
{
if(isprime(m))return -1;
int ten = to_string(n).size();
priority_queue<pair<int,string> , vector<pair<int,string>> , greater<pai
r<int,string>> > q;
q.push({n,to_string(n)});
int res = INT_MAX;
vector<int> paper(100001 , INT_MAX);
while(!q.empty())
{
string nowstr = q.top().second;
int nowint = stoi(nowstr);
int nowt = q.top().first;
// cout << nowstr << " " << nowt << " " << endl ;
q.pop();
if(nowint == m)return nowt;
if( isprime(nowint) ) continue;
int first = 0;
for(int i = 0 ; i < ten ; i ++)
{
if(nowstr[i] == '0' && first == 0)
{
continue;
}
first = 1;
if(nowstr[i] == '9')
{
string hi = nowstr;
hi[i]
作者: altecaux ( )   2024-12-08 00:57:00
不可以动不动就哭 尼是小件货 哭ㄌ会心疼
楼主: oin1104 (是oin的说)   2024-12-08 01:00:00
呜呜呜哇哇哇
作者: mrsonic (typeB)   2024-12-08 01:02:00
你是哑巴吗

Links booklink

Contact Us: admin [ a t ] ucptt.com