본문 바로가기

컴퓨터

티스토리 코드블럭 이쁘게 넣기2 html 편집으로 들어간다 티스토리에서 코드블럭을 넣으면 pre 태그 밑에 code 태그로 들어간다. 그래서 code 태그를 스타일링 해주는 코드를 넣는다. 넣기 전과 후를 비교해보자면 넣기 전 넣은 후 줄간격이랑 폰트 크기는 취향껏! 더보기
프로그래머스 - 징검다리 (바위 부수기) def solution(distance, rocks, n): rocks.sort() minDistance, maxDistance = 1, distance current = (minDistance + maxDistance) // 2 while True : cnt = 0 pre = 0 for idx in range(len(rocks)): if rocks[idx] - pre n : break else : pre = rocks[idx] continue if cnt 더보기
릿코드 5 - Longest Palindromic Substring class Solution: def longestPalindrome(self, s: str) -> str: answer = [0, 0] for i in range(1, len(s)-1): l, r = i, i while True : if s[l] == s[r]: l, r = l - 1, r + 1 if l == -1 or r == len(s): l, r = l + 1, r - 1 break else : l, r = l + 1, r - 1 break if answer[1] - answer[0] < r - l: answer = [l, r] for i in range(0, len(s) - 1): if s[i] == s[i + 1]: l, r = i, i + 1 while True : if s[l] == s[r.. 더보기
프로그래머스 호텔 방 배정 import sys sys.setrecursionlimit(10000) def find(room_number, assignment): if room_number not in assignment : assignment[room_number] = room_number + 1 return room_number empty_room = find(assignment[room_number], assignment) assignment[room_number] = empty_room + 1 return empty_room def solution(k, room_number): answer = [] assignment = {} for request_room_number in room_number: num = find(requ.. 더보기
프로그래머스 징검다리 건너기 def solution(stones, k): minimum = 0 maximum = 200000000 person = maximum // 2 while True: # 건널 수 있나? possible = True cnt = 0 for i in stones : if i < person : cnt += 1 else : cnt = 0 if cnt == k : possible = False break # 건널 수 있으면 if possible : minimum = person next = (person + maximum) // 2 if person == next : return person person = next # 건널 수 없으면 else : maximum = person next = (person + mini.. 더보기
프로그래머스 - 디스크 컨트롤러[heap] import heapq def solution(jobs): jobLen = len(jobs) jobs.sort() waitSum = 0 time = jobs[0][0] for comeIn, jobTime in jobs: if comeIn > time: time = comeIn + jobTime else: time += jobTime waitSum += time - comeIn answer1 = waitSum // jobLen waitSum = 0 time = jobs[0][0] heap = [] task = jobs.pop(0) heapq.heappush(heap, (task[1], task)) while jobs or heap: if heap: task = heapq.heappop(heap)[1] if.. 더보기
프로그래머스5 - 방의 개수 def solution(arrows): direction = [ [0,1], [1,1], [1,0], [1,-1], [0,-1], [-1,-1], [-1,0], [-1,1], ] answer = 0 dic = {} path = {} x, y = 0, 0 dic[str([x, y])] = True for item in arrows: for i in range(2) : nextx, nexty = x + direction[item][0], y + direction[item][1] pathKey = str([x, y, nextx, nexty]) pathKey2 = str([nextx, nexty, x, y]) dicKey = str([nextx, nexty]) if not dicKey in dic : path[.. 더보기
릿코드 3 - Longest Substring, 카카오 보석쇼핑 def solution(gems): mySet = set(gems) setLen = len(mySet) minCount = 100001 if setLen == 1: return [1, 1] dic = {} for i in mySet: dic[i] = 0 r, l = 0, 0 dic[gems[0]] += 1 while r ran : minCount = ran ans = [l+1, r+1] if ran == setLen-1 : return ans dic[gems[l]] -.. 더보기
릿코드 84 - Largest Rectangle in Histogram class Solution: def largestRectangleArea(self, heights: List[int]) -> int: tmp = [heights[0]] cnt = [1] for i in range(1, len(heights)) : if heights[i] == tmp[-1] : cnt[-1] += 1 else : tmp.append(heights[i]) cnt.append(1) heights = tmp max = 0 for idx, item in enumerate(heights) : tmp = cnt[idx] for i in range(idx + 1, len(heights)) : if heights[i] < item : break tmp += cnt[i] for i in range(idx.. 더보기
릿코드 338 - 2진법 변환 class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): cnt = 0 while i != 0 : if i % 2 : cnt += 1 i = i // 2 ans.append(cnt) return ans 너무 간단한 이진법 문제. 파이썬의 내장함수와 속도를 비교해봤다. class Solution: def countBits(self, n: int) -> List[int]: ans = [] for i in range(n+1): ans.append(bin(i).count(str(1))) return ans 훨씬 빠르다 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 수가 작을때는 첫번째 코드가 더 빠르지만 수가 커지면 bin() 과 coun.. 더보기