πŸ“
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • μ½œλ°±ν•¨μˆ˜ (Callback function)
    • ν•¨μˆ˜μ˜ 호좜
    • 자료ꡬ쑰 new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • 객체
    • Arguments
    • higher order function
    • operators-function-control-flow
    • 객체 생성 νŒ¨ν„΄ 3κ°€μ§€
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • 데이터 νƒ€μž…
    • Object 객체
    • ν‘œμ€€ λ‚΄μž₯ 객체의 ν™•μž₯
    • μ°Έμ‘°
    • ν•¨μˆ˜
    • 상속(Inheritance)
    • this - 'this'λ₯Ό μ‚¬μš©ν•˜λŠ” ν•΄λ‹Ή ν•¨μˆ˜λ₯Ό 'μ–΄λ–»κ²Œ' μ‹€ν–‰ν•˜λŠλƒμ— λ”°λ₯Έ 4κ°€μ§€ this μ •μ˜
    • 전역객체(Global object)
    • 객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ°
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - μžμ—°μˆ˜, μ •μˆ˜, 10μ§„μˆ˜, 2μ§„μˆ˜, λΆ€λ™μ†Œμˆ˜μ , μ‹€μˆ˜
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • ν•¨μˆ˜ν˜• μ»΄ν¬λ„ŒνŠΈμ™€ 클래슀, μ–΄λ–€ 차이가 μ‘΄μž¬ν• κΉŒ?
    • Hooksκ³Ό useEffect μ‚¬μš©ν•΄ 보기
    • Route
    • async wait μ‚¬μš©ν•˜κΈ°
    • Hooks API Reference
    • context
    • npm uninstall ν•˜λŠ”λ²•
    • test λ§Œλ“€κΈ°
  • tip
    • ν΄λ¦°μ½”λ“œ
    • BxSlider둜 ν…μŠ€νŠΈ 흐λ₯΄λŠ” 효과 λ§Œλ“€κΈ°
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ 2λΆ€
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • μš°μ•„ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • λ¨Έμ§€ ν›„ branch μ‚­μ œν•˜κΈ°
    • μ§€μ˜₯μ—μ„œ 온 Git
    • reset-revert
    • develop branch λ₯Ό pull ν•˜κ³  μ‹Άμ„λ•Œ
  • conference-and-seminar
    • λͺ¨λ˜ ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œν™˜κ²½μ˜ 이해
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • μ•Œκ³ λ¦¬μ¦˜ μ½”λ“œλ¦¬λ·°
    • 슀슀둜 κ³΅λΆ€ν•˜λŠ” 법
    • 바닐라코딩 μˆ˜κ°• ν›„κΈ°
    • async 과제 ν›„κΈ° - 비동기, 동기, ν΄λ‘œμ €, λ°°μ—΄κ³Ό 객체
    • μœ μš©ν•œ μ‚¬μ΄νŠΈ
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars 이용자 μ†”λ£¨μ…˜ λͺ¨μŒ
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • νƒ€μž…κ³Ό 좔상화
      • 객체 지도
      • μ΄μƒν•œ λ‚˜λΌμ˜ 객체
      • 좔상화 기법
      • 05. μ±…μž„κ³Ό λ©”μ‹œμ§€
      • 07.ν•¨κ»˜ λͺ¨μœΌκΈ°
      • 04. μ—­ν• , μ±…μž„, ν˜‘λ ₯
      • ν˜‘λ ₯ν•˜λŠ” κ°μ²΄λ“€μ˜ 곡동체
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • μ›Ή λΈŒλΌμš°μ €μ— URL을 μž…λ ₯ν–ˆμ„ λ•Œ μ–΄λ–»κ²Œ μ›Ή νŽ˜μ΄μ§€κ°€ λ³΄μ—¬μ§ˆκΉŒ?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex 해버렸지 뭐야
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd λͺ…λ Ήμ–΄ λͺ¨μŒ
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN μ„€μ •ν•˜κΈ°
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans μ˜€λ‹΅λ…ΈνŠΈ
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 2/2
    • μ½”λ“œ μŠ€ν”ΌμΈ  - CSS Rendering 1회차 1/2
  • you-dont-know-js
    • νƒ€μž…
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • 문제
  • 문제 이해
  • ν•΄κ²° 방법
  • μ½”λ“œ κ΅¬ν˜„
  • κ²°κ³Ό 뢄석

Was this helpful?

  1. algorithm

find key

μ»΅ 돌리기 κ²Œμž„

문제

μ„Έ 개의 λ’€μ§‘νžŒ μ»΅ 쀑 ν•œ 개의 μ»΅ μ•ˆμ— μ—΄μ‡ κ°€ μžˆμŠ΅λ‹ˆλ‹€. 당신이 μ—΄μ‡ λ₯Ό μ°ΎκΈ° μœ„ν•΄ 컡을 λ“€μ–΄μ˜¬λ¦¬λ €λŠ” μˆœκ°„, Drogon이 λΉ λ₯΄κ²Œ 컡의 μœ„μΉ˜λ₯Ό λ’€μ„žκΈ° μ‹œμž‘ν•©λ‹ˆλ‹€. 컡의 κ΅ν™˜μ΄ 끝났을 λ•Œ, μ—΄μ‡ κ°€ λ“€μ–΄μžˆλŠ” 컡을 μ°Ύμ•„μ•Ό ν•©λ‹ˆλ‹€.

  • 컡의 μœ„μΉ˜λŠ” 인덱슀둜 ν‘œν˜„λ©λ‹ˆλ‹€. (0λΆ€ν„° μ‹œμž‘)

  • ν‚€κ°€ λ“€μ–΄μžˆλŠ” 컡의 μΈλ±μŠ€μ™€ κ΅ν™˜λœ 컡의 인덱슀λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ°°μ—΄(swaps)을 μž…λ ₯으둜 λ°›μŠ΅λ‹ˆλ‹€.

