본문 바로가기

Research/Coding Test24

Leetcode_bubbleSort_Height Checker Problem Complexity Time complexity: O(n^2) Code /** Height Checker - students stand in single file line in non-decreasing order - expected[i] -> i'th student - heights : current order array - return numbers of indices where heights[i] not equal to expected[i] */ const heights = [1,1,4,2,1,3] const expected = [1,1,1,2,3,4] var heightChecker = function(heights) { const expected = [...heights] // G.. 2023. 4. 19.
HackerRank_Apple and Orange 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') .. 2023. 4. 12.
Leetcode_121. Best Time to Buy and Sell Stock Problem https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/928840784/ Intuition 반복문으로 최소값과 최대값 차이를 변수에 저장하고 이 변수보다 큰 차이가 나올 때마다 지속 최신화시켜주다가 마지막에 이를 리턴하면 되지 않을까 Approach 이중 반복문, Max.max를 사용해보았지만 시간 복잡도 문제로 실패. min, max 변수로 접근하는건 동일하되, 더 작은 min 값이 발견될 때는 min값만 업데이트 해주고, min보다 큰 수가 나올 때는 현재값과 min의 차이를 비교한 수가 max(최대 차이) 보다 클 경우 업데이트해주는 방식으로 해결. Complexity Time complexity: O(n).. 2023. 4. 6.
HackerRank_Grading Students Problem https://www.hackerrank.com/challenges/grading/problem?isFullScreen=true Code function gradingStudents(grades) { const result = []; grades.forEach(el => { const nextMultiple = ((Math.floor(el/5))*5) + 5; console.log(`${el}의 multiple은 ${nextMultiple}`); if(el < 38) { result.push(el) } else if(Math.abs(nextMultiple - el) < 3) { result.push(nextMultiple) } else { result.push(el) } }) return .. 2023. 4. 5.