LeetCode weekly contest 259
This weekend is Chinese mid-autumn festival, which is a tradition day to got together with family. I am a country boy. My family live in another province, Shanxi. It is about 1,000 km far from Beijing, where I studied and work. Because of that, I cannot visit my parents. Fortunately, my girlfriend grow in Beijing. Her parents invite us to their home for lunch and dinner. They prepared much seafood because their daughter loves it. This is my excuse for not attending this contest.
I solved those problems after contest and share my intuitions and solutions now.
So no score board here.
2011. Final Value of Variable After Performing Operations
Sign-in question. A trick, which could reduce 4 operations to 2, is that the middle character (operations[i][1]) decides increment or decrement. With this, we could simplify our code.
1 | class Solution { |
Time complexity: O(N), where N = operations.size()
Space complexity: O(1).
2012. Sum of Beauty in the Array
For score is 2, compare nums[i]
with the largest number before i
and the smallest number after i
. We maintain a variable to record and update the largest number
and a pre-calculate array to record the smallest number
.
For score is 1, just compare nums[i]
with nums[i-1]
and nums[i+1]
.
1 | class Solution { |
Time complexity: O(n),
Space complexity: O(n).
2013. Detect Squares
Attention: Square instead of rectangle.
I wasted a lot of time because of this misunderstanding. Luckily, it is not on real contest.
Use unordered_map<int, map<int, int>>
to store all points.
When count squares, we iterate every row and find the width, then with width find the other two points.
1 | class DetectSquares { |
Time complexity:
- DetectSquares: O(1),
- add: O(log n),
- count: O(n log n).
Space complexity: O(n).
Where n = points number.