본문 바로가기

전체 글267

[BinarySearch] K번째 수 1300번 wookje.dance/2017/02/20/boj-1300-K%EB%B2%88%EC%A7%B8-%EC%88%98/ [BOJ] 1300 : K번째 수 1300 : K번째 수 풀이 임의의 숫자 m을 골라서 K번째 숫자인지 판단해보자! 그 임의의 숫자 m은 무려 O(log K)에! 무려 이분 탐색으로 찾아보자! 그렇다면 떠오르는 질문, m 보다 작은 숫자의 개수를 어 wookje.dance N = 4일 때 배열 A = [1, 2, 3, 4] [2, 4, 6, 8] [3, 6, 9, 12] [4, 8, 12, 16] 풀이 임의의 숫자 m을 골라서 K번째 숫자인지 판단해보자! 그 임의의 숫자 m은 무려 O(log K)에! 무려 이분 탐색으로 찾아보자! 그렇다면 떠오르는 질문, m 보다 작은 숫자의 개수를 어떻게 .. 2020. 12. 3.
[BinarySearch] 가장 긴 증가하는 부분 수열 2 hooongs.tistory.com/129 [백준12015번] 가장 긴 증가하는 부분 수열 2 / Python3 문제 수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50}. hooongs.tistory.com m.blog.naver.com/PostView.nhn?blogId=bestmaker0290&logNo=220820005454&proxyReferer=https:%2F%2Fwww.google.com%2F 알고리즘 기초 - Lower Bound & Upper Bound Lower Bound와 Upper Boun.. 2020. 12. 3.
[BinarySearch] 수들의 합 서로 다른 N개의 자연수의 합이 S라고 한다. S를 알 때, 자연수 N의 최댓값은 얼마일까? N의 최댓값을 구해야되기 때문에 자연수의 합이 주어졌을 때 가장 작은 수 부터 차례로 더하면서 그 합이 S를 넘기 직전의 개수를 출력하면 된다. 1부터 N까지의 합은 N(N+1) / 2 N이 1일때를 제외하고 N(N+1) / 2 2020. 12. 1.
[BinarySearch] 공유기 설치 n, c = list(map(int, input().split(' '))) array = [] for _ in range(n): array.append(int(input())) array.sort() start = array[1] - array[0] end = array[-1] - array[0] result = 0 while (start = value + mid: value = array[i] print("installing to {} as {}th router".format(array[i], count)) count += 1 if count >= c: # if more than c routers can be installed, increase range print("increase") start = .. 2020. 12. 1.
[BinarySearch] 예산 랜선자르기와 비슷한 유형의 문제 _ = int(input()) array = list(map(int, input().split())) max_budget = int(input()) start = 0 end = max(array) while start max_budget: end = mid - 1 else: start = mid + 1 print(end) # start = 128, end = 127로 종료된다 여기서도 최대를 찾는 것이기 때문에 랜선 자르기 문제처럼 end지점이 찾고자 하는 값이다. our_budget을 리스트로 만들었는데 그냥 0으로 할당하고 매번 더해도 상관은 없다. 디버깅 하면서 리스트 보고 싶어서 만들었음. 검색하다보니 min으로 x와 mid를 비교하면 코드가 더 간단해진다는 것을 .. 2020. 11. 29.
[BinarySearch&Counter] 숫자 카드 2 특정 array에 있는 특정 정수의 개수를 구하는 문제이다. 생각해본 방법 1. Counter 메서드를 사용해서 dict의 key값이 있다면 해당 key의 value를 차례로 출력 2. BinarySearch를 활용 ( 정확히 어떤식으로 구현해야될지 모르겠음) Counter는 쉽게 구현가능하다 from collections import Counter n = int(input()) sang = list(map(int, input().split())) m = int(input()) array = list(map(int, input().split())) C = Counter(sang) for x in array: if x in C: print(C[x], end=' ') else: print("0", end='.. 2020. 11. 27.
[BinarySearch] 랜선 자르기 여기서 계속 헤멨던 이유 기존 사용하던 BinarySearch Iterative n, m = map(int, input().split()) array = list(map(int, input().split())) array.sort() # Using start, mid, end start = 1 end = max(array) # Binary Search # H = 0 while(start = N:) 여기서도 탐색을 멈추지 않고 계속 진행하면서 end값을 찾으면 되는 것이다. 2020. 11. 20.
[BinarySearch] 나무 자르기 (iterative vs recursion) 여기서 에러가 났던 부분은 시간제한을 맞추지 못했던. Recursion으로 BinarySearch를 수행하면 실패하지만 while문으로 해결하면 통과가 가능하다. Recursion import time start_time = time.time() n, target = map(int, input().split()) trees = list(map(int, input().split())) trees.sort() def BinarySearch(array, target, start, end): if start > end: return None mid = (start+end)//2 lefty = 0 for tree in trees: if tree > mid: lefty += tree - mid if lefty ==.. 2020. 11. 19.
198. House Robber Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. class Solution(object): def rob(self, nums): """ :type nums: L.. 2020. 8. 16.