Skip to content

Commit 22c0111

Browse files
committed
5 problems solved.
1 parent b4d2fdc commit 22c0111

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

src/1252.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int oddCells(int m, int n, vector<vector<int>>& indices) {
2+
vector<vector<int>> vec(m, vector<int>(n, 0));
3+
int len = indices.size();
4+
for (int i = 0; i < len; ++i)
5+
{
6+
int row = indices[i][0], col = indices[i][1];
7+
for (int r = 0; r < m; ++r)
8+
vec[r][col]++;
9+
for (int c = 0; c < n; ++c)
10+
vec[row][c]++;
11+
}
12+
13+
int ct = 0;
14+
for (int i = 0; i < m; ++i)
15+
{
16+
for (int j = 0; j < n; ++j)
17+
{
18+
if (vec[i][j]%2 == 1)
19+
++ct;
20+
}
21+
}
22+
23+
return ct;
24+
}

src/1725.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int countGoodRectangles(vector<vector<int>>& rectangles) {
2+
int len = rectangles.size();
3+
int n = 0;
4+
int maxlen = 0;
5+
for (int i = 0; i < len; ++i)
6+
{
7+
int k = rectangles[i][0] < rectangles[i][1] ? rectangles[i][0] : rectangles[i][1];
8+
if (k > maxlen)
9+
{
10+
maxlen = k;
11+
n = 1;
12+
}
13+
else if (k == maxlen)
14+
++n;
15+
}
16+
17+
return n;
18+
}

src/2138.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
vector<string> divideString(string s, int k, char fill) {
2+
int len = s.size();
3+
int n = (len+k-1)/k;
4+
int total = n*k;
5+
vector<string> res(n, string(k, ' '));
6+
int i = 0, j = 0, m = -1;
7+
while (i < len)
8+
{
9+
if (i%k == 0)
10+
{
11+
++m;
12+
j = 0;
13+
res[m][j] = s[i];
14+
}
15+
else
16+
{
17+
res[m][j] = s[i];
18+
}
19+
++i;
20+
++j;
21+
}
22+
23+
while (i < total)
24+
{
25+
res[m][j++] = fill;
26+
++i;
27+
}
28+
29+
return res;
30+
}

src/495.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
2+
int len = timeSeries.size();
3+
int time = 0;
4+
int bg = timeSeries[0], ed = timeSeries[0]+duration-1;
5+
int i = 1;
6+
while (i < len)
7+
{
8+
if (timeSeries[i] > ed)
9+
{
10+
time += ed-bg+1;
11+
bg = timeSeries[i];
12+
ed = timeSeries[i]+duration-1;
13+
}
14+
else
15+
{
16+
ed = timeSeries[i]+duration-1;
17+
}
18+
++i;
19+
}
20+
21+
time += ed-bg+1;
22+
23+
return time;
24+
}

src/74.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
2+
int m = matrix.size();
3+
int n = matrix[0].size();
4+
5+
int p = 0, q = m*n-1;
6+
while (p <= q)
7+
{
8+
int mid = (p+q)/2;
9+
int r = mid/n, c = mid%n;
10+
if (matrix[r][c] == target)
11+
return true;
12+
else if (matrix[r][c] < target)
13+
p = mid+1;
14+
else
15+
q = mid-1;
16+
}
17+
18+
return false;
19+
}

0 commit comments

Comments
 (0)