1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int reachNumber(int target) {
target = abs(target);
int k = 0, sum = 0;
while (sum < target || (sum - target) & 1)
sum += ++k;
return k;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int reachNumber(int target) {
target = abs(target);
long n = ceil((-1.0 + sqrt(1 + 8.0 * target)) / 2);
long sum = n * (n + 1) / 2;
if (sum == target) return n;
long diff = sum - target;
if ((diff & 1) == 0) return n;
return n + ((n & 1) ? 2 : 1);
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
unordered_map<int, int> frequency;
for (const string& word: words) {
int mask = 0;
for (char ch: word)
mask |= (1 << (ch - 'a'));
if (__builtin_popcount(mask) <= 7)
++frequency[mask];
}
vector<int> ans;
for (const string& puzzle: puzzles) {
int cnt = 0;
int mask = 0;
for (int i = 1; i < 7; ++i)
mask |= (1 << (puzzle[i] - 'a'));
int subset = mask;
do {
int s = subset | (1 << (puzzle[0] - 'a'));
if (frequency.find(s) != frequency.end())
cnt += frequency[s];
subset = (subset - 1) & mask;
} while (subset != mask);
ans.emplace_back(cnt);
}
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
int R = matrix.size(), C = matrix[0].size();
vector<vector<int>> ans(C, vector<int>(R));
for (int r = 0; r < R; ++r)
for (int c = 0; c < C; ++c)
ans[c][r] = matrix[r][c];
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
int n = A.size();
for (int i = 0; i < n; ++i) {
int left = 0, right = n - 1;
while (left < right) {
if (A[i][left] == A[i][right]) {
A[i][left] ^= 1;
A[i][right] ^= 1;
}
++left;
--right;
}
if (left == right)
A[i][left] ^= 1;
}
return A;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int X) {
int n = customers.size(), total = 0, increase = 0, maxIncrease;
for (int i = 0; i < n; ++i)
if (grumpy[i] == 0)
total += customers[i];
for (int i = 0; i < X; ++i)
increase += customers[i] * grumpy[i];
maxIncrease = increase;
for (int i = X; i < n; ++i) {
increase = increase - customers[i - X] * grumpy[i - X] + customers[i] * grumpy[i];
maxIncrease = max(maxIncrease, increase);
}
return total + maxIncrease;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
if (m == 1 || n == 1) return true;
int start_x = m - 2, start_y = 0;
while (start_y < n - 1) {
for (int next_x = start_x + 1, next_y = start_y + 1; next_x < m && next_y < n; ++next_x, ++next_y)
if (matrix[start_x][start_y] != matrix[next_x][next_y])
return false;
if (--start_x < 0) {
start_x = 0;
++start_y;
}
}
return true;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class MonotonicQueue {
private:
deque<int> queMin;
deque<int> queMax;
public:
int min() {
return queMin.front();
}
int max() {
return queMax.front();
}
void push(int x) {
while (!queMin.empty() && x < queMin.back())
queMin.pop_back();
queMin.push_back(x);
while (!queMax.empty() && x > queMax.back())
queMax.pop_back();
queMax.push_back(x);
}
void popMin() {
queMin.pop_front();
}
void popMax() {
queMax.pop_front();
}
};
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
MonotonicQueue q;
int n = nums.size(), left = 0, right = 0, ans = 1;
while (right < n) {
q.push(nums[right]);
while (left < n && q.max() - q.min() > limit) {
if (q.min() == nums[left])
q.popMin();
if (q.max() == nums[left])
q.popMax();
++left;
}
ans = max(ans, ++right - left);
}
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
unordered_map<int, int[3]> m; // num => [freq, left, right]
int n = nums.size(), degree = 1, ans = INT_MAX;
for (int i = 0; i < nums.size(); ++i) {
int num = nums[i];
if (m.find(num) == m.end()) {
m[num][0] = 1;
m[num][1] = i;
m[num][2] = i;
} else {
int freq = ++m[num][0];
m[num][2] = i;
degree = max(degree, freq);
}
}
for (auto [_, info] : m)
if (degree == info[0])
ans = min(ans, info[2] - info[1] + 1);
return ans;
}
};

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int kthSmallest(int[][] matrix, int k) {
final int R = matrix.length, C = matrix[0].length;
int[] sorted = new int[R * C];
int idx = 0;
for (int[] row : matrix)
for (int num : row)
sorted[idx++] = num;
Arrays.sort(sorted);
return sorted[k - 1];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int kthSmallest(int[][] matrix, int k) {
final int R = matrix.length;
PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[0] - b[0]);
for (int i = 0; i < R; ++i)
q.offer(new int[]{matrix[i][0], i, 0});
for (int i = 0; i < k - 1; ++i) {
int[] now = q.poll();
if (now[2] != R - 1)
q.offer(new int[]{matrix[now[1]][now[2] + 1], now[1], now[2] + 1});
}
return q.poll()[0];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int n = matrix.length;
int left = matrix[0][0], right = matrix[n - 1][n - 1];
while (left < right) {
int mid = left + ((right - left) >> 1);
if (check(matrix, k, mid, n))
right = mid;
else
left = mid + 1;
}
return left;
}
private boolean check(int[][] matrix, int k, int mid, int n) {
int i = n - 1, j = 0, cnt = 0;
while (i >= 0 && j < n) {
if (matrix[i][j] <= mid) {
cnt += i + 1;
++j;
} else {
--i;
}
}
return cnt >= k;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int minKBitFlips(vector<int>& A, int K) {
int n = A.size(), flipped = 0, ans = 0;
vector<int> isFlipped(n);
for (int i = 0; i < n; ++i) {
if (i >= K)
flipped ^= isFlipped[i - K];
if (flipped == A[i]) {
if (i + K > n)
return -1;
isFlipped[i] = 1;
flipped ^= 1;
++ans;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int minKBitFlips(vector<int>& A, int K) {
int n = A.size(), flipped = 0, ans = 0;
queue<int> isFlipped;
for (int i = 0; i < n; ++i) {
if (i >= K) {
flipped ^= isFlipped.front();
isFlipped.pop();
}
if (flipped == A[i]) {
if (i + K > n)
return -1;
isFlipped.push(1);
flipped ^= 1;
++ans;
} else {
isFlipped.push(0);
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minKBitFlips(vector<int>& A, int K) {
// "cur" is the count of flipping times within current sliding window.
int n = A.size(), cur = 0, ans = 0;
for (int i = 0; i < n; ++i) {
if (i >= K && A[i - K] > 1) {
--cur;
A[i - K] += 2;
}
if (cur % 2 == A[i]) {
if (i + K > n)
return -1;
A[i] += 2;
++cur;
++ans;
}
}
return ans;
}
};

Reference: [Java/C++/Python] One Pass and O(1) Space - LeetCode Discuss.

0%