🌌 Day 11 Algorithm Review

JBΒ·2022λ…„ 3μ›” 28일
0

Algorithms

λͺ©λ‘ 보기
12/12

🌝 Class

πŸƒ μ •μˆ˜ 제곱근 νŒλ³„

function solution(n){
	return Number.isInteger(Math.sqrt(n))
  ? (Math.sqrt(n)+1) ** 2 : -1
}

solution(121)
solution(3)

== μ‚¬μš©λœ λ©”μ†Œλ“œ ==

// Math.pow(μ œκ³±ν•  수, μ–Όλ§ˆλ₯Ό μ œκ³±ν• κ±΄μ§€)
3 ** 2
Math.pow(3, 2)	// 9

// 제곱근 μ°ΎλŠ” λ©”μ†Œλ“œ
Math.sqrt(4) // 2	
Math.sqrt(3) // 1.73205080...  => μ†Œμˆ˜μ μ΄ μ‘΄μž¬ν•˜λ©΄ 제곱근이 μ—†λŠ”κ²ƒ

// μ •μˆ˜μΈμ§€ νŒλ³„ ν•΄μ£ΌλŠ” λ©”μ†Œλ“œ
Number.isInteger(4.1) // false

πŸƒ 제일 μž‘μ€ 수 μ œκ±°ν•˜κΈ°

function solution (arr){

    const min = Math.min(...arr)
    
    const answer = arr.filter(num => {
      console.log(num)	// 4  3  2  1   // 10
      return num > min
    })
  
    return answer.length === 0 ? [-1] : answer
  }
  
  solution([4,3,2,1])	// [4,3,2]
  solution([10])			// [-1]
  

== μ‚¬μš©λœ λ©”μ†Œλ“œ ==

// λ°°μ—΄μ˜ μ΅œμ†Œκ°’ κ΅¬ν•˜λŠ” λ©”μ†Œλ“œ
arr = [1,2,3,0]
Math.min(...arr)  // spread μ—°μ‚°μž μ‚¬μš©ν•΄μ£ΌκΈ°  =>  0

profile
두비두λ°₯λ°₯

0개의 λŒ“κΈ€