반응형
(Original)
Good morning! Here's your coding interview problem for today.
This problem was recently asked by Google.
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
(Google번역)
숫자 목록과 숫자 k가 주어지면 목록에서 두 숫자의 합이 k가 되는지 여부를 반환합니다.
예를 들어 [10, 15, 3, 7] 및 17의 k가 주어지면 10 + 7은 17이므로 true를 반환합니다.
보너스: 이 작업을 한 번에 수행할 수 있습니까?
매일 문제을 받고 싶다면 아래 링크에서 구독해보세요.
(단, 문제 풀이는 프리미엄 구독자만 받을 수 있음)
https://www.dailycodingproblem.com/
// example
const array = [10, 15, 3, 7]
const k = 17
let result;
let i = 0;
let j = i + 1;
for (; i < array.length; i++) {
for (; j < array.length; j++) {
if (array[i] + array[j] === k) {
result = true;
break;
}
}
if (result) break;
}
console.log(result);
숫자 배열안에 N개의 숫자가 있을 때,
for 2번 사용하여 최대 N * (N-1) 으로 O(N2) 복잡도를 생각할 수 있다.
그런데 한 번에 수행한다는 의미가... O(N) or O(1) 이 가능하다는 말인지..
'2023' 카테고리의 다른 글
sql,nosql,앱개발,정처기,,,230616 (0) | 2023.06.18 |
---|---|
2023.01.16. Daily Coding Problem: Problem #3 [Medium] (0) | 2023.01.16 |
2023.01.13.vue build (0) | 2023.01.13 |
2023.01.12. (0) | 2023.01.12 |
2023.01.11.vue (0) | 2023.01.11 |