forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekwangho.ts
More file actions
146 lines (122 loc) Β· 4.02 KB
/
Baekwangho.ts
File metadata and controls
146 lines (122 loc) Β· 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
ν©μ°μ΄ 0μ΄ λλ μλ‘μ μ‘°ν©μ μ°ΎμμΌ νλ€.. μ’νκ° λ€λ₯Έ κ°μ κ°μ νμ©νλ,
κ°μ μλ‘ κ΅¬μ±λ μ‘°ν©μ μ€λ³΅μ λΆννλ€.
κΈ°λ³Έμ μΌλ‘ 3κ°μ§ μλ₯Ό μ€λ³΅μμ΄ μ‘°ν©ν΄μ λ½μ λ€μ, μ λ ¬μ μ¬μ©νμ¬ set μΌλ‘ ꡬλΆν μ μμ§ μμκΉ?
function threeSum(nums: number[]): number[][] {
const arraySet = new Set<string>();
for (let i = 0; i <= nums.length - 3; i++) {
for (let j = i + 1; j <= nums.length - 2; j++) {
for (let k = j + 1; k <= nums.length - 1; k++) {
if (nums[i] + nums[j] + nums[k] === 0) {
const sorted = [nums[i], nums[j], nums[k]];
sorted.sort((a, b) => a - b);
const string = sorted.join("#");
arraySet.add(string);
}
}
}
}
return Array.from(arraySet).map((el) =>
el.split("#").map((el) => Number(el))
);
}
*/
/**
μκ° μ΄κ³Όν΄λ²λ Έλ€. μ΄λ»κ² ν¨μ¨μ μΌλ‘ ν μ μμκΉ?
two sum μ 보λ ν΄μν
μ΄λΈμ μ΄λ€κ³ νλ€. nums λ₯Ό ν΄μν
μ΄λΈλ‘ λ³ννκ³ ,
μ΄μ€ λ°λ³΅λ¬Έμμ μ΄λ₯Ό νμ©ν΄λ³΄μ.
function threeSum(nums: number[]): number[][] {
const arraySet = new Set<string>();
const hash = new Map<number, number[]>();
nums.forEach((val, idx) => {
const idxes = hash.get(val);
if (idxes && idxes.length) {
hash.set(val, [...idxes, idx]);
} else {
hash.set(val, [idx]);
}
});
for (let i = 0; i < nums.length - 2; i++) {
for (let j = i + 1; j < nums.length - 1; j++) {
const idxes = hash.get(0 - (nums[i] + nums[j]));
idxes?.forEach((idx) => {
if (idx != i && idx != j) {
const sorted = [nums[i], nums[j], nums[idx]];
sorted.sort((a, b) => a - b);
const string = sorted.join("#");
arraySet.add(string);
}
});
}
}
return Array.from(arraySet).map((el) =>
el.split("#").map((el) => Number(el))
);
}
*/
/**
... 0μΌλ‘λ§ κ°λμ°¬ λ°°μ΄μμ time limit μ΄ λ°μνλ€.
νμ΄λ₯Ό λ³΄κ³ μ€λ, μ λ‘μ§μμ 0μΌλ‘ κ°λμ°¬ arraySet μ μννλ κ³³μμ κ²°κ΅ O(n^3) μ΄ λμ΄λ²λ¦°λ€λ μ μ νμΈν μ μμλ€.
νμ€ν κ²μ, μ λ ¬μ μΆκ°νμ κ²½μ° μ λ‘μ§λ ν΅κ³Ό κ°λ₯ν μ λλ‘ λ§λ€μ΄λ³Ό μ μμ§ μμκΉ μΆμλ€.
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const arraySet = new Set<string>();
const hash = new Map<number, number[]>();
nums.forEach((val, idx) => {
const idxes = hash.get(val);
if (idxes && idxes.length) {
hash.set(val, [...idxes, idx]);
} else {
hash.set(val, [idx]);
}
});
for (let i = 0; i < nums.length - 2; i++) {
for (let j = i + 1; j < nums.length - 1; j++) {
const idxes = hash.get(0 - (nums[i] + nums[j])) ?? [];
for (let k = 0; k < idxes.length; k++) {
const idx = idxes[k];
if (idx != i && idx != j) {
const sorted = [nums[i], nums[j], nums[idx]];
sorted.sort((a, b) => a - b);
const string = sorted.join("#");
arraySet.add(string);
break;
}
}
}
}
return Array.from(arraySet).map((el) =>
el.split("#").map((el) => Number(el))
);
}
κ·Όλ° μ΄λ»κ²λ λͺ»λ§λ¦.
*/
/**
μ΄μ ν΄λ΅μ 보μμΌλ, λ€μνλ² νμ΄λ³΄μ.
ν΅μ¬μ ν¬ ν¬μΈν°λ₯Ό μ¬μ©νλ κ², μλ£κ΅¬μ‘°μ μ½λ©μ΄μ§ μλ κ²
*/
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const results = [];
// [-4, -1, -1, 0, 1, 2]
for (let i = 0; i < nums.length; i++) {
while (nums[i - 1] === nums[i]) i++;
let low = i + 1;
let high = nums.length - 1;
while (low < high) {
const sum = nums[i] + nums[low] + nums[high];
if (sum < 0) {
low++;
} else if (sum > 0) {
high--;
} else {
while (nums[high] === nums[high - 1]) high--;
while (nums[low] === nums[low + 1]) low++;
results.push([nums[i], nums[low], nums[high]]);
low++;
high--;
}
}
}
return results;
}