[Programmers] Level 2. 아날로그 시계
·
코딩테스트/Python
🔗 Problem Link 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr❔Thinking아날로그 시계에서, 초침이 주어진 시간 범위 내에서의 시침 또는 분침과 일치하는 횟수를 반환한다.두개의 바늘이 겹친다는 의미는, 하나의 바늘이 1초 후에 다른 바늘보다 앞선다는 뜻과 동일하다.00시와 12시는 모든 바늘이 겹치는 구간이기 때문에, 정확히 예외처리를 해야 한다.💻Solutiondef solution(h1, m1, s1, h2, m2, s2): cnt = 0 init_time = h1*60*60 + m1*60 + s1 end_time = h2*60*60 + m2*60 + s2 i..
[LeetCode] 443. String Compression
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/string-compression/❔Thinking주어진 문자 배열을 압축한다 (ex - ['a','a'] -> ['a','2'])주어진 문자 배열에 압축한 결과를 저장한다.💻Solutiondef compress(self, chars: List[str]) -> int: compressed = [] n = len(chars) cnt = 0 for i in range(1, len(chars)): if chars[i] == chars[i-1]: cnt += 1 else: cnt += 1 compressed.append(cha..
[LeetCode] 238. Product of Array Except Self
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/product-of-array-except-self/❔Thinking배열이 주어지면, 자신의 원소를 제외한 모든 원소의 곱을 배열에 담아 반환한다.나누기 연산 없이, O(n)의 시간복잡도를 가지는 알고리즘을 작성해야 한다.💻Solutiondef productExceptSelf(self, nums: List[int]) -> List[int]: left, right = 1, 1 n = len(nums) answer = [1] * n for i in range(n): answer[i] *= left left *= nums[i] for i in range(n-1, -1, -1):..
[LeetCode] 345. Reverse Vowels of a String
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/reverse-vowels-of-a-string/description/❔Thinking영단어가 주어지면, 모음(a,e,i,o,u)만을 역순으로 재배치하여 반환한다. (abe -> eba)알파벳은 소문자와 대문자를 구분한다.💻Solution1. 단어에 포함된 모음을 배열에 담고, 해당 배열에서 pop 하여 순서 재배치def reverseVowels(self, s: str) -> str: vowels_set = set(['a','e','i','o','u','A','E','I','O','U']) vowels_list = [] s = list(s) for char in s: if char in v..
[LangChain] 시작하기
·
ML_DL/MUJAKJUNG (무작정 시리즈)
LangChain 이란?LLM을 활용한 어플리케이션 개발을 위한 오픈소스 프레임워크DB, File System 지원LangChain 설치가상환경 설정conda create -n langchain python=3.11langchain 설치pip install -U langchainopenai 설치pip install langchain-openai입출력 확인from dotenv import load_dotenvfrom langchain_openai import ChatOpenAIload_dotenv()llm = ChatOpenAI()question = "100을 10으로 나눈 몫과 나머지를 알려줘."result = llm.invoke(question)print(result.content)# 나눈 몫은 10이..
[LeetCode] 605. Can Place Flowers
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/can-place-flowers/description❔Thinking0과 1로만 이루어진 flowerbed 배열이 주어질 때 주어진 꽃을 모두 심을 수 있다면 True, 아니라면 False를 반환한다.0인 곳에 꽃을 하나 심을 수 있고, 1이 인접하지 않아야 한다. (ex - [1,1], [0,1,1] 불가능)💻Solution1. 초기 풀이def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: flowers = 0 if flowerbed == [0] or (flowerbed == [1] and n == 0): return True elif flow..