Skip to content

feat: update lc problems #3346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0600-0699/0614.Second Degree Follower/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tags:
| follower | varchar |
+-------------+---------+
(followee, follower) 是该表的主键(具有唯一值的列的组合)。
该表的每一行表示关注者关注了社交网络上的关注者
该表的每一行表示关注者关注了社交网络上的被关注者
不会有用户关注他们自己。
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0800-0899/0833.Find%20And%20Replace%20in%20String/README.md
tags:
- 数组
- 哈希表
- 字符串
- 排序
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/0800-0899/0833.Find%20And%20Replace%20in%20String/README_EN.md
tags:
- Array
- Hash Table
- String
- Sorting
---
Expand Down
6 changes: 3 additions & 3 deletions solution/1000-1099/1001.Grid Illumination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ tags:

假设一盏灯的坐标为 $(x, y)$,那么它所在的行的数值为 $x$,列的数值为 $y$,正对角线的数值为 $x-y$,反对角线的数值为 $x+y$。确定某一直线的唯一数值标识后,我们就可以通过哈希表来记录某一直线所拥有的灯的数目。

我们遍历数组 $lamps$,将当前遍历到的灯所在的行、列和正、反对角线拥有灯的数目分别加 $1$。
我们遍历数组 $\textit{lamps}$,将当前遍历到的灯所在的行、列和正、反对角线拥有灯的数目分别加 $1$。

注意,在处理 $lamps$ 时,需要进行去重,因为我们将重复的灯看作同一盏灯。
注意,在处理 $\textit{lamps}$ 时,需要进行去重,因为我们将重复的灯看作同一盏灯。

接下来,我们遍历 queries,判断当前查询点所在的行,列和正、反对角线是否有灯,如果有,则置 $1$,即该点在查询时是被照亮的。然后进行关闭操作,查找查询点所在的八近邻点及它本身是否有灯,如果有,将该点所在的行、列和正、反对角线的灯数目分别减 $1$,并且将灯从网格中去掉。

最后,返回答案数组即可。

时间复杂度 $O(m + q)$,其中 $m$ 和 $q$ 分别为数组 $lamps$ 和 $queries$ 的长度。
时间复杂度 $O(m + q)$,其中 $m$ 和 $q$ 分别为数组 $\textit{lamps}$ 和 $\textit{queries}$ 的长度。

<!-- tabs:start -->

Expand Down
14 changes: 13 additions & 1 deletion solution/1000-1099/1001.Grid Illumination/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ The 1<sup>st</sup>&nbsp;query asks if the lamp at grid[1][0] is illuminated or n

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

Suppose the coordinates of a lamp are $(x, y)$. Then, the row value is $x$, the column value is $y$, the main diagonal value is $x-y$, and the anti-diagonal value is $x+y$. Once we determine the unique value identifier for a line, we can use a hash table to record the number of lamps on that line.

We traverse the array $\textit{lamps}$, and for each lamp, we increment the count of lamps in its row, column, main diagonal, and anti-diagonal by $1$.

Note that when processing $\textit{lamps}$, we need to remove duplicates because we treat repeated lamps as the same lamp.

Next, we traverse the queries and check if there are lamps in the row, column, main diagonal, or anti-diagonal of the current query point. If there are, we set the value to $1$, indicating that the point is illuminated during the query. Then, we perform the turn-off operation by checking the eight neighboring points of the query point and the point itself to see if there are any lamps. If there are, we decrement the count of lamps in the corresponding row, column, main diagonal, and anti-diagonal by $1$ and remove the lamp from the grid.

Finally, we return the answer array.

The time complexity is $O(m + q)$, where $m$ and $q$ are the lengths of the arrays $\textit{lamps}$ and $\textit{queries}$, respectively.

<!-- tabs:start -->

Expand Down
85 changes: 38 additions & 47 deletions solution/1000-1099/1002.Find Common Characters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ tags:

我们用一个长度为 $26$ 的数组 $cnt$ 记录每个字符在所有字符串中出现的最小次数,最后遍历 $cnt$ 数组,将出现次数大于 $0$ 的字符加入答案即可。

时间复杂度 $O(n \sum w_i)$,空间复杂度 $O(C)$。其中 $n$ 为字符串数组 $words$ 的长度,而 $w_i$ 为字符串数组 $words$ 中第 $i$ 个字符串的长度,另外 $C$ 为字符集的大小,本题中 $C = 26$。
时间复杂度 $O(n \sum w_i)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 为字符串数组 $words$ 的长度,而 $w_i$ 为字符串数组 $words$ 中第 $i$ 个字符串的长度,另外 $|\Sigma|$ 为字符集的大小,本题中 $|\Sigma| = 26$。

