Skip to content

Commit 55da3ef

Browse files
authored
update: 添加问题“2711.对角线上不同值的数量差”的代码和题解(#838)
* leetcode: 2711 - Half - 跑步去了 max(mn)·mn版本 + Half的前缀和mn版本 Signed-off-by: LetMeFly666 <[email protected]> * article: HTML tag of 《借助mitmproxy通过电子邮件隐式传输信息》 Signed-off-by: LetMeFly666 <[email protected]> * 2711: AC.cpp - AC,75.00%,75.00% Signed-off-by: LetMeFly666 <[email protected]> * 2711: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2711: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2711: AC.cpp - AC,100.00%,100.00% Signed-off-by: LetMeFly666 <[email protected]> * 2711: AC.py Signed-off-by: LetMeFly666 <[email protected]> * 2711: WA.Java Signed-off-by: LetMeFly666 <[email protected]> * 2711: WA.Java Signed-off-by: LetMeFly666 <[email protected]> * 2711: AC.Java - AC,82.76%,68.97% Signed-off-by: LetMeFly666 <[email protected]> * 2711: 编译错误.Golang Line 32: Char 33: undefined: abs (solution.go) Signed-off-by: LetMeFly666 <[email protected]> * 2711: AC.Golang - AC,100.00%,100.00% Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2711.对角线上不同值的数量差”的代码和题解(#838) leetcode: 2711(双100%版本-C++/Go) article: HTML tag of 《借助mitmproxy通过电子邮件隐式传输信息》 Signed-off-by: LetMeFly666 <[email protected]> * index_img: 2711 Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 81dfcff commit 55da3ef

13 files changed

+584
-6
lines changed

.commitmsg

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
refactor: 周报->上周
2-
3-
index_img: 借助mitmproxy通过电子邮件隐式传输信息
1+
leetcode: 2711(双100%版本-C++/Go)
42

3+
article: HTML tag of 《借助mitmproxy通过电子邮件隐式传输信息》

.nodeploy

Whitespace-only changes.

.todo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
设置VsCode括号不自动补全

Codes/2711-difference-of-number-of-distinct-values-on-diagonals.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2025-03-25 16:52:02
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-03-25 16:53:14
5+
* @LastEditTime: 2025-03-25 17:42:27
66
*/
77
#ifdef _WIN32
88
#include "_[1,2]toVector.h"
@@ -14,8 +14,16 @@ class Solution {
1414
vector<vector<int>> ans(grid.size(), vector<int>(grid[0].size()));
1515
for (int i = 0; i < grid.size(); i++) {
1616
for (int j = 0; j < grid[i].size(); j++) {
17-
17+
unordered_set<int> l, r;
18+
for (int k = 1; i - k >= 0 && j - k >= 0; k++) {
19+
l.insert(grid[i - k][j - k]);
20+
}
21+
for (int k = 1; i + k < grid.size() && j + k < grid[0].size(); k++) {
22+
r.insert(grid[i + k][j + k]);
23+
}
24+
ans[i][j] = abs(int(l.size() - r.size()));
1825
}
1926
}
27+
return ans;
2028
}
2129
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-25 19:44:55
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-25 21:04:00
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<vector<int>> differenceOfDistinctValues(vector<vector<int>>& grid) {
14+
int n = grid.size(), m = grid[0].size();
15+
vector<vector<int>> ans(n, vector<int>(m));
16+
for (int k = 0; k < m + n - 1; k++) {
17+
int i = k < m ? 0 : k - m + 1, j = k < m ? k : 0;
18+
unordered_set<int> se;
19+
int d = 0;
20+
for (; i + d < n && j + d < m; d++) {
21+
ans[i + d][j + d] = se.size();
22+
se.insert(grid[i + d][j + d]);
23+
}
24+
se.clear();
25+
for (d--; d >= 0; d--) {
26+
ans[i + d][j + d] = abs(int(ans[i + d][j + d] - se.size()));
27+
se.insert(grid[i + d][j + d]);
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-25 21:06:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-25 21:17:48
6+
* @Description: AC,100.00%,100.00%
7+
*/
8+
#ifdef _WIN32
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
12+
typedef long long ll;
13+
14+
15+
class Solution {
16+
public:
17+
vector<vector<int>> differenceOfDistinctValues(vector<vector<int>>& grid) {
18+
int n = grid.size(), m = grid[0].size();
19+
vector<vector<int>> ans(n, vector<int>(m));
20+
for (int k = 0; k < m + n - 1; k++) {
21+
int i = k < m ? 0 : k - m + 1, j = k < m ? k : 0;
22+
ll se = 0;
23+
int d = 0;
24+
for (; i + d < n && j + d < m; d++) {
25+
ans[i + d][j + d] = __builtin_popcountll(se);
26+
se |= 1LL << grid[i + d][j + d];
27+
}
28+
se = 0;
29+
for (d--; d >= 0; d--) {
30+
ans[i + d][j + d] = abs(ans[i + d][j + d] - __builtin_popcountll(se));
31+
se |= 1LL << grid[i + d][j + d];
32+
}
33+
}
34+
return ans;
35+
}
36+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-25 21:29:42
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-25 21:39:51
6+
@Description: AC,100.00%,100.00%
7+
*/
8+
package main
9+
10+
import "math/bits"
11+
12+
func abs2711(a int) int {
13+
if a < 0 {
14+
return -a
15+
}
16+
return a
17+
}
18+
19+
func differenceOfDistinctValues(grid [][]int) [][]int {
20+
n, m := len(grid), len(grid[0])
21+
ans := make([][]int, n)
22+
for i := range ans {
23+
ans[i] = make([]int, m)
24+
}
25+
for k := 0; k < m + n - 1; k++ {
26+
var i, j int
27+
if k < m {
28+
i, j = 0, k
29+
} else {
30+
i, j = k - m + 1, 0
31+
}
32+
se := uint64(0)
33+
d := 0
34+
for ; i + d < n && j + d < m; d++ {
35+
ans[i + d][j + d] = bits.OnesCount64(se)
36+
se |= uint64(1) << grid[i + d][j + d]
37+
}
38+
se = 0
39+
for d--; d >= 0; d-- {
40+
ans[i + d][j + d] = abs2711(ans[i + d][j + d] - bits.OnesCount64(se))
41+
se |= uint64(1) << grid[i + d][j + d]
42+
}
43+
}
44+
return ans
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-25 21:23:51
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-25 21:28:30
6+
*/
7+
class Solution {
8+
public int[][] differenceOfDistinctValues(int[][] grid) {
9+
int n = grid.length, m = grid[0].length;
10+
int[][] ans = new int[n][m];
11+
for (int k = 0; k < m + n - 1; k++) {
12+
int i = k < m ? 0 : k - m + 1;
13+
int j = k < m ? k : 0;
14+
long se = 0;
15+
int d = 0;
16+
for (; i + d < n && j + d < m; d++) {
17+
ans[i + d][j + d] = Long.bitCount(se);
18+
se |= 1L << grid[i + d][j + d];
19+
}
20+
se = 0;
21+
for (d--; d >= 0; d--) {
22+
ans[i + d][j + d] = Math.abs(ans[i + d][j + d] - Long.bitCount(se));
23+
se |= 1L << grid[i + d][j + d];
24+
}
25+
}
26+
return ans;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-25 21:18:16
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-25 21:23:09
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def differenceOfDistinctValues(self, grid: List[List[int]]) -> List[List[int]]:
11+
n, m = len(grid), len(grid[0])
12+
ans = [[0] * m for _ in range(n)]
13+
for k in range(m + n - 1):
14+
i = 0 if k < m else k - m + 1
15+
j = k if k < m else 0
16+
se = d = 0
17+
while i + d < n and j + d < m:
18+
ans[i + d][j + d] = se.bit_count() # python10才有
19+
se |= 1 << grid[i + d][j + d]
20+
d += 1
21+
se = 0
22+
d -= 1
23+
while d >= 0:
24+
ans[i + d][j + d] = abs(ans[i + d][j + d] - se.bit_count())
25+
se |= 1 << grid[i + d][j + d]
26+
d -= 1
27+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,7 @@
810810
|2706.购买两块巧克力|简单|<a href="https://leetcode.cn/problems/buy-two-chocolates/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/29/LeetCode%202706.%E8%B4%AD%E4%B9%B0%E4%B8%A4%E5%9D%97%E5%B7%A7%E5%85%8B%E5%8A%9B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135295305" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/buy-two-chocolates/solutions/2584324/letmefly-2706gou-mai-liang-kuai-qiao-ke-vw4ai/" target="_blank">LeetCode题解</a>|
811811
|2708.一个小组的最大实力值|中等|<a href="https://leetcode.cn/problems/maximum-strength-of-a-group/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/09/04/LeetCode%202708.%E4%B8%80%E4%B8%AA%E5%B0%8F%E7%BB%84%E7%9A%84%E6%9C%80%E5%A4%A7%E5%AE%9E%E5%8A%9B%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/141884725" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-strength-of-a-group/solutions/2903918/letmefly-2708yi-ge-xiao-zu-de-zui-da-shi-6vng/" target="_blank">LeetCode题解</a>|
812812
|2710.移除字符串中的尾随零|简单|<a href="https://leetcode.cn/problems/remove-trailing-zeros-from-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/29/LeetCode%202710.%E7%A7%BB%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E5%B0%BE%E9%9A%8F%E9%9B%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140067468" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-trailing-zeros-from-a-string/solutions/2825991/letmefly-2710yi-chu-zi-fu-chuan-zhong-de-r6ol/" target="_blank">LeetCode题解</a>|
813+
|2711.对角线上不同值的数量差|中等|<a href="https://leetcode.cn/problems/difference-of-number-of-distinct-values-on-diagonals/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/25/LeetCode%202711.%E5%AF%B9%E8%A7%92%E7%BA%BF%E4%B8%8A%E4%B8%8D%E5%90%8C%E5%80%BC%E7%9A%84%E6%95%B0%E9%87%8F%E5%B7%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146514119" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/difference-of-number-of-distinct-values-on-diagonals/solutions/3627916/letmefly-omnshi-jian-o1kong-jian-wei-yun-ey8o/" target="_blank">LeetCode题解</a>|
813814
|2717.半有序排列|简单|<a href="https://leetcode.cn/problems/semi-ordered-permutation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/11/LeetCode%202717.%E5%8D%8A%E6%9C%89%E5%BA%8F%E6%8E%92%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144412760" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/semi-ordered-permutation/solutions/3017796/letmefly-2717ban-you-xu-pai-lie-yi-ci-bi-8k4i/" target="_blank">LeetCode题解</a>|
814815
|2731.移动机器人|中等|<a href="https://leetcode.cn/problems/movement-of-robots/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/10/LeetCode%202731.%E7%A7%BB%E5%8A%A8%E6%9C%BA%E5%99%A8%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133758255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/2476843/letmefly-2731yi-dong-ji-qi-ren-nao-jin-j-vwf2/" target="_blank">LeetCode题解</a>|
815816
|2734.执行子串操作后的字典序最小字符串|中等|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-substring-operation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/27/LeetCode%202734.%E6%89%A7%E8%A1%8C%E5%AD%90%E4%B8%B2%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/140027022" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-substring-operation/solutions/2824438/letmefly-2734zhi-xing-zi-chuan-cao-zuo-h-qcr8/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)