First BB
I haven't done algorithm problems for some time. I haven't participated in the LeetCode weekly competition since preparing for the spring move. I was able to do two or three problems correctly two or three years ago. These years have passed, this level has even regressed a lot.
It seems that you can still remember that when you first came into contact with LeetCode, Zhou Sai still wrote secretly in Java class with iPad. You can only do the first check-in question correctly, and the second question may not be written.
At that time, there were far fewer participants than now
I feel that the level of my classmates and younger brothers and sisters is AC 3 4 now.
Attach the working environment of the company lounge
Careful students will find that this position is too sunny, so I changed to the other side
Again
https://leetcode-cn.com/contest/weekly-contest-252/
1. Triple divisor
A divisor must have 1 and itself. The third, see if there is anything else from 2 to sqrt(x)
class Solution { public: bool isThree(int n) { for(int i=2;i<=sqrt(n);i++){ if(i==sqrt(n))return true; if(n%i==0)return false; }return false; } };
There's no limit. In fact, hard writing is OK
class Solution { public: bool isThree(int n) { int r = 0; for (int i = 1; i <= n; i += 1) if (n % i == 0) r += 1; return r == 3; } };
2. The maximum number of weeks you can work
It's required that the work can't be continuous, so just mix it up
1010101 the most common type of this method, for example, is 1. 101 at most, just one more
So get the sum of all the numbers and the maximum number
class Solution { public: long long numberOfWeeks(vector<int>& milestones) { long long int getmax = -1; long long int sum=0; for(int i=0;i<milestones.size();i++){ if(milestones[i]>getmax)getmax=milestones[i]; sum+=milestones[i]; } if((sum-getmax)<getmax)return 2*(sum-getmax)+1; return sum; } };
3. Sufficient minimum garden length
Drawing and finding rules
I've been looking for it for a long time. It's divided into four * 4
class Solution { public: long long minimumPerimeter(long long neededApples) { long long ans = 0; while(++ans){ long long int hang = (1+ans)*(ans)/2; long long int pre= (ans+1)*hang + ans*(1+ans)*ans/2; if(pre*4 >= neededApples)return ans*8; } } };
Steal a copy of someone else's answer on the ranking list
using LL = long long; class Solution { public: long long minimumPerimeter(long long neededApples) { LL L = 1, R = 1000000; while (L < R) { LL M = (L + R) >> 1; LL x = (1 + M) * M * (2 * M + 1) * 2; if (x < neededApples) L = M + 1; else R = M; } return L * 8; } };
4. Count the number of special subsequences
I didn't sit down and say the wrong idea
That is, the whole three arrays are scanned for one round and then counted
The number of 0 in front of this position, the number of 1 in front of this position and the number of 2 in the back of this position
Divide into two groups, one 1 and multiple 1
Group one. One 1
After scanning to 1, see how many 0 in front and how many 2 behind, and then combine
The second set of two 1s is the boundary of 1
Look at the 0's in front of the first 1 and the 2's behind the second 1
Combine the 1 between two 1s and the 2 after the previous 0
Timeout + memory overflow, let's see the normal solution of others 🤪
using LL = long long; constexpr LL mod = 1'000'000'007; class Solution { public: int countSpecialSubsequences(vector<int>& nums) { LL A = 0, B = 0, C = 0; for (int x : nums) { if (x == 0) A = (A * 2 + 1) % mod;//The number of combinations of A has or does not have 2 kinds, with or without the front + the present if (x == 1) B = (B * 2 + A) % mod;//Number of combinations of B with and without B and A followed by B if (x == 2) C = (C * 2 + B) % mod;//Number of combinations of C }return C; } };