# LeetCode Image Smoother

Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

Example 1:

Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0


Note:

1. The value in the given matrix is in the range of [0, 255].
2. The length and width of the given matrix are in the range of [1, 150].

class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int x = M.size(), y = M.size();
vector<vector<int>> A(x, vector<int>(y, 0));
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
int s = 0, n = 0;
for (int u = i - 1 >= 0 ? i - 1 : 0; u < x && u <= i + 1; ++u) {
for (int v = j - 1 >= 0 ? j - 1 : 0; v < y && v <= j + 1; ++v) {
s += M[u][v];
++n;
}
}
A[i][j] = s / n;
}
}
return A;
}
};

# LeetCode Closest Divisors

1362. Closest Divisors

Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.

Return the two integers in any order.

Example 1:

Input: num = 8
Output: [3,3]
Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.


Example 2:

Input: num = 123
Output: [5,25]


Example 3:

Input: num = 999
Output: [40,25]


Constraints:

• 1 <= num <= 10^9

class Solution {
public:
int mySqrt(int x)
{
long long y = x;
while (y*y > x)
{
y = (y + x / y) / 2;
}
return y;
}

vector<int> work(int num) {
for (int i = mySqrt(num) + 1; i >= 1; --i) {
if (num%i == 0) {
return { i,num / i };
}
}
return { 1,num };
}
vector<int> closestDivisors(int num) {
vector<int> plus1 = work(num + 1);
vector<int> plus2 = work(num + 2);
if (abs(plus1 - plus1) < abs(plus2 - plus2))return plus1;
else return plus2;
}
};


# LeetCode Number of Days Between Two Dates

1360. Number of Days Between Two Dates

Write a program to count the number of days between two dates.

The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.

Example 1:

Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1


Example 2:

Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15


Constraints:

• The given dates are valid dates between the years 1971 and 2100.

struct Date {
int year;
int month;
int day;
Date() {
year = month = day = 0;
}
void parse(string date) {
year = stoi(date.substr(0, 4));
month = stoi(date.substr(5, 2));
day = stoi(date.substr(8, 2));
}
};
class Solution {
public:
bool isRunYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
int DaysInMonth(int m, int y) {
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if (isRunYear(y))return 29;
else return 28;
default:
return 30;
}
}

int DaysInYear(int year) {
if (isRunYear(year))return 366;
else return 365;
}

int daysBetweenDates(string date1, string date2) {
Date d1, d2;
if (date1 < date2) {
d1.parse(date1);
d2.parse(date2);
}
else {
d1.parse(date2);
d2.parse(date1);
}

if (d1.year == d2.year && d1.month==d2.month) {
return d2.day - d1.day;
}
else if (d1.year == d2.year) {
int part1 = DaysInMonth(d1.month, d1.year) - d1.day;
int part3 = d2.day;
int part2 = 0;
for (int m = d1.month + 1; m < d2.month; ++m) {
part2 += DaysInMonth(m, d1.year);
}
return part1 + part2 + part3;
}
else {
int part1 = DaysInMonth(d1.month, d1.year) - d1.day;
for (int m = d1.month + 1; m <= 12; ++m) {
part1 += DaysInMonth(m, d1.year);
}
int part2 = 0;
for (int y = d1.year + 1; y < d2.year; ++y) {
part2 += DaysInYear(y);
}
int part3 = d2.day;
for (int m = 1; m < d2.month; ++m) {
part3 += DaysInMonth(m, d2.year);
}
return part1 + part2 + part3;
}
}
};