-
Notifications
You must be signed in to change notification settings - Fork 0
Week7_SunghunKim #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Week7_SunghunKim #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // 백준_11286 절댓값 힙 | ||
| /* | ||
| 오답1. 절대값이 같은 값이 여러 개일 경우에는 작은 수를 먼저 출력해야함 | ||
| */ | ||
| func solution(_ calculations: [Int]) { | ||
| var heap: [Int] = [0] // heap[0]은 0으로 더미 값 | ||
|
|
||
| func insert(_ value: Int) { | ||
| heap.append(value) | ||
|
|
||
| /* 오답 1 | ||
| // bubble up | ||
| var index = heap.count - 1 | ||
| while index > 1 && abs(heap[index]) < abs(heap[index/2]) { | ||
| heap.swapAt(index, index/2) | ||
| index /= 2 | ||
| } | ||
| */ | ||
|
|
||
| // bubble up | ||
| var index = heap.count - 1 | ||
| while index > 1 { | ||
| let parent = index / 2 | ||
| if abs(heap[index]) < abs(heap[parent]) || | ||
| (abs(heap[index]) == abs(heap[parent]) && | ||
| heap[index] < heap[parent]) { | ||
| heap.swapAt(index, parent) | ||
| index = parent | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func removeMin() -> Int { | ||
| if heap.count == 1 { | ||
| return 0 | ||
| } else if heap.count == 2 { | ||
| return heap.removeLast() | ||
| } else { | ||
| let minValue = heap[1] | ||
| heap[1] = heap.removeLast() | ||
|
|
||
| /* | ||
| 오답1. | ||
|
|
||
| // bubble down | ||
| var index: Int = 1 | ||
| while index*2 < heap.count { | ||
| var child = index * 2 | ||
| if child + 1 < heap.count && abs(heap[child+1]) < abs(heap[child]) { | ||
| child += 1 | ||
| } | ||
|
|
||
| if abs(heap[index]) <= abs(heap[child]) { break } | ||
|
|
||
| heap.swapAt(index, child) | ||
| index = child | ||
| } | ||
|
|
||
| */ | ||
| var index: Int = 1 | ||
| while index * 2 < heap.count { | ||
| var child = index * 2 | ||
| if child + 1 < heap.count && | ||
| (abs(heap[child + 1]) < abs(heap[child]) || | ||
| (abs(heap[child + 1]) == abs(heap[child]) && | ||
| heap[child + 1] < heap[child])) { | ||
| child += 1 | ||
| } | ||
|
|
||
| if abs(heap[index]) < abs(heap[child]) || | ||
| (abs(heap[index]) == abs(heap[child]) && | ||
| heap[index] <= heap[child]) { | ||
| break | ||
| } | ||
|
|
||
| heap.swapAt(index, child) | ||
| index = child | ||
| } | ||
|
|
||
| return minValue | ||
| } | ||
| } | ||
|
|
||
| for calculation in calculations { | ||
| if calculation == 0 { | ||
| print(removeMin()) | ||
| } else { | ||
| insert(calculation) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let n: Int = Int(readLine()!)! | ||
| var calculations: [Int] = [] | ||
| for _ in (0..<n) { | ||
| calculations.append(Int(readLine()!)!) | ||
| } | ||
| solution(calculations) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // 백준 2075 N번째 큰 수 | ||
|
|
||
| /* | ||
| 최소힙 사용 | ||
| - 최대 n개까지 유지 | ||
| - 입력되는 수를 하나씩 힙에 삽입 | ||
| - 힙 크기가 n이 넘으면 루트(가장 작은 값) 제거 | ||
| - 마지막에 루트값이 n번째 큰 수가 됨 | ||
| */ | ||
|
|
||
| /* | ||
| 오답1: solution1메서드: 배열을 사용해봤지만 역시 시간복잡도에서 문제가 됩니다. | ||
| 오답2: solution메서드: 구조체를 사용하면 조금 빨라져서 해봤는데 아직 문제가 됩니다. | ||
| */ | ||
|
|
||
| func solution1(_ arrays: [[Int]]) { | ||
| let n: Int = arrays.count | ||
|
|
||
| var heap: [Int] = [0] // 0은 인덱스 처리를 위한 더미값, | ||
|
|
||
| func insert(_ value: Int) { | ||
| heap.append(value) | ||
|
|
||
| // bubble up | ||
| var index = heap.count - 1 | ||
| while index > 1 && heap[index] < heap[index/2] { | ||
| heap.swapAt(index, index/2) | ||
| index /= 2 | ||
| } | ||
| } | ||
|
|
||
| func removeMin() { | ||
| heap[1] = heap.removeLast() | ||
|
|
||
| // bubble down | ||
| var index = 1 | ||
| while index*2 < heap.count { | ||
| var child = index*2 | ||
| if child + 1 < heap.count && | ||
| heap[child + 1] < heap[child] { | ||
| child += 1 | ||
| } | ||
| if heap[index] <= heap[child] { break } | ||
|
|
||
| heap.swapAt(index, child) | ||
| index = child | ||
| } | ||
| } | ||
|
|
||
| for array in arrays { | ||
| for num in array { | ||
| if heap.count <= n { | ||
| insert(num) | ||
| } else if num > heap[1] { | ||
| removeMin() | ||
| insert(num) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| print(heap[1]) | ||
| } | ||
|
|
||
| // heap 구조체 정의 | ||
| struct MinHeap { | ||
| private var heap = [Int]() | ||
|
|
||
| init() { | ||
| heap.append(0) | ||
| } | ||
|
|
||
| var count: Int { | ||
| return heap.count - 1 | ||
| } | ||
|
|
||
| var top: Int { | ||
| return heap[1] | ||
| } | ||
|
|
||
| mutating func insert(_ value: Int) { | ||
| heap.append(value) | ||
|
|
||
| // bubble up | ||
| var index = heap.count - 1 | ||
| while index > 1 && heap[index] < heap[index/2] { | ||
| heap.swapAt(index, index/2) | ||
| index /= 2 | ||
| } | ||
| } | ||
|
|
||
| mutating func removeMin() { | ||
| guard heap.count > 1 else { return } | ||
| heap.swapAt(1, heap.count - 1) | ||
| _ = heap.removeLast() | ||
|
|
||
| // bubble down | ||
| var index = 1 | ||
| while index*2 < heap.count { | ||
| var child = index * 2 | ||
| if child + 1 < heap.count && | ||
| heap[child + 1] < heap[child] { | ||
| child += 1 | ||
| } | ||
| if heap[index] <= heap[child] { break } | ||
|
|
||
| heap.swapAt(index, child) | ||
| index = child | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func solution(_ arrays: [[Int]]) { | ||
| let n = arrays.count | ||
| var heap = MinHeap() | ||
|
|
||
| for array in arrays { | ||
| for num in array { | ||
| if heap.count < n { | ||
| heap.insert(num) | ||
| } else if num > heap.top { | ||
| heap.removeMin() | ||
| heap.insert(num) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| print(heap.top) | ||
| } | ||
|
|
||
| let n: Int = Int(readLine()!)! | ||
| var arrays: [[Int]] = [] | ||
| for _ in (0..<n) { | ||
| let line: [Int] = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| arrays.append(line) | ||
| } | ||
|
|
||
| solution(arrays) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Foundation | ||
|
|
||
| /* | ||
| 오답: 시간초과 | ||
| func solution(_ topping: [Int]) -> Int { | ||
| var count: Int = 0 | ||
|
|
||
| for i in (1..<topping.count) { | ||
| let leftSet = Set(Array(topping[..<i])) | ||
| let rightSet = Set(Array(topping[i...])) | ||
|
|
||
| if leftSet.count == rightSet.count { count += 1 } | ||
| } | ||
|
|
||
| return count | ||
| } | ||
| */ | ||
|
|
||
| func solution(_ topping: [Int]) -> Int { | ||
| var count: Int = 0 | ||
| var leftSet: Set<Int> = [] | ||
| var rightDict: [Int: Int] = [:] | ||
|
|
||
| // 먼저 모든 토핑을 rightDict에 저장 | ||
| for top in topping { | ||
| rightDict[top, default: 0] += 1 | ||
| } | ||
|
|
||
| // 왼쪽에서 하나씩 늘려가면서 비교하기 | ||
| for i in (0..<topping.count) { | ||
| let t = topping[i] | ||
| leftSet.insert(t) | ||
|
|
||
| rightDict[t]! -= 1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뒤에다가 !를붙이는군요 스위프트에서는 신기하네요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 딕셔너리는 키로 값을 찾을때 보통 |
||
| if rightDict[t]! == 0 { | ||
| rightDict.removeValue(forKey: t) | ||
| } | ||
|
|
||
| if leftSet.count == rightDict.count { | ||
| count += 1 | ||
| } | ||
| } | ||
|
|
||
| return count | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| func solution(_ progresses: [Int], _ speeds: [Int]) -> [Int] { | ||
| // 남은 배포일 계산 | ||
| var days: [Int] = [] | ||
| for i in 0..<progresses.count { | ||
| let remainDay = ((100 - progresses[i]) + (speeds[i] - 1)) / speeds[i] | ||
| days.append(remainDay) | ||
| } | ||
|
|
||
| // 스택에 넣으면서 결과 만들기 | ||
| var stack: [Int] = [] | ||
| var result: [Int] = [] | ||
| for day in days { | ||
| if stack.isEmpty { | ||
| stack.append(day) | ||
| } else if day <= stack.last! { // day가 top과 같거나 작으면 같은 배포일 | ||
| stack.append(stack.last!) | ||
| } else { // day가 top보다 크면 날짜 기록하고, stack 초기화(pop) | ||
| result.append(stack.count) | ||
| stack = [day] | ||
| } | ||
| } | ||
|
|
||
| if !stack.isEmpty { | ||
| result.append(stack.count) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 큐를 생각했는데, 생각해보니 결국 어차피 한 방향으로 꺼내기만 하는거라 스택이든 큐든 상관이 없네용
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 스택 큐 둘다 안쓰고도 되더라구요 |
||
| // 스택을 사용하지 않는 버전 | ||
| func solution2(_ progresses: [Int], _ speeds: [Int]) -> [Int] { | ||
| var days: [Int] = [] | ||
| for i in 0..<progresses.count { | ||
| let remainDay = ((100 - progresses[i]) + (speeds[i] - 1)) / speeds[i] | ||
| days.append(remainDay) | ||
| } | ||
|
|
||
| var result: [Int] = [] | ||
| var current = days[0] | ||
| var count = 1 | ||
|
|
||
| for i in 1..<days.count { | ||
| if days[i] <= current { | ||
| count += 1 | ||
| } else { | ||
| result.append(count) | ||
| current = days[i] | ||
| count = 1 | ||
| } | ||
| } | ||
|
|
||
| result.append(count) | ||
| return result | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 첫번째 방법을 가장 먼저 생각했네요,,, 핵심은 저장 데이터?를 적게 만드는 것 같습니당 이 코드로 중복 검사를 set외에 dictonary를 이용하는 방법 있다는 걸 알게 되었습니다☺️