코-딩

코-딩/Leetcode

[leetcode] 435. Non-overlapping Intervals

문제 : https://leetcode.com/problems/non-overlapping-intervals/description/ 난이도 : Medium 인터벌들이 겹치지 않도록 제거해야하는 인터벌 수 구하는 문제!! 풀이 class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: count = 0 intervals.sort() for i in range(len(intervals) - 1): if intervals[i + 1][0] < intervals[i][1]: count += 1 intervals[i + 1][1] = min(intervals[i + 1][1],intervals[i][1]) return co..

코-딩/Leetcode

[leetcode] 1342. Number of Steps to Reduce a Number to Zero

문제 : https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/description/ 난이도 : Easy 풀이 class Solution: def numberOfSteps(self, num: int) -> int: cnt = 0 while num > 0: if num % 2 == 0: num /= 2 else: num -= 1 cnt += 1 return cnt 이건 뭐... 너무 쉬워서 할 말이 없움ㅎ 다른 풀이 class Solution: def numberOfSteps(self, num: int) -> int: return bin(num).count("1") * 2 + bin(num).count("0") - 2 한 줄로 끝..

코-딩/Leetcode

[leetcode] 997. Find the Town Judge

문제 : https://leetcode.com/problems/find-the-town-judge/description/ 난이도 : Lv.1 풀이 class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust: if n == 1: return 1 else: return -1 relationship = [[] for _ in range(n+1)] for a, b in trust: #a는 b를 신뢰 relationship[b].append(a) for i in range(1, n+1): if len(relationship[i]) == n-1: for t in range(1, n+1): if i in relatio..

코-딩/프로그래머스

프로그래머스::실패율

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42889 난이도 : Lv.1 풀이 def solution(N, stages): answer = {} totalUser = len(stages) # 총 유저수 for i in range(1, N+1): if(totalUser == 0): answer[i] = 0 else: answer[i] = stages.count(i)/totalUser totalUser -= stages.count(i) answer = dict(sorted(answer.items(), key=lambda x : x[1], reverse=True)) return list(answer.keys()) 다 풀고 나니까, 마지막에 ..

코-딩/프로그래머스

프로그래머스::주차 요금 계산

문제 : https://school.programmers.co.kr/learn/courses/30/lessons/92341 난이도 : Lv.2 풀이 from collections import defaultdict import math def solution(fees, records): answer = [] inDict = dict() outDict = defaultdict(int) for re in records: time, carNo, div = re.split(" ") mTime = int(time.split(":")[0])*60 + int(time.split(":")[1]) # 분 환산 if(div == "IN"): inDict[carNo] = mTime elif(div == "OUT"): outD..

코-딩/Leetcode

[leetcode] 14.Longest Common Prefix

문제 : https://leetcode.com/problems/longest-common-prefix/description/ 난이도 : Easy 풀이 class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 1: return strs[0] strs = sorted(strs) minLen = min(len(strs[0]), len(strs[-1])) i = 0 while i < minLen and strs[0][i] == strs[-1][i]: i += 1 prefix = strs[0][0:i] return prefix 배열을 다 훑어서 하나하나 비교해야 될 것 같지만, 사실은 오름차순 정렬 후 처음과 끝의 ..

힞뚜루마뚜루
'코-딩' 카테고리의 글 목록 (4 Page)