λ¬Έμ μ€λͺ
λ¬Έμμ΄ sκ° μ£Όμ΄μ‘μ λ, sμ κ° μμΉλ§λ€ μμ λ³΄λ€ μμ λμμΌλ©΄μ, μμ κ³Ό κ°μ₯ κ°κΉμ΄ κ³³μ μλ κ°μ κΈμκ° μ΄λ μλμ§ μκ³ μΆμ΅λλ€.
μλ₯Ό λ€μ΄, s = "banana"λΌκ³ ν λ, κ° κΈμλ€μ μΌμͺ½λΆν° μ€λ₯Έμͺ½μΌλ‘ μ½μ΄ λκ°λ©΄μ λ€μκ³Ό κ°μ΄ μ§νν μ μμ΅λλ€.
- bλ μ²μ λμκΈ° λλ¬Έμ μμ μ μμ κ°μ κΈμκ° μμ΅λλ€. μ΄λ -1λ‘ ννν©λλ€.
- aλ μ²μ λμκΈ° λλ¬Έμ μμ μ μμ κ°μ κΈμκ° μμ΅λλ€. μ΄λ -1λ‘ ννν©λλ€.
- nμ μ²μ λμκΈ° λλ¬Έμ μμ μ μμ κ°μ κΈμκ° μμ΅λλ€. μ΄λ -1λ‘ ννν©λλ€.
- aλ μμ λ³΄λ€ λ μΉΈ μμ aκ° μμ΅λλ€. μ΄λ 2λ‘ ννν©λλ€.
- nλ μμ λ³΄λ€ λ μΉΈ μμ nμ΄ μμ΅λλ€. μ΄λ 2λ‘ ννν©λλ€.
- aλ μμ λ³΄λ€ λ μΉΈ, λ€ μΉΈ μμ aκ° μμ΅λλ€. μ΄ μ€ κ°κΉμ΄ κ²μ λ μΉΈ μμ΄κ³ , μ΄λ 2λ‘ ννν©λλ€.
λ°λΌμ, μ΅μ’ κ²°κ³Όλ¬Όμ [ -1, -1, -1, 2, 2, 2 ]κ° λ©λλ€.
λ¬Έμμ΄ sκ° μ£Όμ΄μ§ λ, μμ κ°μ΄ μ μλ μ°μ°μ μννλ ν¨μ solutionμ μμ±ν΄μ£ΌμΈμ.
μ ν 쑰건
- 1 ≤ sμ κΈΈμ΄ ≤ 10,000. sμ μμ΄ μλ¬Έμλ‘λ§ μ΄λ£¨μ΄μ Έ μμ΅λλ€.
μ μΆλ ₯ μ
s | result |
"banana" | [ -1, -1, -1, 2, 2, 2 ] |
"foodbar" | [ -1, -1, 1, -1, -1, -1 ] |
< λμ νμ΄ >
function solution(s) {
let arr = s.split('');
let len = arr.length;
let answer = Array(len).fill(-1);
for ( let i = 0 ; i < arr.length ; i++ ){
for ( let j = 0 ; j < i ; j++){
if( arr[i] == arr[j] ){
answer[i] = i - j;
}
}
}
return answer;
}
μ€ν μκ°μ΄ λλ €μ κ°μ μ΄ νμνλ€. ( μ±λ₯ μμ½ - λ©λͺ¨λ¦¬ : 38MB , μκ° : 369.72 ms )
< λ€λ₯Έ μ¬λμ νμ΄ >
function solution(s) {
const hash = {};
return [...s].map((v, i) => {
let result = hash[v] !== undefined ? i - hash[v] : -1;
hash[v] = i;
return result;
});
}
// μ΅μ’
μ μΌλ‘ hash = { b: 0, a: 5, n: 4 }
- μ΄ν΄λ₯Ό μν΄μ μΆλ ₯μ ν΄ λ³΄μλ€.
function solution(s) {
const hash = {};
return [...s].map((v, i) => {
// κ° λ¬Έμμ μΈλ±μ€λ₯Ό κ³μ°νκΈ° μ μ νμ¬ hash μνλ₯Ό μΆλ ₯
console.log(`Before processing character '${v}' at index ${i}:`, JSON.stringify(hash));
let result = hash[v] !== undefined ? i - hash[v] : -1;
hash[v] = i;
// λ¬Έμλ₯Ό μ²λ¦¬ν ν hash μνλ₯Ό μΆλ ₯
console.log(`After processing character '${v}' at index ${i}:`, JSON.stringify(hash));
return result;
});
}
// ν
μ€νΈ μ€ν
console.log(solution("banana"));
// Before processing character 'b' at index 0: {}
// After processing character 'b' at index 0: {"b":0}
// Before processing character 'a' at index 1: {"b":0}
// After processing character 'a' at index 1: {"b":0,"a":1}
// Before processing character 'n' at index 2: {"b":0,"a":1}
// After processing character 'n' at index 2: {"b":0,"a":1,"n":2}
// Before processing character 'a' at index 3: {"b":0,"a":1,"n":2}
// After processing character 'a' at index 3: {"b":0,"a":3,"n":2}
// Before processing character 'n' at index 4: {"b":0,"a":3,"n":2}
// After processing character 'n' at index 4: {"b":0,"a":3,"n":4}
// Before processing character 'a' at index 5: {"b":0,"a":3,"n":4}
// After processing character 'a' at index 5: {"b":0,"a":5,"n":4}
// [ -1, -1, -1, 2, 2, 2 ]
ex) s = "banana" μΆλ ₯ : [ -1, -1, -1, 2, 2, 2 ]
hash κ°μ²΄λ κ° λ¬Έμκ° λ§μ§λ§μΌλ‘ λ±μ₯ν μΈλ±μ€λ₯Ό μ μ₯νλ€.
[...s]λ λ¬Έμμ΄ "banana"λ₯Ό λ°°μ΄λ‘ λ³ννλ κ²μ΄κ³ , map ν¨μλ λ¬Έμμ΄μ κ° λ¬Έμμ λν΄ μ€νλλ€.
- hash[v] !== undefined : λ¬Έμκ° μ΄μ μ λ±μ₯ν μ μ΄ μλ κ²½μ°, result = i - hash[v]
- hash[v] === undefined : λ¬Έμκ° μ²μ λ±μ₯νλ κ²½μ° , result = -1
[ μΆκ° νμ΅ ]
- λ¬Έμμ΄μ λ¬Έμλ°°μ΄λ‘ λ°κΎΈλ λ°©λ²
let s = "banana";
console.log([...s]); // [ 'b', 'a', 'n', 'a', 'n', 'a' ]
- undefined vs null
1. undefined : λ³μκ° μ μΈλμμ§λ§ κ°μ΄ ν λΉλμ§ μμ μν
let x;
console.log(x); // undefined
function foo(y) {
console.log(y);
}
foo(); // undefined
2. null : λ³μμ λͺ μμ μΌλ‘ "κ°μ΄ μμ"μ λνλ΄λ νΉλ³ν κ°
let obj = null;
console.log(obj); // null
- κ°μ²΄ : ν€ - κ° μμΌλ‘ ꡬμ±λ λ°μ΄ν° ꡬ쑰 ( '{}'λ‘ μ μ )
let person = {
name: "Alice",
age: 30,
profession: "Engineer"
};
console.log(person);
// μΆλ ₯: { name: 'Alice', age: 30, profession: 'Engineer' }
// κ°μ²΄ μ κ·ΌνκΈ°
// μ νκΈ°λ²
console.log(person.name); // "Alice"
person.age = 31;
console.log(person.age); // 31
// λκ΄νΈ νκΈ°λ²
console.log(person["profession"]); // "Engineer"
person["country"] = "USA";
console.log(person["country"]); // "USA"
'μκ³ λ¦¬μ¦ > νλ‘κ·Έλλ¨Έμ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Lv.1 μ΄μν λ¬Έμ λ§λ€κΈ° (0) | 2024.07.21 |
---|---|
Lv.1 μ΅λ곡μ½μμ μ΅μ곡배μ (2) | 2024.07.14 |
Lv.1 ν¬κΈ°κ° μμ λΆλΆ λ¬Έμμ΄ (0) | 2024.07.10 |
Lv.1 xλ§νΌ κ°κ²©μ΄ μλ nκ°μ μ«μ (0) | 2024.07.03 |
Lv.1 μ μ λ΄λ¦Όμ°¨μμΌλ‘ λ°°μΉνκΈ° (0) | 2024.07.02 |