KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (502) N
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)
    • Docker (14) N
    • CS (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • string
  • C
  • recursion
  • Level 1
  • tree
  • 백준
  • 수학
  • 다이나믹 프로그래밍
  • Level1
  • programmers
  • codeup
  • LeetCode
  • js
  • Level 2
  • Level 0
  • 정렬
  • C++
  • 문자열
  • 제코베 JS 100제
  • 그래프

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Array / 2090번 / K Radius Subarray Averages / JS

2023. 6. 21. 19:24

< 문제 바로가기 >

 

K Radius Subarray Averages - LeetCode

Can you solve this real interview question? K Radius Subarray Averages - You are given a 0-indexed array nums of n integers, and an integer k. The k-radius average for a subarray of nums centered at some index i with the radius k is the average of all elem

leetcode.com

 

< 문제 간단설명 >

어떤 원이 k만큼의 반지름을 가질 때, 인덱스 하나마다 해당하는 요소를 중심으로 가질 때, 그 안에 들어오는 배열의 요소들의 평균을 구해서 반환하면 된다.

 

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var getAverages = function(nums, k) {
    let result = Array.from({ length: nums.length }, () => -1);

    if(nums.length < k) {
      return result;
    }

    for(let i=k; i<nums.length - k; i+=1) {
      let sum = 0;
      for(let j=i-k; j<=i+k; j+=1) {
        sum += nums[j];
      }
      // 반지름이 k이기 때문에 총 길이는 중심까지 합해서 k * 2 + 1
      result[i] = Math.floor(sum / (k * 2 + 1));
    }

    return result;
};

 

저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Array / 661번 / Image Smoother / JS  (0) 2023.12.19
LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS  (0) 2023.12.13
LeetCode / Array / 1732번 / Find the Highest Altitude / JS  (0) 2023.06.21
LeetCode / Tree / 1161번 / Maximum Level Sum of a Binary Tree / JS  (0) 2023.06.17
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Array / 661번 / Image Smoother / JS
    • LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS
    • LeetCode / Array / 1732번 / Find the Highest Altitude / JS
    • LeetCode / Tree / 1161번 / Maximum Level Sum of a Binary Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바