KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • 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 (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Array / 661번 / Image Smoother / JS

2023. 12. 19. 17:55

< 문제 바로가기 >

 

Image Smoother - LeetCode

Can you solve this real interview question? Image Smoother - An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nin

leetcode.com

 

< 문제 간단설명 >

이미지에 3*3의 필터를 씌워서 부드럽게 만드는 문제이다. 예를 들어 (i, j)의 셀을 부드럽게 만드려면, (i, j)를 중심으로 3*3의 필터를 씌워서 필터안에 들어오는 모든 값에 대한 평균으로 만든다. 단, 필터의 범위가 기존의 이미지의 범위를 벗어났다면, 그 벗어난 범위는 무시하고 평균을 구하면 된다.

 

/**
 * @param {number[][]} img
 * @return {number[][]}
 */
const imageSmoother = (img) => {
  let width = img[0].length;
  let height = img.length;

  /**
   * 인자로 받은 행과 열에 해당하는 값을 부드럽게 변환한 값으로 반환하는 함수
   * 
   * @param {number} row 행
   * @param {number} col 열 
   * @returns 부드럽게 변환한 셀의 값을 반환한다.
   */
  const getSmoothCell = (row, col) => {
    let sum = 0;
    let divisor = 0;

    for (let i = row - 1; i <= row + 1; i++) {
      // 주위의 셀 중 행이 벗어난 셀은 처리하지 않는다.
      if (i < 0 || i >= height) {
        continue;
      }
      for (let j = col - 1; j <= col + 1; j++) {
        // 주위의 셀 중 열이 벗어난 셀은 처리하지 않는다.
        if (j < 0 || j >= width) {
          continue;
        }
        sum += img[i][j];
        // 범위내의 셀의 개수로만 나눈다.
        divisor++;
      }
    }
    const smoothCell = Math.floor(sum / divisor);

    return smoothCell;
  };

  /**
   * 부드럽게 변환한 이미지를 반환하는 함수
   * 
   * @returns {number[][]} 부드럽게 변환한 이미지를 반환한다.
   */
  const getSmoothImg = () => {
    const smoothImg = Array.from({ length: height }, () =>
      Array.from({ length: width })
    );

    for (let i = 0; i < height; i++) {
      for (let j = 0; j < width; j++) {
        smoothImg[i][j] = getSmoothCell(i, j);
      }
    }

    return smoothImg;
  };

  const smoothImg = getSmoothImg();

  return smoothImg;
};
저작자표시

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

LeetCode / Array / 1442번 / Count Triplets That Can Form Two Arrays of Equal XOR / JS  (0) 2024.05.30
LeetCode / Binary Search / 1608번 / Special Array With X Elements Greater Than or Equal X / JS  (1) 2024.05.28
LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS  (0) 2023.12.13
LeetCode / Array / 2090번 / K Radius Subarray Averages / JS  (0) 2023.06.21
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Array / 1442번 / Count Triplets That Can Form Two Arrays of Equal XOR / JS
    • LeetCode / Binary Search / 1608번 / Special Array With X Elements Greater Than or Equal X / JS
    • LeetCode / Array / 1582번 / Special Positions in a Binary Matrix / JS
    • LeetCode / Array / 2090번 / K Radius Subarray Averages / JS
    KimMinJun
    KimMinJun

    티스토리툴바