惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
D
Docker
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
C
Check Point Blog
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
M
MIT News - Artificial intelligence
B
Blog RSS Feed
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
美团技术团队
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale

The Tracks of mulder21c

Atomic Design + Storybook 적용 후기 json-server에 사용자 인증 구현하기 개발환경 WSL2 + zsh로 갈아타기 pass function as props in vue 2020년 회고 colum flexbox에서 padding bottom 문제 해결 Nuxt를 통해 보는 프론트엔드 개발자가 하는 일 Nuxt Router kebab-case 처리 JS to SCSS 변환 Nuxt + Storybook 통합 하기 2020 이직 이야기 2020 이직 이야기 2020 이직 이야기 2020 이직 이야기 Windows에서 PM2 실행 오류 해결 오픈톡 정지에 대한 카카오톡 고객센터 후기 배려에 대한 단상 학습이 잘 되지 않는 이유 웹팩 4 마이그레이션 삽질기 babel 7 업데이트 후 node_modules 패키지가 변환되지 않는다면? white space는 4px이다? 정말? 2020년 시간 관리를 위해서 도입 한 툴들 Codility Lesson 5 — PassingCars 2019년 회고 Codility Lesson 4 — MissingInteger Codility Lesson 4 — MaxCounters Codility Lesson 4 — FrogRiverOne Codility Lesson — PermCheck 착각은 자유가 아닌각 세미나 진행 후기 Codility Lesson 3 — PermMissingElm
Codility Lesson 3 — tapeEquilibrium
멀더끙 · 2019-09-19 · via The Tracks of mulder21c

authored by 멀더끙

Task description

|(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])| 최소값 찾기

How I did solve

  • 배열의 전체 합 sumOfTatal을 구함

  • A의 요소를 탐색해가며

    탐색한 요소들의 합을 구하면 A[0] + ... + A[P-1]

    탐색한 요소들의 합을 전체 합에서 빼면 A[P] + ... + A[N−1]

  • 둘의 차이 중 최소값을 반환

Solved Code

function solution(A) {
  const sumOfTotal = A.reduce( (acc, entry) => acc + entry, 0 )
  let  diff = Number.MAX_SAFE_INTEGER

  for(let P = -1, sumOfFirstPart = 0, sumOfSecondPart = sumOfTotal; ++P < A.length - 1 ; ) {
    sumOfFirstPart += A[P]
    sumOfSecondPart -= A[P]

    diff = Math.min(diff, Math.abs( sumOfFirstPart - sumOfSecondPart ) )
  }
  return diff;
}

Retrospective

  • 두 번만에 풀었다.

  • 첫 번째 풀 때에는 P를 증가시키면서 배열을 slice해서 각 배열의 합의 차이를 구하는 방식으로 했더니 시간 복잡도 때문에 timeout 되더라 하하...

  • codility가 코드를 제출해야만 평가를 해주기 때문에 불편한 감이 많다. 테스트 데이터를 미리 제공해주면 그래도 좀 제출 전에 검증을 해 볼 수 있을 텐데 말이다.