Git Product home page Git Product logo

daily-datastructure-algorithm's People

Watchers

 avatar  avatar

Forkers

luorixiangyang

daily-datastructure-algorithm's Issues

Note: 2์ฐจ์› ๋ฐฐ์—ด 90๋„ ํšŒ์ „

def rotate1(arr):
    return list(zip(*arr[::-1]))
# ์ฐธ๊ณ : https://stackoverflow.com/questions/8421337/rotating-a-two-dimensional-array-in-python

def rotate2(arr):
  n = len(arr) # ํ–‰ ๊ธธ์ด ๊ณ„์‚ฐ
  m = len(arr[0]) # ์—ด ๊ธธ์ด ๊ณ„์‚ฐ
  result = [[0] * n for _ in range(m)] # ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ
  for i in range(n):
    for j in range(m):
      result[j][n - i - 1] = arr[i][j]
  return result
  • zip(*iterable): ๋™์ผํ•œ ๊ฐœ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ž๋ฃŒํ˜•์„ ๋ฌถ์–ด ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜
>>> list(zip([1, 2, 3], [4, 5, 6]))
[(1, 4), (2, 5), (3, 6)] # ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ™์€ ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ผ๋ฆฌ ๋ฌถ์ž„
>>> list(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> list(zip("abc", "def"))
[('a', 'd'), ('b', 'e'), ('c', 'f')]
  • * : Unpacking Argument Lists. ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ argument๋กœ ๋„˜๊ฒจ์ค€๋‹ค

Note: ์ˆœ์—ด & ์กฐํ•ฉ

function getAllSubsets(array) {
    const subsets = [[]];
    
    for (const el of array) {
        const last = subsets.length-1;
        for (let i = 0; i <= last; i++) { // ๊ธฐ์กด subset ๋ฐฐ์—ด์— ์ž์‹ ์„ ์ถ”๊ฐ€ํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋งŒ๋“ค์–ด push
            subsets.push( [...subsets[i], el] ); 
        }
    }
    
    return subsets;
}

Here is an example for [1, 2, 3]

  • Start with an empty subset: []

  • Create new subsets by adding "1" to each existing subset. It will be:[] [1]

  • Create new subsets by adding "2" to each existing subset. It will be:[], [1] [2], [1, 2]

  • Create new subsets by adding "3" to each existing subset. It will be: [], [1], [2], [1, 2] [3], [1, 3], [2, 3], [1, 2, 3]

์ฐธ๊ณ  : https://stackoverflow.com/questions/42773836/how-to-find-all-subsets-of-a-set-in-javascript

Note: Longest Increasing Subsequence

๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด ๊ตฌํ•˜๊ธฐ - ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ

  • D[i] = arr[i]๋ฅผ ๋งˆ์ง€๋ง‰ ์›์†Œ๋กœ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด
  • DP ํ…Œ์ด๋ธ”์˜ ๊ฐ’์€ ๋ชจ๋‘ 1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • ๋ชจ๋“  0 <= j < i ์— ๋Œ€ํ•˜์—ฌ, D[i] = max(D[i], D[j] + 1) if arr[j] < arr[i]

Note: ์†Œ์ˆ˜ ํŒ๋ณ„

์†Œ์ˆ˜: 2๋ณด๋‹ค ํฐ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ 1๊ณผ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•œ ์ž์—ฐ์ˆ˜๋กœ๋Š” ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š” ์ˆ˜

# ์†Œ์ˆ˜ ํŒ๋ณ„ ํ•จ์ˆ˜
def is_prime(x):
  # 2๋ถ€ํ„ฐ x์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜๋ฅผ ํ™•์ธํ•˜์—ฌ
  for i in range(2,  int(math.sqrt(x)) + 1):
    # x๊ฐ€ ํ•ด๋‹น ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง„๋‹ค๋ฉด
     if x % i == 0:
          return False # ์†Œ์ˆ˜๊ฐ€ ์•„๋‹˜
  return True

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด

  1. 2๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ชจ๋“  ์ž์—ฐ์ˆ˜ ๋‚˜์—ด
  2. ๋‚จ์€ ์ˆ˜ ์ค‘์—์„œ ์•„์ง ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜ i ์ฐพ๊ธฐ
  3. ๋‚จ์€ ์ˆ˜ ์ค‘์—์„œ i์˜ ๋ฐฐ์ˆ˜ ๋ชจ๋‘ ์ œ๊ฑฐ
  4. ๋” ์ด์ƒ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ 2, 3๋ฒˆ ๊ณผ์ • ๋ฐ˜๋ณต
// ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด
  const isPrime = new Array(maxNum).fill(true);
  isPrime[0] = isPrime[1] = false;
  for (let i = 2; i <= Math.sqrt(maxNum); i++) {
    if (isPrime[i]) {
      let j = 2;
      while (i * j <= maxNum) {
        isPrime[i * j++] = false;
      }
    }
  }

python ๋ฌธ๋ฒ• : mutable๊ณผ immutable์˜ ์ฐจ์ด

๋‘ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ํ•œ๊บผ๋ฒˆ์— ๋Œ€์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ™์€ ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋”๋ผ๋„ a, b = -1, -1 ๋ฐฉ์‹์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ฐ–์— ๋ชฐ๋ž๋Š”๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ a=b=1๋„ ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ด ๊ธ€์„ ํ†ตํ•ด ๋ฐœ๊ฒฌ ! ๊ทธ๋Ÿฌ๋‚˜ ์ด์™€ ๊ฐ™์ด ๋Œ€์ž…ํ•  ๋•Œ์— a = b = []์™€ ๊ฐ™์ด mutable ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์ฃผ์˜ํ•˜๋ผ๊ณ  ํ•œ๋‹ค. mutable vs immutable์— ๋Œ€ํ•ด ๋Œ€์ถฉ ๋ ˆํผ๋Ÿฐ์Šค ์ฐจ์ด?๋ผ๊ณ ๋งŒ ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋Š”๋ฐ ๋” ์ž์„ธํžˆ ์•Œ์•„๋‘ฌ์•ผ ํ•˜๋Š” ๊ฐœ๋…์ธ ๊ฒƒ ๊ฐ™์•„ ์ดํ›„ ๊ณต๋ถ€ํ•ด๋ณด๊ธฐ๋กœ !

Note: bisect ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

bisect

bisect_left(a, x): ์ •๋ ฌ๋œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ a์— ๋ฐ์ดํ„ฐ x๋ฅผ ์‚ฝ์ž…ํ•  ๊ฐ€์žฅ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ
bisect_right(a, x): ์ •๋ ฌ๋œ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ a์— ๋ฐ์ดํ„ฐ x๋ฅผ ์‚ฝ์ž…ํ•  ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ

ํŠน์ • ๋ฒ”์œ„์— ์†ํ•˜๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜

def count_by_range(a, left_value, right_value):
    right_index = bisect_right(a, right_value)
    left_index = bisect_left(a, left_value)
    return right_index - left_index

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.