Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64f4426

Browse files
feat: add solutions to lc problems: No.2678,2679,2680 (#1866)
* No.2678.Number of Senior Citizens * No.2679.Sum in a Matrix * No.2680.Maximum OR
1 parent 18815d4 commit 64f4426

File tree

10 files changed

+277
-1
lines changed

10 files changed

+277
-1
lines changed

‎solution/2600-2699/2678.Number of Senior Citizens/README.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
遍历结束后,返回答案即可。
5959

60-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 是 `details` 的长度。
60+
时间复杂度 $O(n),ドル其中 $n$ 是 `details` 的长度。空间复杂度 $O(1)$
6161

6262
<!-- tabs:start -->
6363

@@ -152,6 +152,21 @@ impl Solution {
152152
}
153153
```
154154

155+
### **TypeScript**
156+
157+
```ts
158+
function countSeniors(details: string[]): number {
159+
let ans = 0;
160+
for (const x of details) {
161+
const age = parseInt(x.slice(11, 13));
162+
if (age > 60) {
163+
++ans;
164+
}
165+
}
166+
return ans;
167+
}
168+
```
169+
155170
### **...**
156171

157172
```

‎solution/2600-2699/2678.Number of Senior Citizens/README_EN.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Traversal and Counting**
49+
50+
We can traverse each string $x$ in `details` and convert the 12ドル$th and 13ドル$th characters (indexed at 11ドル$ and 12ドル$) of $x$ to integers, and check if they are greater than 60ドル$. If so, we add one to the answer.
51+
52+
After the traversal, we return the answer.
53+
54+
The time complexity is $O(n),ドル where $n$ is the length of `details`. The space complexity is $O(1)`.
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**
@@ -134,6 +142,21 @@ impl Solution {
134142
}
135143
```
136144

145+
### **TypeScript**
146+
147+
```ts
148+
function countSeniors(details: string[]): number {
149+
let ans = 0;
150+
for (const x of details) {
151+
const age = parseInt(x.slice(11, 13));
152+
if (age > 60) {
153+
++ans;
154+
}
155+
}
156+
return ans;
157+
}
158+
```
159+
137160
### **...**
138161

139162
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function countSeniors(details: string[]): number {
2+
let ans = 0;
3+
for (const x of details) {
4+
const age = parseInt(x.slice(11, 13));
5+
if (age > 60) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}

‎solution/2600-2699/2679.Sum in a Matrix/README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,46 @@ function matrixSum(nums: number[][]): number {
157157
}
158158
```
159159

160+
### **Rust**
161+
162+
```rust
163+
impl Solution {
164+
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
165+
let mut nums = nums;
166+
for row in nums.iter_mut() {
167+
row.sort();
168+
}
169+
let transposed: Vec<Vec<i32>> = (0..nums[0].len()).map(|i| {
170+
nums.iter().map(|row| row[i]).collect()
171+
}).collect();
172+
173+
transposed.iter().map(|row| row.iter().max().unwrap()).sum()
174+
}
175+
}
176+
```
177+
178+
```rust
179+
impl Solution {
180+
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
181+
let mut nums = nums.clone();
182+
for row in nums.iter_mut() {
183+
row.sort();
184+
}
185+
186+
let mut ans = 0;
187+
for j in 0..nums[0].len() {
188+
let mut mx = 0;
189+
for row in &nums {
190+
mx = mx.max(row[j]);
191+
}
192+
ans += mx;
193+
}
194+
195+
ans
196+
}
197+
}
198+
```
199+
160200
### **...**
161201

162202
```

‎solution/2600-2699/2679.Sum in a Matrix/README_EN.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,46 @@ function matrixSum(nums: number[][]): number {
138138
}
139139
```
140140

141+
### **Rust**
142+
143+
```rust
144+
impl Solution {
145+
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
146+
let mut nums = nums;
147+
for row in nums.iter_mut() {
148+
row.sort();
149+
}
150+
let transposed: Vec<Vec<i32>> = (0..nums[0].len()).map(|i| {
151+
nums.iter().map(|row| row[i]).collect()
152+
}).collect();
153+
154+
transposed.iter().map(|row| row.iter().max().unwrap()).sum()
155+
}
156+
}
157+
```
158+
159+
```rust
160+
impl Solution {
161+
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
162+
let mut nums = nums.clone();
163+
for row in nums.iter_mut() {
164+
row.sort();
165+
}
166+
167+
let mut ans = 0;
168+
for j in 0..nums[0].len() {
169+
let mut mx = 0;
170+
for row in &nums {
171+
mx = mx.max(row[j]);
172+
}
173+
ans += mx;
174+
}
175+
176+
ans
177+
}
178+
}
179+
```
180+
141181
### **...**
142182

143183
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn matrix_sum(nums: Vec<Vec<i32>>) -> i32 {
3+
let mut nums = nums.clone();
4+
for row in nums.iter_mut() {
5+
row.sort();
6+
}
7+
8+
let mut ans = 0;
9+
for j in 0..nums[0].len() {
10+
let mut mx = 0;
11+
for row in &nums {
12+
mx = mx.max(row[j]);
13+
}
14+
ans += mx;
15+
}
16+
17+
ans
18+
}
19+
}

‎solution/2600-2699/2680.Maximum OR/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,49 @@ func max(a, b int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function maximumOr(nums: number[], k: number): number {
150+
const n = nums.length;
151+
const suf: bigint[] = Array(n + 1).fill(0n);
152+
for (let i = n - 1; i >= 0; i--) {
153+
suf[i] = suf[i + 1] | BigInt(nums[i]);
154+
}
155+
let [ans, pre] = [0, 0n];
156+
for (let i = 0; i < n; i++) {
157+
ans = Math.max(Number(ans), Number(pre | (BigInt(nums[i]) << BigInt(k)) | suf[i + 1]));
158+
pre |= BigInt(nums[i]);
159+
}
160+
return ans;
161+
}
162+
```
163+
164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
169+
let n = nums.len();
170+
let mut suf = vec![0; n + 1];
171+
172+
for i in (0..n).rev() {
173+
suf[i] = suf[i + 1] | nums[i] as i64;
174+
}
175+
176+
let mut ans = 0i64;
177+
let mut pre = 0i64;
178+
let k64 = k as i64;
179+
for i in 0..n {
180+
ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]);
181+
pre |= nums[i] as i64;
182+
}
183+
184+
ans
185+
}
186+
}
187+
```
188+
146189
### **...**
147190

148191
```

