Skip to content

Commit f6ef5cf

Browse files
committed
feat: add solutions to lc problem: No.1710
No.1710.Maximum Units on a Truck
1 parent 6d6dbe4 commit f6ef5cf

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

solution/1700-1799/1710.Maximum Units on a Truck/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,48 @@ func min(a, b int) int {
143143
}
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
function maximumUnits(boxTypes: number[][], truckSize: number): number {
150+
boxTypes.sort((a, b) => b[1] - a[1]);
151+
let sum = 0;
152+
let ans = 0;
153+
for (const [count, size] of boxTypes) {
154+
if (sum + count < truckSize) {
155+
ans += size * count;
156+
sum += count;
157+
} else {
158+
ans += (truckSize - sum) * size;
159+
break;
160+
}
161+
}
162+
return ans;
163+
}
164+
```
165+
166+
### **Rust**
167+
168+
```rust
169+
impl Solution {
170+
pub fn maximum_units(mut box_types: Vec<Vec<i32>>, truck_size: i32) -> i32 {
171+
box_types.sort_by(|a, b| b[1].cmp(&a[1]));
172+
let mut sum = 0;
173+
let mut ans = 0;
174+
for box_type in box_types.iter() {
175+
if sum + box_type[0] < truck_size {
176+
sum += box_type[0];
177+
ans += box_type[0] * box_type[1];
178+
} else {
179+
ans += (truck_size - sum) * box_type[1];
180+
break;
181+
}
182+
}
183+
ans
184+
}
185+
}
186+
```
187+
146188
### **...**
147189

148190
```

solution/1700-1799/1710.Maximum Units on a Truck/README_EN.md

+42
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,48 @@ func min(a, b int) int {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
function maximumUnits(boxTypes: number[][], truckSize: number): number {
133+
boxTypes.sort((a, b) => b[1] - a[1]);
134+
let sum = 0;
135+
let ans = 0;
136+
for (const [count, size] of boxTypes) {
137+
if (sum + count < truckSize) {
138+
ans += size * count;
139+
sum += count;
140+
} else {
141+
ans += (truckSize - sum) * size;
142+
break;
143+
}
144+
}
145+
return ans;
146+
}
147+
```
148+
149+
### **Rust**
150+
151+
```rust
152+
impl Solution {
153+
pub fn maximum_units(mut box_types: Vec<Vec<i32>>, truck_size: i32) -> i32 {
154+
box_types.sort_by(|a, b| b[1].cmp(&a[1]));
155+
let mut sum = 0;
156+
let mut ans = 0;
157+
for box_type in box_types.iter() {
158+
if sum + box_type[0] < truck_size {
159+
sum += box_type[0];
160+
ans += box_type[0] * box_type[1];
161+
} else {
162+
ans += (truck_size - sum) * box_type[1];
163+
break;
164+
}
165+
}
166+
ans
167+
}
168+
}
169+
```
170+
129171
### **...**
130172

131173
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn maximum_units(mut box_types: Vec<Vec<i32>>, truck_size: i32) -> i32 {
3+
box_types.sort_by(|a, b| b[1].cmp(&a[1]));
4+
let mut sum = 0;
5+
let mut ans = 0;
6+
for box_type in box_types.iter() {
7+
if sum + box_type[0] < truck_size {
8+
sum += box_type[0];
9+
ans += box_type[0] * box_type[1];
10+
} else {
11+
ans += (truck_size - sum) * box_type[1];
12+
break;
13+
}
14+
}
15+
ans
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maximumUnits(boxTypes: number[][], truckSize: number): number {
2+
boxTypes.sort((a, b) => b[1] - a[1]);
3+
let sum = 0;
4+
let ans = 0;
5+
for (const [count, size] of boxTypes) {
6+
if (sum + count < truckSize) {
7+
ans += size * count;
8+
sum += count;
9+
} else {
10+
ans += (truckSize - sum) * size;
11+
break;
12+
}
13+
}
14+
return ans;
15+
}

0 commit comments

Comments
 (0)