diff --git a/_posts/2024-02-26-jekyll-serve-error-level-undefined-method.md b/_posts/2024-02-26-jekyll-serve-error-level-undefined-method.md new file mode 100644 index 0000000..a91c4ed --- /dev/null +++ b/_posts/2024-02-26-jekyll-serve-error-level-undefined-method.md @@ -0,0 +1,46 @@ +--- +layout: post +title: "Jekyll 실행시 `level': undefined method `[]' for nil (NoMethodError) 에러 해결법" +categories: [블로그] +tags: [루비, ruby, bundle, jekyll, level, fiber, NoMethodError] +date: 2024-02-26 22:00:00 +0900 +image: + path: /assets/images/2024-02-26-jekyll-serve-error-level-undefined-method/error.png +--- + +최근에 맥북을 구매하면서 환경을 새로 구성하였다. + +mac 기본 버전인 2.6.x 에서 ruby 버전을 올리고 +평소대로 `bundle exec jekyll serve` 를 수행하려 했는데 아래와 같은 에러가 발생되었다. + +```rb +jekyll 4.3.2 | Error: undefined method `[]' for nil +/opt/homebrew/Cellar/ruby/3.3.0/lib/ruby/3.3.0/logger.rb:384:in `level': undefined method `[]' for nil (NoMethodError) + + @level_override[Fiber.current] || @level + ^^^^^^^^^^^^^^^ +``` + +- ruby 3.3.0 +- bundler 2.5.4 +- jekyll 4.3.2 + +인 상황에서 발생한 이슈이다. + +## 해결방법 + +jekyll 를 4.3.3 으로 올리면 해결된다. + +`Gemfile` 에서 `jekyll` 에 대한 버전 명시를 해준 뒤 + +```rb +... +gem 'jekyll', "= 4.3.3" +... +``` + +`bundle install` 을 진행하여 `Gemfile.lock` 파일내 버전 정보를 업데이트 한 후 `Gemfile` 에서는 다시 버전 명시를 지워주었다. + +## 참고 + +[https://talk.jekyllrb.com/t/error-when-executing-bundle-install/8822](https://talk.jekyllrb.com/t/error-when-executing-bundle-install/8822) \ No newline at end of file diff --git a/_posts/2024-02-26-leetcode-322.md b/_posts/2024-02-26-leetcode-322.md new file mode 100644 index 0000000..27a6340 --- /dev/null +++ b/_posts/2024-02-26-leetcode-322.md @@ -0,0 +1,133 @@ +--- +layout: post +title: (Leetcode) 322 - Coin Change +categories: [스터디-알고리즘] +tags: [파이썬, 알고리즘, python, algorithm, Leetcode, DP, coin] +date: 2024-02-26 22:00:00 +0900 +image: + path: /assets/images/2024-02-26-leetcode-322/page1.png +--- + +[New Year Gift - Curated List of Top 75 LeetCode Questions to Save Your Time](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-75-LeetCode-Questions-to-Save-Your-Time-OaM1orEU) + +위 링크에 있는 추천 문제들을 시간이 있을때마다 풀어보려고 한다. + +--- + +[https://leetcode.com/problems/coin-change/description/](https://leetcode.com/problems/coin-change/description/) + +DP 문제이다. + +## 내가 풀었던 방식 + +### 처음 생각 + +처음에는 큰 동전부터 넣어서 될 때까지 하면 베스트 일 것이라고 생각했다. 하지만 그렇게 간단하지는 않았다. + +예를들어 `[100, 30, 1]` 이라는 동전이 있다고 해보자. +이때 주어지는 amount는 120 이라고 하면 + +30으로는 4개면 되지만 100을 먼저 채우면 30은 들어갈 수 없고 1짜리 20개를 더 써서 21개의 동전이 사용된다. + +### 중간 생각 + +그래서 모든 경우의 수를 다 찾아봐야 겠다고 생각했다. +처음에는 잘 먹히는 듯 보였으나, 역시나 복잡한 경우로 인해 타임아웃이 발생되었다. + +```python +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + if amount == 0: + return 0 + + coins.sort() + coins.reverse() + + stack = [] + worst = math.floor(amount / coins[-1]) + min = worst + 1 + + def calc(index, amount): + nonlocal min + # print("start:", stack, len(stack), amount, index) + if len(stack) == worst: + return -1 if min > worst else min + + while index == len(coins): + last_removed_index = stack.pop() + amount += coins[last_removed_index] + index = last_removed_index + 1 + + while amount > 0: + stack.append(index) + amount -= coins[index] + + if amount == 0: + if min > len(stack): + min = len(stack) + # print("min:", min) + + removed_index = stack.pop() + amount += coins[removed_index] + return calc(removed_index + 1, amount) + + try: + return calc(0, amount) + except: + return min +``` + +## 모범 답안 + +모든 시점에서의 최선을 상황을 찾는다. + +```python +from typing import List +import math + + +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [0] + ([float('inf')] * amount) + for i in range(1, amount + 1): + for coin in coins: + if coin <= i: + dp[i] = min(dp[i], dp[i - coin] + 1) + + if dp[-1] == float('inf'): + return -1 + return dp[-1] +``` + +### 예시를 들어 차근차근 실행시켜보기 + +for 문의 끝에 아래와 같이 print를 넣어주면 다음과 같이 print 된다. + +```python +print(solution.coinChange([1, 2, 5], 10)) +``` + +```python +[0, 1, inf, inf, inf, inf, inf, inf, inf, inf, inf] 1 +[0, 1, 1, inf, inf, inf, inf, inf, inf, inf, inf] 2 +[0, 1, 1, 2, inf, inf, inf, inf, inf, inf, inf] 3 +[0, 1, 1, 2, 2, inf, inf, inf, inf, inf, inf] 4 +[0, 1, 1, 2, 2, 1, inf, inf, inf, inf, inf] 5 +[0, 1, 1, 2, 2, 1, 2, inf, inf, inf, inf] 6 +[0, 1, 1, 2, 2, 1, 2, 2, inf, inf, inf] 7 +[0, 1, 1, 2, 2, 1, 2, 2, 3, inf, inf] 8 +[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, inf] 9 +[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2] 10 +``` + +풀어서 설명하면 다음과 같다. + +![example page1](/assets/images/2024-02-26-leetcode-322/page1.png) +![example page2](/assets/images/2024-02-26-leetcode-322/page2.png) +![example page3](/assets/images/2024-02-26-leetcode-322/page3.png) +![example page4](/assets/images/2024-02-26-leetcode-322/page4.png) +![example page5](/assets/images/2024-02-26-leetcode-322/page5.png) + +## 결론 + +DP 문제를 더 많이 접해봐야겠다는 생각이 들었다. \ No newline at end of file diff --git a/assets/images/2024-02-26-jekyll-serve-error-level-undefined-method/error.png b/assets/images/2024-02-26-jekyll-serve-error-level-undefined-method/error.png new file mode 100644 index 0000000..09cf48a Binary files /dev/null and b/assets/images/2024-02-26-jekyll-serve-error-level-undefined-method/error.png differ diff --git a/assets/images/2024-02-26-leetcode-322/page1.png b/assets/images/2024-02-26-leetcode-322/page1.png new file mode 100644 index 0000000..415cccb Binary files /dev/null and b/assets/images/2024-02-26-leetcode-322/page1.png differ diff --git a/assets/images/2024-02-26-leetcode-322/page2.png b/assets/images/2024-02-26-leetcode-322/page2.png new file mode 100644 index 0000000..50ab20c Binary files /dev/null and b/assets/images/2024-02-26-leetcode-322/page2.png differ diff --git a/assets/images/2024-02-26-leetcode-322/page3.png b/assets/images/2024-02-26-leetcode-322/page3.png new file mode 100644 index 0000000..b531cc4 Binary files /dev/null and b/assets/images/2024-02-26-leetcode-322/page3.png differ diff --git a/assets/images/2024-02-26-leetcode-322/page4.png b/assets/images/2024-02-26-leetcode-322/page4.png new file mode 100644 index 0000000..89d3c5d Binary files /dev/null and b/assets/images/2024-02-26-leetcode-322/page4.png differ diff --git a/assets/images/2024-02-26-leetcode-322/page5.png b/assets/images/2024-02-26-leetcode-322/page5.png new file mode 100644 index 0000000..31c109e Binary files /dev/null and b/assets/images/2024-02-26-leetcode-322/page5.png differ