Skip to content
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

feat: update lc problems #3697

Merged
merged 1 commit into from
Oct 30, 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
10 changes: 5 additions & 5 deletions lcof2/剑指 Offer II 109. 开密码锁/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ class Solution {
if target == "0000" {
return 0
}

var visited = Set<String>()
var queue = ["0000"]
visited.insert("0000")
var step = 0

while !queue.isEmpty {
step += 1
for _ in 0..<queue.count {
Expand All @@ -311,10 +311,10 @@ class Solution {
}
}
}

return -1
}

private func getNeighbors(_ lock: String) -> [String] {
var neighbors = [String]()
var chars = Array(lock)
Expand All @@ -328,7 +328,7 @@ class Solution {
}
return neighbors
}

private func prevChar(_ c: Character) -> Character {
return c == "0" ? "9" : Character(UnicodeScalar(c.asciiValue! - 1))
}
Expand Down
2 changes: 1 addition & 1 deletion lcof2/剑指 Offer II 110. 所有路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Solution {
results.append(Array(path))
return
}

for next in graph[node] {
path.append(next)
dfs(next, &path)
Expand Down
24 changes: 12 additions & 12 deletions lcof2/剑指 Offer II 111. 计算除法/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,23 +298,23 @@ func find(x int) int {
class Solution {
private var parent = [Int]()
private var weight = [Double]()

func calcEquation(
_ equations: [[String]],
_ values: [Double],
_ equations: [[String]],
_ values: [Double],
_ queries: [[String]]
) -> [Double] {
let n = equations.count
parent = Array(0..<(n * 2))
weight = Array(repeating: 1.0, count: n * 2)

var map = [String: Int]()
var index = 0

for i in 0..<n {
let a = equations[i][0]
let b = equations[i][1]

if map[a] == nil {
map[a] = index
index += 1
Expand All @@ -323,18 +323,18 @@ class Solution {
map[b] = index
index += 1
}

let pa = find(map[a]!)
let pb = find(map[b]!)

if pa != pb {
parent[pa] = pb
weight[pa] = weight[map[b]!] * values[i] / weight[map[a]!]
}
}

var result = [Double]()

for query in queries {
let (c, d) = (query[0], query[1])
if let id1 = map[c], let id2 = map[d], find(id1) == find(id2) {
Expand All @@ -343,10 +343,10 @@ class Solution {
result.append(-1.0)
}
}

return result
}

private func find(_ x: Int) -> Int {
if parent[x] != x {
let origin = parent[x]
Expand Down
4 changes: 2 additions & 2 deletions lcof2/剑指 Offer II 112. 最长递增路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class Solution {
m = matrix.count
n = matrix[0].count
memo = Array(repeating: Array(repeating: -1, count: n), count: m)

var ans = 0
for i in 0..<m {
for j in 0..<n {
Expand All @@ -235,7 +235,7 @@ class Solution {
}
var ans = 1
let dirs = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for (dx, dy) in dirs {
let x = i + dx, y = j + dy
if x >= 0, x < m, y >= 0, y < n, matrix[x][y] > matrix[i][j] {
Expand Down
10 changes: 5 additions & 5 deletions lcof2/剑指 Offer II 113. 课程顺序/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,34 +276,34 @@ class Solution {
func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {
var graph = Array(repeating: [Int](), count: numCourses)
var indegree = Array(repeating: 0, count: numCourses)

for prereq in prerequisites {
let course = prereq[0]
let prereqCourse = prereq[1]
graph[prereqCourse].append(course)
indegree[course] += 1
}

var queue = [Int]()
for i in 0..<numCourses {
if indegree[i] == 0 {
queue.append(i)
}
}

var order = [Int]()
while !queue.isEmpty {
let course = queue.removeFirst()
order.append(course)

for nextCourse in graph[course] {
indegree[nextCourse] -= 1
if indegree[nextCourse] == 0 {
queue.append(nextCourse)
}
}
}

return order.count == numCourses ? order : []
}
}
Expand Down
16 changes: 8 additions & 8 deletions lcof2/剑指 Offer II 114. 外星文字典/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class Solution {
var indegree = Array(repeating: 0, count: 26)
var seen = Array(repeating: false, count: 26)
var letterCount = 0

for i in 0..<words.count - 1 {
for char in words[i] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
Expand All @@ -371,39 +371,39 @@ class Solution {
for j in 0..<minLength {
let char1 = words[i][words[i].index(words[i].startIndex, offsetBy: j)]
let char2 = words[i + 1][words[i + 1].index(words[i + 1].startIndex, offsetBy: j)]

if char1 != char2 {
let c1 = Int(char1.asciiValue! - Character("a").asciiValue!)
let c2 = Int(char2.asciiValue! - Character("a").asciiValue!)

if !graph[c1].contains(c2) {
graph[c1].insert(c2)
indegree[c2] += 1
}
break
}

if j == minLength - 1 && words[i].count > words[i + 1].count {
return ""
}
}
}

for char in words[words.count - 1] {
let index = Int(char.asciiValue! - Character("a").asciiValue!)
if !seen[index] {
seen[index] = true
letterCount += 1
}
}

var queue = [Int]()
for i in 0..<26 {
if seen[i] && indegree[i] == 0 {
queue.append(i)
}
}

var order = ""
while !queue.isEmpty {
let u = queue.removeFirst()
Expand All @@ -415,7 +415,7 @@ class Solution {
}
}
}

return order.count == letterCount ? order : ""
}
}
Expand Down
112 changes: 58 additions & 54 deletions solution/2200-2299/2222.Number of Ways to Select Buildings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ tags:

<!-- solution:start -->

### 方法一:统计 010 和 101 的出现次数
### 方法一:计数 + 枚举

有效方案只有两种情况:$010$ 和 $101$。枚举中间数字,累计方案数
根据题目描述,我们需要选择 $3$ 栋建筑,且相邻的两栋不能是同一类型

时间复杂度 $O(n)$,其中 $n$ 表示 $s$ 的长度。
我们可以枚举中间的建筑,假设为 $x$,那么左右两边的建筑类型只能是 $x \oplus 1$,其中 $\oplus$ 表示异或运算。因此,我们可以使用两个数组 $l$ 和 $r$ 分别记录左右两边的建筑类型的数量,然后枚举中间的建筑,计算答案即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -87,18 +89,13 @@ tags:
```python
class Solution:
def numberOfWays(self, s: str) -> int:
n = len(s)
cnt0 = s.count("0")
cnt1 = n - cnt0
c0 = c1 = 0
l = [0, 0]
r = [s.count("0"), s.count("1")]
ans = 0
for c in s:
if c == "0":
ans += c1 * (cnt1 - c1)
c0 += 1
else:
ans += c0 * (cnt0 - c0)
c1 += 1
for x in map(int, s):
r[x] -= 1
ans += l[x ^ 1] * r[x ^ 1]
l[x] += 1
return ans
```

Expand All @@ -108,23 +105,17 @@ class Solution:
class Solution {
public long numberOfWays(String s) {
int n = s.length();
int cnt0 = 0;
for (char c : s.toCharArray()) {
if (c == '0') {
++cnt0;
}
int[] l = new int[2];
int[] r = new int[2];
for (int i = 0; i < n; ++i) {
r[s.charAt(i) - '0']++;
}
int cnt1 = n - cnt0;
long ans = 0;
int c0 = 0, c1 = 0;
for (char c : s.toCharArray()) {
if (c == '0') {
ans += c1 * (cnt1 - c1);
++c0;
} else {
ans += c0 * (cnt0 - c0);
++c1;
}
for (int i = 0; i < n; ++i) {
int x = s.charAt(i) - '0';
r[x]--;
ans += 1L * l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}
Expand All @@ -138,19 +129,16 @@ class Solution {
public:
long long numberOfWays(string s) {
int n = s.size();
int cnt0 = 0;
for (char& c : s) cnt0 += c == '0';
int cnt1 = n - cnt0;
int c0 = 0, c1 = 0;
int l[2]{};
int r[2]{};
r[0] = ranges::count(s, '0');
r[1] = n - r[0];
long long ans = 0;
for (char& c : s) {
if (c == '0') {
ans += c1 * (cnt1 - c1);
++c0;
} else {
ans += c0 * (cnt0 - c0);
++c1;
}
for (int i = 0; i < n; ++i) {
int x = s[i] - '0';
r[x]--;
ans += 1LL * l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}
Expand All @@ -160,22 +148,38 @@ public:
#### Go

```go
func numberOfWays(s string) int64 {
func numberOfWays(s string) (ans int64) {
n := len(s)
cnt0 := strings.Count(s, "0")
cnt1 := n - cnt0
c0, c1 := 0, 0
ans := 0
l := [2]int{}
r := [2]int{}
r[0] = strings.Count(s, "0")
r[1] = n - r[0]
for _, c := range s {
if c == '0' {
ans += c1 * (cnt1 - c1)
c0++
} else {
ans += c0 * (cnt0 - c0)
c1++
}
x := int(c - '0')
r[x]--
ans += int64(l[x^1] * r[x^1])
l[x]++
}
return int64(ans)
return
}
```

#### TypeScript

```ts
function numberOfWays(s: string): number {
const n = s.length;
const l: number[] = [0, 0];
const r: number[] = [s.split('').filter(c => c === '0').length, 0];
r[1] = n - r[0];
let ans: number = 0;
for (const c of s) {
const x = c === '0' ? 0 : 1;
r[x]--;
ans += l[x ^ 1] * r[x ^ 1];
l[x]++;
}
return ans;
}
```

Expand Down
Loading
Loading