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