‎solution/2600-2699/2680.Maximum OR/README_EN.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Greedy + Preprocessing**
42+
43+
We notice that in order to maximize the answer, we should apply $k$ times of bitwise OR to the same number.
44+
45+
First, we preprocess the suffix OR value array $suf$ of the array $nums,ドル where $suf[i]$ represents the bitwise OR value of $nums[i], nums[i + 1], \cdots, nums[n - 1]$.
46+
47+
Next, we traverse the array $nums$ from left to right, and maintain the current prefix OR value $pre$. For the current position $i,ドル we perform $k$ times of bitwise left shift on $nums[i],ドル i.e., $nums[i] \times 2^k,ドル and perform bitwise OR operation with $pre$ to obtain the intermediate result. Then, we perform bitwise OR operation with $suf[i + 1]$ to obtain the maximum OR value with $nums[i]$ as the last number. By enumerating all possible positions $i,ドル we can obtain the final answer.
48+
49+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
50+
4151
<!-- tabs:start -->
4252

4353
### **Python3**
@@ -123,6 +133,49 @@ func max(a, b int) int {
123133
}
124134
```
125135

136+
### **TypeScript**
137+
138+
```ts
139+
function maximumOr(nums: number[], k: number): number {
140+
const n = nums.length;
141+
const suf: bigint[] = Array(n + 1).fill(0n);
142+
for (let i = n - 1; i >= 0; i--) {
143+
suf[i] = suf[i + 1] | BigInt(nums[i]);
144+
}
145+
let [ans, pre] = [0, 0n];
146+
for (let i = 0; i < n; i++) {
147+
ans = Math.max(Number(ans), Number(pre | (BigInt(nums[i]) << BigInt(k)) | suf[i + 1]));
148+
pre |= BigInt(nums[i]);
149+
}
150+
return ans;
151+
}
152+
```
153+
154+
### **Rust**
155+
156+
```rust
157+
impl Solution {
158+
pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
159+
let n = nums.len();
160+
let mut suf = vec![0; n + 1];
161+
162+
for i in (0..n).rev() {
163+
suf[i] = suf[i + 1] | nums[i] as i64;
164+
}
165+
166+
let mut ans = 0i64;
167+
let mut pre = 0i64;
168+
let k64 = k as i64;
169+
for i in 0..n {
170+
ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]);
171+
pre |= nums[i] as i64;
172+
}
173+
174+
ans
175+
}
176+
}
177+
```
178+
126179
### **...**
127180

128181
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
3+
let n = nums.len();
4+
let mut suf = vec![0; n + 1];
5+
6+
for i in (0..n).rev() {
7+
suf[i] = suf[i + 1] | nums[i] as i64;
8+
}
9+
10+
let mut ans = 0i64;
11+
let mut pre = 0i64;
12+
let k64 = k as i64;
13+
for i in 0..n {
14+
ans = ans.max(pre | ((nums[i] as i64) << k64) | suf[i + 1]);
15+
pre |= nums[i] as i64;
16+
}
17+
18+
ans
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maximumOr(nums: number[], k: number): number {
2+
const n = nums.length;
3+
const suf: bigint[] = Array(n + 1).fill(0n);
4+
for (let i = n - 1; i >= 0; i--) {
5+
suf[i] = suf[i + 1] | BigInt(nums[i]);
6+
}
7+
let [ans, pre] = [0, 0n];
8+
for (let i = 0; i < n; i++) {
9+
ans = Math.max(Number(ans), Number(pre | (BigInt(nums[i]) << BigInt(k)) | suf[i + 1]));
10+
pre |= BigInt(nums[i]);
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /