본문 바로가기
Research/Coding Test

HackerRank_Apple and Orange

by RIEM 2023. 4. 12.
728x90

Problem

https://www.hackerrank.com/challenges/apple-and-orange/problem?isFullScreen=true

Complexity

Time complexity: O(n)
Space complexity: O(n)

Code

function countApplesAndOranges(s, t, a, b, apples, oranges) {
    // console.log('house range')
    // console.log(s)
    // console.log(t)
    // console.log('Apple tree')
    // console.log(a)
    // console.log('Orange tree')
    // console.log(b)
    // console.log('apples')
    // console.log(apples)
    // console.log('oranges')
    // console.log(oranges)
    
    const applesRelativeDistance = apples.map((el) => el + a);
    const filteredApples = applesRelativeDistance.filter((el) => {
        if (el >= s && el <= t) return true
        else return false
    }
    );
    const orangesRelativeDistance = oranges.map((el) => el + b);
    const filteredOranges = orangesRelativeDistance.filter((el) => {
       if (el >= s && el <= t) return true
        else return false}
        )
    // console.log('filtered')
    console.log(filteredApples.length)
    console.log(filteredOranges.length)
}
728x90

'Research > Coding Test' 카테고리의 다른 글

해시_완주하지 못한 선수  (0) 2023.11.16
Leetcode_bubbleSort_Height Checker  (0) 2023.04.19
Leetcode_121. Best Time to Buy and Sell Stock  (0) 2023.04.06
HackerRank_Grading Students  (0) 2023.04.05
HackerRank_Birthday Cake Candles  (0) 2023.04.05

댓글