(DataStructure & Algorithm) problems (2)

์ž„๊ฒฝ๋ฏผยท2023๋…„ 10์›” 18์ผ
1
post-thumbnail

๐Ÿ”‘Summarization


  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ง„ํ–‰

๐Ÿ“—Contents


๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์„ ํ˜•๊ฒ€์ƒ‰ : ์„ ํ˜•์œผ๋กœ ๋‚˜์—ด๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์Šค์บ”ํ•˜๋ฉด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์Œ
    • ๋ณด์ดˆ๋ฒ• : ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ์ฐพ์œผ๋ ค๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์„œ ์ฐพ๋Š” ๊ณผ์ •์„ ๊ฐ„๋žตํ™”
  • ์ด์ง„๊ฒ€์ƒ‰
    • ๊ฐ€์šด๋ฐ ๋ฐ์ดํ„ฐ(์ค‘์•™๊ฐ’) ์ด์šฉ
      โ€ป ์ •๋ ฌ์ด ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋ผ ๊ฐ€์ •ํ•จ
    • ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ์ค‘์•™๊ฐ’๊ณผ์˜ ํฌ๊ณ  ์ž‘์Œ์„ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
    • ๊ฒ€์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ขํ˜€๋‚˜๊ฐ€๋ฉด์„œ ์›ํ•˜๋Š” ๊ฐ’์„ ์ฐพ์Œ

์ˆœ์œ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์ˆœ์œ„ : ์ˆ˜์˜ ํฌ๊ณ  ์ž‘์Œ์„ ์ด์šฉํ•ด์„œ ์ˆ˜์˜ ์ˆœ์„œ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ


์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๋ฒ„๋ธ”์ •๋ ฌ : ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ธ์ ‘ํ•˜๋Š” ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ˆœ์ฐจ์ ์œผ๋กœ ๋น„๊ตํ•˜๋ฉด์„œ ํฐ ์ˆซ์ž๋ฅผ ๊ฐ€์žฅ ๋์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์‚ฝ์ž…์ •๋ ฌ : ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ์ž๋ฃŒ ๋ฐฐ์—ด๊ณผ ๋น„๊ตํ•ด์„œ, ์ •๋ ฌ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.
  • ์„ ํƒ์ •๋ ฌ : - ์ฃผ์–ด์ง„ ๋ฆฌ์ŠคํŠธ ์ค‘์— ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„ ๊ทธ ๊ฐ’์„ ๋งจ ์•ž์— ์œ„์น˜ํ•œ ๊ฐ’๊ณผ ๊ต์ฒดํ•˜๋Š” ๋ฐฉ์‹. ์ž๋ฃŒ๋ฅผ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜


์ˆ˜ํ•™ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์ตœ๋Œ“๊ฐ’(Max) : ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค
  • ์ตœ์†Ÿ๊ฐ’(Min) : ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค
  • ์ตœ๋นˆ๊ฐ’(Mid) : ๋ฐ์ดํ„ฐ์—์„œ ๋นˆ๋„์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ๋ฐ์ดํ„ฐ
  • ๊ทผ์‚ฟ๊ฐ’(Approximation) : ํŠน์ • ๊ฐ’(์ฐธ ๊ฐ’)์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ฐ’
  • ํ‰๊ท (Mean) : ์—ฌ๋Ÿฌ ์ˆ˜๋‚˜ ์–‘์˜ ์ค‘๊ฐ„๊ฐ’์„ ๊ฐ–๋Š” ์ˆ˜
  • ์žฌ๊ท€(Recursive) : ๋‚˜ ์ž์‹ ์„ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ

์˜ˆ์ œ๋ฌธ์ œํ’€์ด

  • ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ชจ๋“ˆ์„ ๋‹ค์Œ ์š”๊ฑด์— ๋”ฐ๋ผ ๋งŒ๋“ค์–ด ๋ณด์ž.
