Re: LeetCode Weekly Contest 427

作者: oin1104 (是oin的說)   2024-12-08 12:16:21
我第三題有地方沒改到
結束之後馬上就找到錯
我他媽的

4500名
我要扣分了
我破防了
我破防了
我破防了
我破防

幹你娘
第一題
暴力
```cpp
class Solution {
public:
vector<int> constructTransformedArray(vector<int>& nums)
{
int n = nums.size();
vector<int> res(n,0);
for(int i = 0 ; i < n ; i ++)
{
res[i] = (nums[((i+nums[i])%n+n)%n] );
}
return res;
}
};
```
第二題
只要知道怎麼判斷矩形就好
我這題寫超級久
真的幹你娘
```cpp
class Solution {
public:
bool okrect(vector<vector<int>>& points , vector<int> test)
{
int x1 =INT_MAX;
int y1 =INT_MIN;
int x2 =INT_MIN;
int y2 =INT_MAX;
for(int j = 0 ; j < 4 ; j ++)
{
x1 = min(x1,points[j][0]);
y1 = max(y1,points[j][1]);
x2 = max(x2,points[j][0]);
y2 = min(y2,points[j][1]);
}
if(test[0] >= x1 && test[0] <= x2 && test[1] <= y1 && test[1] >= y2 )
{
return 0;
}
return 1;
}
int rect(vector<vector<int>>& points)
{
int x1 =INT_MAX;
int y1 =INT_MIN;
int x2 =INT_MIN;
int y2 =INT_MAX;
for(int j = 0 ; j < 4 ; j ++)
{
x1 = min(x1,points[j][0]);
y1 = max(y1,points[j][1]);
x2 = max(x2,points[j][0]);
y2 = min(y2,points[j][1]);
}
for(int k = 0 ; k < 4 ; k ++)
{
if(points[k][0] > x1 && points[k][0] < x2 && points[k][1] < y1 && po
ints[k][1] > y2 )
{
return -1;
}
if(points[k][0] != x1 && points[k][0] != x2)return -1;
if(points[k][1] != y1 && points[k][1] != y2)return -1;
}
return (x2-x1)*(y1-y2);
}
int maxRectangleArea(vector<vector<int>>& points)
{
int n = points.size();
int res = -1;
for(int i = 0 ; i < n ; i ++)
{
for(int j = i+1 ; j < n ; j ++)
{
for(int k = j+1 ; k < n ; k ++)
{
for(int p = k+1 ; p < n ; p ++)
{
vector<vector<int>> test;
test.push_back(points[i]);
test.push_back(points[j]);
test.push_back(points[k]);
test.push_back(points[p]);
int ok = 1;
for(int o = 0 ; o < n ; o ++)
{
if(o == i || o == j || o == k || o == p )continue;
if(!okrect(test,points[o]))ok = 0;
}
if(ok)
{
// for(int kk = 0 ; kk < 4 ; kk ++)
// {
// cout << test[kk][0] << " " << test[kk][1] <<
endl;
// }
// cout << rect(test) << " ======= " << endl;
res = max(res,rect(test));
}
}
}
}
}
if(res == 0)return -1;
return res;
}
};
```
第三題
我破防了
我差10秒寫出來
用prefixsum + hash記i%k的最小sum
```cpp
class Solution {
public:
long long maxSubarraySum(vector<int>& nums, int k)
{
long long res = LLONG_MIN;
int n = nums.size();
int l = 0 ;
int r = 0 ;
long long now = 0 ;
unordered_map<int,long long> save;
vector<long long> paper(n+1,0);
for(int i = 0 ; i < n ; i ++)
{
paper[i+1] = nums[i] + paper[i];
}
for(int i = 0 ; i <= n ; i ++)
{
// for(auto k2 : save)
// {
// cout << k2.first << " " << k2.second << endl;
// }
// cout << "========== \n";
if(save.find(i%k) != save.end())
{
// cout << paper[i] <<" - " << save[i%k] << endl ;
res = max(paper[i] - save[i%k] , res);
}
if(save.find(i%k) == save.end())
{
save[i%k] = paper[i];
}
else
{
save[i%k] = min(save[i%k] , paper[i]);
}
// cout << "========== \n";
}
if( res == LLONG_MIN)return -1;
return res;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com