예λ₯Όλ“€μ–΄, μ—΄μ‡ κ°€ λ“€μ–΄μžˆλŠ” 컡의 처음 μœ„μΉ˜κ°€ 0이고 컡이 κ΅ν™˜λ˜λŠ” μˆœμ„œκ°€ λ‹€μŒκ³Ό κ°™λ‹€λ©΄ [(0, 1), (1, 2), (1, 0)]

  • 첫 κ΅ν™˜λ•Œ μ—΄μ‡ κ°€ μžˆλŠ” 컡은 0 μ—μ„œ 1둜 μ΄λ™ν•˜κ²Œ λ©λ‹ˆλ‹€.

  • λ‘λ²ˆμ§Έ κ΅ν™˜λ•Œ μ—΄μ‡ κ°€ μžˆλŠ” 컡은 1 μ—μ„œ 2둜 μ΄λ™ν•˜κ²Œ λ©λ‹ˆλ‹€.

  • λ§ˆμ§€λ§‰ κ΅ν™˜λ•Œ 1에 μžˆλŠ” 컡이 0으둜 κ°€μ§€λ§Œ, μ—΄μ‡ κ°€ μžˆλŠ” μ»΅μ—λŠ” 영ν–₯을 λ―ΈμΉ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

  • λ”°λΌμ„œ μ—΄μ‡ κ°€ μžˆλŠ” 컡의 μœ„μΉ˜λŠ” 2κ°€ λ©λ‹ˆλ‹€.

swaps = [[0, 1], [1, 2], [1, 0]] firstPosition = 0 findKey(firstPosition, swaps) == 2

컡의 κ°―μˆ˜λŠ” μ΅œμ†Œν•œ 두 개 μ΄μƒμž…λ‹ˆλ‹€.

/*
* @param {number} start
* @param {number[]} swaps
* @return {number}
*/

findKey (start, swaps){
    //code
}

문제 이해

첫번째 μΈμžλŠ” μ—΄μ‡ μ˜ μœ„μΉ˜μ΄λ‹€. λ‘λ²ˆμ§Έ 인자둜 λ“€μ–΄μ˜¨ λ°°μ—΄μ•ˆμ˜ λ°°μ—΄μ—μ„œ μ—΄μ‡ μ˜ μœ„μΉ˜λ₯Ό μ°Ύκ³  -> λ°”λ€μœ„μΉ˜λ₯Ό κΈ°μ–΅ -> μ—΄μ‡  좔적 -> 바뀐 μœ„μΉ˜κ°€ μžˆμ„ μ‹œ κΈ°μ–΅ ->.. λ‘λ²ˆμ§Έ μΈμžκ°€ 끝날 λ•Œ κΉŒμ§€ μΆ”μ ν•œλ‹€!

ν•΄κ²° 방법

  1. start 인자의 값을 κ°€μ§„ 배열을 μ°ΎλŠ”λ‹€.

  2. 같은 숫자인 μΈλ±μŠ€κ°€ 0이면 인덱슀 1의 숫자λ₯Ό λ³€μˆ˜μ— λ‹΄λŠ”λ‹€. 인덱슀 1의 μˆ«μžμ™€ κ°™λ‹€λ©΄ 인덱슀2의 μˆ«μžμ— λ³€μˆ˜μ— λ‹΄λŠ”λ‹€.

  3. swaps의 배열을 총 λ‹€ λŒμ•˜λ‹€λ©΄ λ§ˆμ§€λ§‰μœΌλ‘œ 담은 숫자λ₯Ό return ν•΄μ€€λ‹€.

μ½”λ“œ κ΅¬ν˜„

function findKey (start, swaps){
  // Complete the findKey function.
  let position = start;
  const startIdx = 0;
  const endIdx = 1;

  swaps.forEach(cup => {

    //첫번째 μœ„μΉ˜μ™€ λ‘λ²ˆμ§Έμœ„μΉ˜μ— μ΄λ™ν•œ μˆ«μžμ™€ μΌμΉ˜ν•˜λŠ”μ§€ νŒλ³„ 
    if (cup[startIdx] === position) {
      position = cup[endIdx];
    } else if (cup[endIdx] === position) {
      position = cup[startIdx];
    }

  });
  return position;
}

κ²°κ³Ό 뢄석

ν…ŒμŠ€νŠΈ 톡과

startIdx 와 endIdx λ³€μˆ˜λ₯Ό ꡳ이 λ§Œλ“€ ν•„μš” μ—†λ‹€λŠ” ν”Όλ“œλ°±μ„ λ°›μ•˜μŠ΅λ‹ˆλ‹€.

:white_check_mark: ν•˜λ“œμ½”λ”©μ„ μ•ˆν•˜κΈ° μœ„ν•΄ λ³€μˆ˜λ₯Ό λ‹΄κ²Œ λ˜μ—ˆμ§€λ§Œ 0κ³Ό 1μ΄λΌλŠ” λΆ„λͺ…ν•œ 값은 직접 μž…λ ₯ν•˜λŠ”κ²Œ 더 λ‚«λ‹€.

PreviousShortest WordNextTwo Sum

Last updated 5 years ago

Was this helpful?