def searchNumberByLineAlgorithm(ns, sn):

    searchResultIdx = -1

    print(f'Numbers : {ns}')
    print(f'Search Numbers : {sn}')

    n = 0
    while True:

        if n == len(ns):
            print('search FAIL')
            break
        if ns[n] == sn:
            searchResultIdx = n
            print('search SUCCESS')
            print(f'search result INDEX : {searchResultIdx}')

        n += 1

    return searchResultIdx
  • ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ˆซ์ž๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ชจ๋“ˆ์„ ๋‹ค์Œ ์š”๊ฑด์— ๋”ฐ๋ผ ๋งŒ๋“ค์–ด๋ณด์ž.
def searchNumberByBinaryAlgorithm(ns, sn):

    searchResultIdx = -1

    startIdx = 0
    endIdx = len(ns) - 1
    midIdx = (startIdx + endIdx) // 2
    midValue = ns[midIdx]

    print('start Idx : {}, endIdx : {}' .format(startIdx, endIdx))
    print('mid Idx : {}, midvalue : {}'.format(midIdx, midValue))

    while sn >= ns[0] and sn <= ns[len(ns) - 1]:

        if sn == ns[len(ns) - 1]:
            searchResultIdx = len(ns) - 1
            break

        if startIdx + 1 == endIdx:
            if ns[startIdx] != sn and ns[endIdx] != sn:
                break

        if sn > midValue:
            startIdx = midIdx
            midIdx = (startIdx + endIdx) // 2
            midValue = ns[midIdx]
            print('start Idx : {}, endIdx : {}'.format(startIdx, endIdx))
            print('mid Idx : {}, midvalue : {}'.format(midIdx, midValue))

        elif sn < midValue:
            endIdx = midIdx
            midIdx = (startIdx + endIdx) // 2
            midValue = ns[midIdx]
            print('start Idx : {}, endIdx : {}'.format(startIdx, endIdx))
            print('mid Idx : {}, midvalue : {}'.format(midIdx, midValue))

        elif sn == midValue:
            searchResultIdx = midIdx
            break

    return searchResultIdx
  • ์ตœ์†Ÿ๊ฐ’ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฆฌ์ŠคํŠธ์—์„œ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด๋ณด์ž.
    (๋ฆฌ์ŠคํŠธ๋Š” 1๋ถ€ํ„ฐ 50๊นŒ์ง€์˜ ๋‚œ์ˆ˜ 30๊ฐœ๋ฅผ ์ด์šฉํ•˜๋˜, ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๋„๋ก ํ•œ๋‹ค
class MinAlgorithm:

    def __init__(self, ns):
        self.nums = ns
        self.minNum = 0
        self.minNumCnt = 0

    def setMinNum(self):
        self.minNum = 50

        for n in self.nums:
            if self.minNum > n:
                self.minNum = n

    def getMinNum(self):
        self.setMinNum()
        return self.minNum

    def setMinNumCnt(self):
        self.setMinNum()

        for n in self.nums:
            if self.minNum == n:
                self.minNumCnt += 1

    def getMinNumCnt(self):
        self.setMinNumCnt()
        return self.minNumCnt
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ •์ˆ˜ ๋‘๊ฐœ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž‘์€ ์ •์ˆ˜์™€ ํฐ ์ •์ˆ˜ ์‚ฌ์ด์˜ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์–ด๋ณด์ž.

class NumsSum:

    def __init__(self, n1, n2):
        self.bigNum = 0
        self.smallNum = 0
        self.setN1N2(n1, n2)

    def setN1N2(self, n1, n2):
        self.bigNum = n1
        self.smallNum = n2

        if n1 < n2:
            self.bigNum = n2
            self.smallNum = n1

    def addNum(self, n):
        if n <= 1:
            return n
        return n + self.addNum(n - 1)

    def sumBetweenNums(self):
        return self.addNum(self.bigNum - 1) - self.addNum(self.smallNum)

0๊ฐœ์˜ ๋Œ“๊ธ€