전체 글41 [Leetcode] 237. Delete Node in a Linked List [문제] Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail node in the list. [생각] 알고 보면 정말 간단한 문제였는데 순간 뇌정지가 왔네요 ㅋㅋ 이전 node의 정보를 알 수 없는데 이걸 삭제하라고...? Linked List 특성상 다음 node의 정보는 접근 가능하나, 이전 node의 접근은 불가능 합니다. 어.. 2022. 3. 20. [Leetcode] 54. Spiral Matrix (Python) class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: def check(matrix): for i in range(len(matrix)): for j in range(len(matrix[i])): if matrix[i][j] == 0: return True return False row = len(matrix) col = len(matrix[0]) visited = [[0]*col for i in range(row)] ans = [] # left: [0,1], down:[1,0], right: [0,-1], up: [-1,0] move = [0, 1] y = 0 x = 0 while check(visited): if co.. 2022. 1. 24. 백준 [2839번] 설탕 배달 (Python) 좀 복잡하지만 조건을 여러개 세워서 풀었습니다. n = int(input()) cnt = 0 pass_flag = False m = n if m%5 == 0: cnt = m//5 else: if m5: temp = m//5 while 1: m = n m -= (5*temp) if m % 3 == 0: cnt += temp cnt += m//3 pass_flag = True break temp -= 1 if temp < 0: break if pass_flag == False: m = n if m % 3 == 0: cnt += n//3 else: cnt = -1 print(cnt) 아래는 1년전 풀이인데 더 간단하게 풀었더라고요,,, 퇴화하는 실력..? n = int(input()) s=0 if n==4 o.. 2022. 1. 20. 백준 [6571번] 피보나치 수의 개수 (Python) 정답률이 생각보다 낮은 문제입니다,, 아마 피보나치를 재귀로 구하다가 시간초과가 많이 나서 정답률이 낮은 것 같네요! 피보나치는 동적 프로그래밍(Dynamic Programming) 방식으로 풀어야 쉽게 통과 될 걸로 보입니다. (피보나치 DP 코딩에 대해 쓴 글입니다. https://yejun402.tistory.com/2) Python Code def dp(a, b): dp = [1,1] cnt = 0 for i in range(2,b+1): if dp[i-1]+dp[i-2] > b: break dp.append(dp[i-1]+dp[i-2]) dp = dp[1:] for i in dp: if a 2022. 1. 11. 동적 프로그래밍(Dynamic Programming)을 이용한 피보나치(Fibonacci) 수 구하기 (Python) 보통 코딩을 배울 때, 피보나치 구현을 배울 때는 재귀(Recursion)를 이용한 방법으로 구현한다. 그러나 재귀 방식의 알고리즘은 시간복잡도가 O(n^2)로 아주 오래 걸리기 때문에 보통은 동적 프로그래밍(DP)를 이용하는 편이다. 동적 프로그래밍의 시간복잡도는 O(n)이다. 재귀 방식의 피보나치 수를 구하는 함수 def recursion(n): if n==1 or n==2: return 1 else: return fibo(n-1) + fibo(n-2) 동적 프로그래밍 방식의 피보나치 수를 구하는 함수 def dp(n): dp = [1,1] if n==1 or n== 2: return 1 for i in range(2,n): dp.append(dp[i-1]+dp[i-2]) return dp[-1] 2022. 1. 11. 이전 1 ··· 4 5 6 7 다음