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 790bcbc

Browse files
committed
feat: add solutions to lcci problems: No.16.01,16.02,16.10
- No.16.01.Swap Numbers - No.16.02.Words Frequency - No.16.10.Living People
1 parent c04990a commit 790bcbc

File tree

9 files changed

+357
-0
lines changed

9 files changed

+357
-0
lines changed

‎lcci/16.01.Swap Numbers/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ function swapNumbers(numbers: number[]): number[] {
6262
}
6363
```
6464

65+
```ts
66+
function swapNumbers(numbers: number[]): number[] {
67+
[numbers[0], numbers[1]] = [numbers[1], numbers[0]];
68+
return numbers;
69+
}
70+
```
71+
6572
### **C++**
6673

6774
```cpp

‎lcci/16.02.Words Frequency/README.md‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,67 @@ WordsFrequency.prototype.get = function (word) {
108108
*/
109109
```
110110

111+
### **TypeScript**
112+
113+
```ts
114+
class WordsFrequency {
115+
private map: Map<string, number>;
116+
117+
constructor(book: string[]) {
118+
const map = new Map<string, number>();
119+
for (const word of book) {
120+
map.set(word, (map.get(word) ?? 0) + 1);
121+
}
122+
this.map = map;
123+
}
124+
125+
get(word: string): number {
126+
return this.map.get(word) ?? 0;
127+
}
128+
}
129+
130+
/**
131+
* Your WordsFrequency object will be instantiated and called as such:
132+
* var obj = new WordsFrequency(book)
133+
* var param_1 = obj.get(word)
134+
*/
135+
```
136+
137+
### **Rust**
138+
139+
```rust
140+
use std::collections::HashMap;
141+
struct WordsFrequency {
142+
counter: HashMap<String, i32>
143+
}
144+
145+
146+
/**
147+
* `&self` means the method takes an immutable reference.
148+
* If you need a mutable reference, change it to `&mut self` instead.
149+
*/
150+
impl WordsFrequency {
151+
152+
fn new(book: Vec<String>) -> Self {
153+
let mut counter = HashMap::new();
154+
for word in book.into_iter() {
155+
*counter.entry(word).or_insert(0) += 1;
156+
}
157+
Self { counter }
158+
}
159+
160+
fn get(&self, word: String) -> i32 {
161+
*self.counter.get(&word).unwrap_or(&0)
162+
}
163+
}
164+
165+
/**
166+
* Your WordsFrequency object will be instantiated and called as such:
167+
* let obj = WordsFrequency::new(book);
168+
* let ret_1: i32 = obj.get(word);
169+
*/
170+
```
171+
111172
### **...**
112173

113174
```

‎lcci/16.02.Words Frequency/README_EN.md‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,67 @@ WordsFrequency.prototype.get = function (word) {
113113
*/
114114
```
115115

116+
### **TypeScript**
117+
118+
```ts
119+
class WordsFrequency {
120+
private map: Map<string, number>;
121+
122+
constructor(book: string[]) {
123+
const map = new Map<string, number>();
124+
for (const word of book) {
125+
map.set(word, (map.get(word) ?? 0) + 1);
126+
}
127+
this.map = map;
128+
}
129+
130+
get(word: string): number {
131+
return this.map.get(word) ?? 0;
132+
}
133+
}
134+
135+
/**
136+
* Your WordsFrequency object will be instantiated and called as such:
137+
* var obj = new WordsFrequency(book)
138+
* var param_1 = obj.get(word)
139+
*/
140+
```
141+
142+
### **Rust**
143+
144+
```rust
145+
use std::collections::HashMap;
146+
struct WordsFrequency {
147+
counter: HashMap<String, i32>
148+
}
149+
150+
151+
/**
152+
* `&self` means the method takes an immutable reference.
153+
* If you need a mutable reference, change it to `&mut self` instead.
154+
*/
155+
impl WordsFrequency {
156+
157+
fn new(book: Vec<String>) -> Self {
158+
let mut counter = HashMap::new();
159+
for word in book.into_iter() {
160+
*counter.entry(word).or_insert(0) += 1;
161+
}
162+
Self { counter }
163+
}
164+
165+
fn get(&self, word: String) -> i32 {
166+
*self.counter.get(&word).unwrap_or(&0)
167+
}
168+
}
169+
170+
/**
171+
* Your WordsFrequency object will be instantiated and called as such:
172+
* let obj = WordsFrequency::new(book);
173+
* let ret_1: i32 = obj.get(word);
174+
*/
175+
```
176+
116177
### **...**
117178

118179
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use std::collections::HashMap;
2+
struct WordsFrequency {
3+
counter: HashMap<String, i32>
4+
}
5+
6+
7+
/**
8+
* `&self` means the method takes an immutable reference.
9+
* If you need a mutable reference, change it to `&mut self` instead.
10+
*/
11+
impl WordsFrequency {
12+
13+
fn new(book: Vec<String>) -> Self {
14+
let mut counter = HashMap::new();
15+
for word in book.into_iter() {
16+
*counter.entry(word).or_insert(0) += 1;
17+
}
18+
Self { counter }
19+
}
20+
21+
fn get(&self, word: String) -> i32 {
22+
*self.counter.get(&word).unwrap_or(&0)
23+
}
24+
}
25+
26+
/**
27+
* Your WordsFrequency object will be instantiated and called as such:
28+
* let obj = WordsFrequency::new(book);
29+
* let ret_1: i32 = obj.get(word);
30+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class WordsFrequency {
2+
private map: Map<string, number>;
3+
4+
constructor(book: string[]) {
5+
const map = new Map<string, number>();
6+
for (const word of book) {
7+
map.set(word, (map.get(word) ?? 0) + 1);
8+
}
9+
this.map = map;
10+
}
11+
12+
get(word: string): number {
13+
return this.map.get(word) ?? 0;
14+
}
15+
}
16+
17+
/**
18+
* Your WordsFrequency object will be instantiated and called as such:
19+
* var obj = new WordsFrequency(book)
20+
* var param_1 = obj.get(word)
21+
*/

‎lcci/16.10.Living People/README.md‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ death = {1948, 1951, 2000}
2424

2525
<!-- 这里可写通用的实现逻辑 -->
2626

27+
不在乎某个区间,而是某一年的最多存活人数。
28+
29+
可以使用哈希表来统计每年的存活人数,当 `birth[i] >= year && year <= death[i]`,该年份的存活人数加一。
30+
31+
> 只有 `birth``death` 当中的出现过的年份才是有效年份,或者说,可能成为返回值的年份。
32+
33+
题目当中已说明年份范围是 `1900 ~ 2000`,对此也可以使用数组进行计数(`year - 1900`)。
34+
2735
<!-- tabs:start -->
2836

2937
### **Python3**
@@ -77,6 +85,66 @@ class Solution {
7785
}
7886
```
7987

88+
### **TypeScript**
89+
90+
```ts
91+
function maxAliveYear(birth: number[], death: number[]): number {
92+
const n = birth.length;
93+
const counter = new Map<number, number>();
94+
for (let i = 0; i < n; i++) {
95+
counter.set(birth[i], 0);
96+
counter.set(death[i], 0);
97+
}
98+
for (let i = 0; i < n; i++) {
99+
const start = birth[i];
100+
const end = death[i];
101+
for (const key of counter.keys()) {
102+
if (key >= start && key <= end) {
103+
counter.set(key, (counter.get(key) ?? 0) + 1);
104+
}
105+
}
106+
}
107+
let res = 0;
108+
let max = 0;
109+
for (const [key, val] of counter) {
110+
if (val === max) {
111+
res = Math.min(res, key);
112+
} else if (val > max) {
113+
res = key;
114+
max = Math.max(max, val);
115+
}
116+
}
117+
return res;
118+
}
119+
120+
```
121+
122+
### **Rust**
123+
124+
```rust
125+
impl Solution {
126+
pub fn max_alive_year(birth: Vec<i32>, death: Vec<i32>) -> i32 {
127+
let n = birth.len();
128+
let mut counter = vec![0; 101];
129+
for i in 0..n {
130+
let (start, end) = (birth[i] - 1900, death[i] - 1900);
131+
for j in start..=end {
132+
counter[j as usize] += 1;
133+
}
134+
}
135+
let mut res = 0;
136+
let mut max = 0;
137+
for (i, count) in counter.iter().enumerate() {
138+
if *count > max {
139+
res = i;
140+
max = *count;
141+
}
142+
}
143+
(res + 1900) as i32
144+
}
145+
}
146+
```
147+
80148
### **...**
81149

82150
```

‎lcci/16.10.Living People/README_EN.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,66 @@ class Solution {
8080
}
8181
```
8282

83+
### **TypeScript**
84+
85+
```ts
86+
function maxAliveYear(birth: number[], death: number[]): number {
87+
const n = birth.length;
88+
const counter = new Map<number, number>();
89+
for (let i = 0; i < n; i++) {
90+
counter.set(birth[i], 0);
91+
counter.set(death[i], 0);
92+
}
93+
for (let i = 0; i < n; i++) {
94+
const start = birth[i];
95+
const end = death[i];
96+
for (const key of counter.keys()) {
97+
if (key >= start && key <= end) {
98+
counter.set(key, (counter.get(key) ?? 0) + 1);
99+
}
100+
}
101+
}
102+
let res = 0;
103+
let max = 0;
104+
for (const [key, val] of counter) {
105+
if (val === max) {
106+
res = Math.min(res, key);
107+
} else if (val > max) {
108+
res = key;
109+
max = Math.max(max, val);
110+
}
111+
}
112+
return res;
113+
}
114+
115+
```
116+
117+
### **Rust**
118+
119+
```rust
120+
impl Solution {
121+
pub fn max_alive_year(birth: Vec<i32>, death: Vec<i32>) -> i32 {
122+
let n = birth.len();
123+
let mut counter = vec![0; 101];
124+
for i in 0..n {
125+
let (start, end) = (birth[i] - 1900, death[i] - 1900);
126+
for j in start..=end {
127+
counter[j as usize] += 1;
128+
}
129+
}
130+
let mut res = 0;
131+
let mut max = 0;
132+
for (i, count) in counter.iter().enumerate() {
133+
if *count > max {
134+
res = i;
135+
max = *count;
136+
}
137+
}
138+
(res + 1900) as i32
139+
}
140+
}
141+
```
142+
83143
### **...**
84144

85145
```

‎lcci/16.10.Living People/Solution.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn max_alive_year(birth: Vec<i32>, death: Vec<i32>) -> i32 {
3+
let n = birth.len();
4+
let mut counter = vec![0; 101];
5+
for i in 0..n {
6+
let (start, end) = (birth[i] - 1900, death[i] - 1900);
7+
for j in start..=end {
8+
counter[j as usize] += 1;
9+
}
10+
}
11+
let mut res = 0;
12+
let mut max = 0;
13+
for (i, count) in counter.iter().enumerate() {
14+
if *count > max {
15+
res = i;
16+
max = *count;
17+
}
18+
}
19+
(res + 1900) as i32
20+
}
21+
}

0 commit comments

Comments
(0)

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