Skip to content

Commit e7d01dc

Browse files
authored
feat: add solutions to lcci problems: No.17.16, No.16.24 (#924)
* No.16.24.Pairs With Sum * No.17.16.The Masseuse
1 parent 08ff287 commit e7d01dc

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

lcci/16.24.Pairs With Sum/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ func pairSums(nums []int, target int) (ans [][]int) {
118118
}
119119
```
120120

121+
### **TypeScript**
122+
123+
```ts
124+
function pairSums(nums: number[], target: number): number[][] {
125+
const cnt = new Map();
126+
const ans: number[][] = [];
127+
for (const x of nums) {
128+
const y = target - x;
129+
if (cnt.has(y)) {
130+
ans.push([x, y]);
131+
const yCount = cnt.get(y) - 1;
132+
if (yCount === 0) {
133+
cnt.delete(y);
134+
} else {
135+
cnt.set(y, yCount);
136+
}
137+
} else {
138+
cnt.set(x, (cnt.get(x) || 0) + 1);
139+
}
140+
}
141+
return ans;
142+
}
143+
```
144+
121145
### **...**
122146

123147
```

lcci/16.24.Pairs With Sum/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ func pairSums(nums []int, target int) (ans [][]int) {
106106
}
107107
```
108108

109+
### **TypeScript**
110+
111+
```ts
112+
function pairSums(nums: number[], target: number): number[][] {
113+
const cnt = new Map();
114+
const ans: number[][] = [];
115+
for (const x of nums) {
116+
const y = target - x;
117+
if (cnt.has(y)) {
118+
ans.push([x, y]);
119+
const yCount = cnt.get(y) - 1;
120+
if (yCount === 0) {
121+
cnt.delete(y);
122+
} else {
123+
cnt.set(y, yCount);
124+
}
125+
} else {
126+
cnt.set(x, (cnt.get(x) || 0) + 1);
127+
}
128+
}
129+
return ans;
130+
}
131+
```
132+
109133
### **...**
110134

111135
```

lcci/16.24.Pairs With Sum/Solution.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function pairSums(nums: number[], target: number): number[][] {
2+
const cnt = new Map();
3+
const ans: number[][] = [];
4+
for (const x of nums) {
5+
const y = target - x;
6+
if (cnt.has(y)) {
7+
ans.push([x, y]);
8+
const yCount = cnt.get(y) - 1;
9+
if (yCount === 0) {
10+
cnt.delete(y);
11+
} else {
12+
cnt.set(y, yCount);
13+
}
14+
} else {
15+
cnt.set(x, (cnt.get(x) || 0) + 1);
16+
}
17+
}
18+
return ans;
19+
}

lcci/17.16.The Masseuse/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ func max(a, b int) int {
126126
}
127127
```
128128

129+
### **TypeScript**
130+
131+
```ts
132+
function massage(nums: number[]): number {
133+
let f = 0,
134+
g = 0;
135+
for (const x of nums) {
136+
const ff = g + x;
137+
const gg = Math.max(f, g);
138+
f = ff;
139+
g = gg;
140+
}
141+
return Math.max(f, g);
142+
}
143+
```
144+
129145
### **...**
130146

131147
```

lcci/17.16.The Masseuse/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ func max(a, b int) int {
115115
}
116116
```
117117

118+
### **TypeScript**
119+
120+
```ts
121+
function massage(nums: number[]): number {
122+
let f = 0,
123+
g = 0;
124+
for (const x of nums) {
125+
const ff = g + x;
126+
const gg = Math.max(f, g);
127+
f = ff;
128+
g = gg;
129+
}
130+
return Math.max(f, g);
131+
}
132+
```
133+
118134
### **...**
119135

120136
```

lcci/17.16.The Masseuse/Solution.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function massage(nums: number[]): number {
2+
let f = 0,
3+
g = 0;
4+
for (const x of nums) {
5+
const ff = g + x;
6+
const gg = Math.max(f, g);
7+
f = ff;
8+
g = gg;
9+
}
10+
return Math.max(f, g);
11+
}

0 commit comments

Comments
 (0)