<!-- tabs:start -->

Expand All @@ -69,13 +69,10 @@ class Solution:
def commonChars(self, words: List[str]) -> List[str]:
cnt = Counter(words[0])
for w in words:
ccnt = Counter(w)
for c in cnt.keys():
cnt[c] = min(cnt[c], ccnt[c])
ans = []
for c, v in cnt.items():
ans.extend([c] * v)
return ans
t = Counter(w)
for c in cnt:
cnt[c] = min(cnt[c], t[c])
return list(cnt.elements())
```

#### Java
Expand All @@ -84,21 +81,19 @@ class Solution:
class Solution {
public List<String> commonChars(String[] words) {
int[] cnt = new int[26];
Arrays.fill(cnt, 10000);
for (String w : words) {
int[] ccnt = new int[26];
Arrays.fill(cnt, 20000);
for (var w : words) {
int[] t = new int[26];
for (int i = 0; i < w.length(); ++i) {
++ccnt[w.charAt(i) - 'a'];
++t[w.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
cnt[i] = Math.min(cnt[i], ccnt[i]);
cnt[i] = Math.min(cnt[i], t[i]);
}
}
List<String> ans = new ArrayList<>();
for (int i = 0; i < 26; ++i) {
while (cnt[i]-- > 0) {
ans.add(String.valueOf((char) (i + 'a')));
}
ans.addAll(Collections.nCopies(cnt[i], String.valueOf((char) ('a' + i))));
}
return ans;
}
Expand All @@ -111,21 +106,20 @@ class Solution {
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
int cnt[26];
memset(cnt, 0x3f, sizeof(cnt));
for (auto& w : words) {
int ccnt[26]{};
for (char& c : w) {
++ccnt[c - 'a'];
vector<int> cnt(26, 20000);
for (const auto& w : words) {
vector<int> t(26, 0);
for (char c : w) {
++t[c - 'a'];
}
for (int i = 0; i < 26; ++i) {
cnt[i] = min(cnt[i], ccnt[i]);
cnt[i] = min(cnt[i], t[i]);
}
}
vector<string> ans;
for (int i = 0; i < 26; ++i) {
while (cnt[i]--) {
ans.emplace_back(1, i + 'a');
for (int j = 0; j < cnt[i]; ++j) {
ans.push_back(string(1, 'a' + i));
}
}
return ans;
Expand All @@ -137,49 +131,46 @@ public:

```go
func commonChars(words []string) (ans []string) {
cnt := [26]int{}
cnt := make([]int, 26)
for i := range cnt {
cnt[i] = 1 << 30
cnt[i] = 20000
}
for _, w := range words {
ccnt := [26]int{}
t := make([]int, 26)
for _, c := range w {
ccnt[c-'a']++
t[c-'a']++
}
for i, v := range cnt {
cnt[i] = min(v, ccnt[i])
for i := 0; i < 26; i++ {
cnt[i] = min(cnt[i], t[i])
}
}
for i, v := range cnt {
for v > 0 {
ans = append(ans, string(i+'a'))
v--
for i := 0; i < 26; i++ {
for j := 0; j < cnt[i]; j++ {
ans = append(ans, string('a'+rune(i)))
}
}
return
return ans
}
```

#### TypeScript

```ts
function commonChars(words: string[]): string[] {
const freq: number[] = Array(26).fill(Number.POSITIVE_INFINITY);
const cnt = Array(26).fill(20000);
const aCode = 'a'.charCodeAt(0);
for (const word of words) {
const t: number[] = Array(26).fill(0);
for (const c of word) {
++t[c.charCodeAt(0) - aCode];
for (const w of words) {
const t = Array(26).fill(0);
for (const c of w) {
t[c.charCodeAt(0) - aCode]++;
}
for (let i = 0; i < 26; ++i) {
freq[i] = Math.min(freq[i], t[i]);
for (let i = 0; i < 26; i++) {
cnt[i] = Math.min(cnt[i], t[i]);
}
}
const ans: string[] = [];
for (let i = 0; i < 26; ++i) {
if (freq[i]) {
ans.push(...String.fromCharCode(i + aCode).repeat(freq[i]));
}
for (let i = 0; i < 26; i++) {
cnt[i] && ans.push(...String.fromCharCode(i + aCode).repeat(cnt[i]));
}
return ans;
}
Expand Down
89 changes: 42 additions & 47 deletions solution/1000-1099/1002.Find Common Characters/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

We use an array $cnt$ of length $26$ to record the minimum number of times each character appears in all strings. Finally, we traverse the $cnt$ array and add characters with a count greater than $0$ to the answer.

The time complexity is $O(n \sum w_i)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string array $words$, $w_i$ is the length of the $i$-th string in the array $words$, and $|\Sigma|$ is the size of the character set, which is $26$ in this problem.

<!-- tabs:start -->

Expand All @@ -56,13 +60,10 @@ class Solution:
def commonChars(self, words: List[str]) -> List[str]:
cnt = Counter(words[0])
for w in words:
ccnt = Counter(w)
for c in cnt.keys():
cnt[c] = min(cnt[c], ccnt[c])
ans = []
for c, v in cnt.items():
ans.extend([c] * v)
return ans
t = Counter(w)
for c in cnt:
cnt[c] = min(cnt[c], t[c])
return list(cnt.elements())
```

#### Java
Expand All @@ -71,21 +72,19 @@ class Solution:
class Solution {
public List<String> commonChars(String[] words) {
int[] cnt = new int[26];
Arrays.fill(cnt, 10000);
for (String w : words) {
int[] ccnt = new int[26];
Arrays.fill(cnt, 20000);
for (var w : words) {
int[] t = new int[26];
for (int i = 0; i < w.length(); ++i) {
++ccnt[w.charAt(i) - 'a'];
++t[w.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
cnt[i] = Math.min(cnt[i], ccnt[i]);
cnt[i] = Math.min(cnt[i], t[i]);
}
}
List<String> ans = new ArrayList<>();
for (int i = 0; i < 26; ++i) {
while (cnt[i]-- > 0) {
ans.add(String.valueOf((char) (i + 'a')));
}
ans.addAll(Collections.nCopies(cnt[i], String.valueOf((char) ('a' + i))));
}
return ans;
}
Expand All @@ -98,21 +97,20 @@ class Solution {
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
int cnt[26];
memset(cnt, 0x3f, sizeof(cnt));
for (auto& w : words) {
int ccnt[26]{};
for (char& c : w) {
++ccnt[c - 'a'];
vector<int> cnt(26, 20000);
for (const auto& w : words) {
vector<int> t(26, 0);
for (char c : w) {
++t[c - 'a'];
}
for (int i = 0; i < 26; ++i) {
cnt[i] = min(cnt[i], ccnt[i]);
cnt[i] = min(cnt[i], t[i]);
}
}
vector<string> ans;
for (int i = 0; i < 26; ++i) {
while (cnt[i]--) {
ans.emplace_back(1, i + 'a');
for (int j = 0; j < cnt[i]; ++j) {
ans.push_back(string(1, 'a' + i));
}
}
return ans;
Expand All @@ -124,49 +122,46 @@ public:

```go
func commonChars(words []string) (ans []string) {
cnt := [26]int{}
cnt := make([]int, 26)
for i := range cnt {
cnt[i] = 1 << 30
cnt[i] = 20000
}
for _, w := range words {
ccnt := [26]int{}
t := make([]int, 26)
for _, c := range w {
ccnt[c-'a']++
t[c-'a']++
}
for i, v := range cnt {
cnt[i] = min(v, ccnt[i])
for i := 0; i < 26; i++ {
cnt[i] = min(cnt[i], t[i])
}
}
for i, v := range cnt {
for v > 0 {
ans = append(ans, string(i+'a'))
v--
for i := 0; i < 26; i++ {
for j := 0; j < cnt[i]; j++ {
ans = append(ans, string('a'+rune(i)))
}
}
return
return ans
}
```

#### TypeScript

```ts
function commonChars(words: string[]): string[] {
const freq: number[] = Array(26).fill(Number.POSITIVE_INFINITY);
const cnt = Array(26).fill(20000);
const aCode = 'a'.charCodeAt(0);
for (const word of words) {
const t: number[] = Array(26).fill(0);
for (const c of word) {
++t[c.charCodeAt(0) - aCode];
for (const w of words) {
const t = Array(26).fill(0);
for (const c of w) {
t[c.charCodeAt(0) - aCode]++;
}
for (let i = 0; i < 26; ++i) {
freq[i] = Math.min(freq[i], t[i]);
for (let i = 0; i < 26; i++) {
cnt[i] = Math.min(cnt[i], t[i]);
}
}
const ans: string[] = [];
for (let i = 0; i < 26; ++i) {
if (freq[i]) {
ans.push(...String.fromCharCode(i + aCode).repeat(freq[i]));
}
for (let i = 0; i < 26; i++) {
cnt[i] && ans.push(...String.fromCharCode(i + aCode).repeat(cnt[i]));
}
return ans;
}
Expand Down
Loading
Loading