-
Notifications
You must be signed in to change notification settings - Fork 0
2회차 #4
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
Merged
2회차 #4
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8451bfc
baekjoon 2750
MuseopKim 08fed40
baekjoon 1377
MuseopKim 7f08f68
baekjoon 1427
MuseopKim 1b429ca
baekjoon 11399
MuseopKim 4b4e201
beakjoon 11004
MuseopKim 0a052f0
baekjoon 2751
MuseopKim 80e469b
baekjoon 1517
MuseopKim 4b00d7d
baekjoon 10989
MuseopKim 2c10f1e
docs: notes.md
MuseopKim 92ed61a
chore: rearrange directory
MuseopKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#=================== 메모리 초과 | ||
import sys | ||
from collections import deque | ||
|
||
max_number = 10_000 | ||
input = sys.stdin.readline | ||
|
||
count = int(input()) | ||
numbers = [] | ||
for i in range(count): | ||
numbers.append(int(input())) | ||
|
||
queues = dict() | ||
for i in range(10): | ||
queues[i] = deque() | ||
|
||
for i in range(len(str(max_number))): | ||
for number in numbers: | ||
if len(str(number)) < i + 1: | ||
queues[0].append(number) # i = 0 => -1, i = 1 => -2, i = 2 => -3, i = 3 => -4 | ||
else: | ||
queues[int(str(number)[-i - 1])].append(number) | ||
|
||
numbers = [] | ||
for j in range(0, 10): | ||
while len(queues[j]) > 0: | ||
numbers.append(queues[j].popleft()) | ||
|
||
for number in numbers: | ||
print(number) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import sys | ||
input = sys.stdin.readline | ||
|
||
n = int(input()) | ||
counts = [0] * 10_001 | ||
|
||
for i in range(n): | ||
counts[int(input())] += 1 | ||
|
||
for i in range(10_001): | ||
if counts[i] != 0: | ||
for _ in range(counts[i]): | ||
print(i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
###### 시간 초과 | ||
import sys | ||
|
||
|
||
def quick_sort(arr: list, left: int, right: int, search_index: int) -> None: | ||
if left < right: | ||
pivot = divide(arr, left, right) | ||
if pivot == search_index: | ||
return | ||
if pivot > search_index: | ||
quick_sort(arr, left, pivot - 1, search_index) | ||
if pivot < search_index: | ||
quick_sort(arr, pivot + 1, right, search_index) | ||
|
||
|
||
def divide(arr: list, left: int, right: int) -> int: | ||
pivot = arr[left] | ||
left_start_index = left + 1 | ||
right_start_index = right | ||
|
||
while left_start_index < right_start_index: | ||
while left_start_index < right and pivot > arr[left_start_index]: | ||
left_start_index += 1 | ||
|
||
while right_start_index > (left + 1) and pivot < arr[right_start_index]: | ||
right_start_index -= 1 | ||
|
||
if left_start_index < right_start_index: | ||
temp = arr[left_start_index] | ||
arr[left_start_index] = arr[right_start_index] | ||
arr[right_start_index] = temp | ||
|
||
if arr[right_start_index] < pivot: | ||
temp = arr[left] | ||
arr[left] = arr[right_start_index] | ||
arr[right_start_index] = temp | ||
|
||
return right_start_index | ||
|
||
|
||
input = sys.stdin.readline | ||
count, k_th = map(int, input().split()) | ||
k_index = k_th - 1 | ||
|
||
numbers = [int(number) for number in input().split()] | ||
quick_sort(numbers, 0, len(numbers) - 1, k_index) | ||
|
||
print(numbers[k_index]) | ||
|
||
# count, k_th = map(int, input().split()) | ||
# k_index = k_th - 1 | ||
# | ||
# numbers = [int(number) for number in input().split()] | ||
# numbers.sort() | ||
# | ||
# print(numbers[k_index]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
count = int(input()) | ||
minutes = list(map(int, input().split())) | ||
|
||
for i in range(1, len(minutes)): | ||
current = minutes[i] | ||
insertionIndex = i | ||
|
||
for j in range(i - 1, -1, -1): | ||
if minutes[j] > current: | ||
minutes[j + 1] = minutes[j] | ||
insertionIndex = j | ||
|
||
if (minutes[j] <= current): | ||
break | ||
|
||
minutes[insertionIndex] = current | ||
|
||
sum_array = [0] * len(minutes) | ||
sum_array[0] = minutes[0] | ||
total = sum_array[0] | ||
for i in range(1, len(minutes)): | ||
sum_array[i] = sum_array[i - 1] + minutes[i] | ||
total += sum_array[i] | ||
|
||
print(total) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import sys | ||
|
||
input = sys.stdin.readline | ||
|
||
count = int(input()) | ||
numbers = [(int(input()), i) for i in range(count)] | ||
|
||
max = 0 | ||
# python의 sort는 O(NlogN)의 성능을 갖는다. | ||
sorted_numbers = sorted(numbers) | ||
for i in range(len(sorted_numbers)): | ||
if sorted_numbers[i][1] - i > max: | ||
max = sorted_numbers[i][1] - i | ||
|
||
# 정렬이 일어나지 않은 구간도 포함되므로 +1이 필요하다. | ||
print(max + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
numbers = [int(i) for i in input()] | ||
|
||
for i in range(len(numbers) - 1): | ||
for j in range(i + 1, len(numbers)): | ||
if numbers[i] < numbers[j]: | ||
temp = numbers[i] | ||
numbers[i] = numbers[j] | ||
numbers[j] = temp | ||
|
||
print(''.join(map(str, numbers))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
|
||
def merge_sort(arr: list, left: int, right: int) -> None: | ||
if left < right: | ||
middle = (left + right) // 2 | ||
merge_sort(arr, left, middle) | ||
merge_sort(arr, middle + 1, right) | ||
merge(arr, left, middle, right) | ||
|
||
|
||
def merge(arr: list, left: int, middle: int, right: int) -> None: | ||
global move_count | ||
left_area_index = left | ||
right_area_index = middle + 1 | ||
merge_array_index = left | ||
|
||
while left_area_index <= middle and right_area_index <= right: | ||
if arr[left_area_index] <= arr[right_area_index]: | ||
merge_array[merge_array_index] = arr[left_area_index] | ||
left_area_index += 1 | ||
elif arr[left_area_index] > arr[right_area_index]: | ||
merge_array[merge_array_index] = arr[right_area_index] | ||
move_count = move_count + (right_area_index - merge_array_index) | ||
right_area_index += 1 | ||
|
||
merge_array_index += 1 | ||
|
||
while left_area_index <= middle: | ||
merge_array[merge_array_index] = arr[left_area_index] | ||
left_area_index += 1 | ||
merge_array_index += 1 | ||
|
||
while right_area_index <= right: | ||
merge_array[merge_array_index] = arr[right_area_index] | ||
right_area_index += 1 | ||
merge_array_index += 1 | ||
|
||
for i in range(left, right + 1): | ||
arr[i] = merge_array[i] | ||
|
||
|
||
input = sys.stdin.readline | ||
|
||
count = int(input()) | ||
numbers = list(map(int, input().split())) | ||
|
||
move_count = 0 | ||
merge_array = [0] * count | ||
merge_sort(numbers, 0, len(numbers) - 1) | ||
|
||
print(move_count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
count = int(input()) | ||
|
||
numbers = [] | ||
for _ in range(count): | ||
numbers.append(int(input())) | ||
|
||
for i in range(0, len(numbers) - 1): | ||
for j in range(0, len(numbers) - i - 1): | ||
if numbers[j] > numbers[j + 1]: | ||
temp = numbers[j] | ||
numbers[j] = numbers[j + 1] | ||
numbers[j + 1] = temp | ||
|
||
for number in numbers: | ||
print(number) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sys | ||
|
||
def merge_sort(arr: list, left: int, right: int) -> None: | ||
if left < right: | ||
middle = (left + right) // 2 | ||
merge_sort(arr, left, middle) | ||
merge_sort(arr, middle + 1, right) | ||
merge(arr, left, middle, right) | ||
|
||
|
||
def merge(arr: list, left: int, middle: int, right: int) -> None: | ||
left_area_index = left | ||
right_area_index = middle + 1 | ||
merge_array_index = left | ||
|
||
while left_area_index <= middle and right_area_index <= right: | ||
if arr[left_area_index] <= arr[right_area_index]: | ||
merge_array[merge_array_index] = arr[left_area_index] | ||
left_area_index += 1 | ||
elif arr[left_area_index] > arr[right_area_index]: | ||
merge_array[merge_array_index] = arr[right_area_index] | ||
right_area_index += 1 | ||
|
||
merge_array_index += 1 | ||
|
||
while left_area_index <= middle: | ||
merge_array[merge_array_index] = arr[left_area_index] | ||
left_area_index += 1 | ||
merge_array_index += 1 | ||
|
||
while right_area_index <= right: | ||
merge_array[merge_array_index] = arr[right_area_index] | ||
right_area_index += 1 | ||
merge_array_index += 1 | ||
|
||
for i in range(left, right + 1): | ||
arr[i] = merge_array[i] | ||
|
||
|
||
input = sys.stdin.readline | ||
|
||
count = int(input()) | ||
numbers = [0] * count | ||
merge_array = [0] * count | ||
for i in range(count): | ||
number = int(input()) | ||
numbers[i] = number | ||
|
||
merge_sort(numbers, 0, len(numbers) - 1) | ||
|
||
for number in numbers: | ||
print(number) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
## Sort | ||
### Bubble sort | ||
- 첫 인덱스부터 순회하며 앞, 뒤 대소 관계를 비교하는 정렬 방식 | ||
- 구현은 간단하지만 O(n^2)의 시간복잡도를 가진다. | ||
- https://github.com/MuseopKim/data-structures/blob/master/rewind/src/main/java/bubblesort/BubbleSort.java | ||
|
||
### Selection sort | ||
- 버블정렬과 마찬가지로 O(n^2)의 성능을 가진다. | ||
- 구현방법 | ||
- 배열을 순회하면서 최소(혹은 최대)값을 특정 변수에 누적한다. | ||
- 순회가 끝났을 때 해당 변수에 저장된 값이 최소(혹은 최대) 값이고, 이 값을 정렬되지 않은 영역의 맨 앞 인덱스에 위치시킨다. | ||
이를 (배열의 길이 - 1)까지 반복한다. | ||
- https://github.com/MuseopKim/data-structures/blob/master/rewind/src/main/java/selectionsort/SelectionSort.java | ||
|
||
### Insertion sort | ||
- 정렬되지 않은 구간 (두 번째 인덱스부터 끝 인덱스) 요소를 순회하며 정렬된 구간(첫 인덱스 요소 하나로 시작하여 삽입이 한번 수행될 때 마다 점차 늘어감)의 특정 인덱스에 해당 요소를 삽입하여 정렬 구간을 넓혀 나가는 방식 | ||
- 정렬된 구간의 시작지점 = index 0 | ||
- 정렬되지 않은 구간의 시작지점 = 정렬된 구간의 끝지점 + 1 | ||
- https://github.com/MuseopKim/data-structures/blob/master/rewind/src/main/java/insertionsort/InsertionSort.java | ||
|
||
### Quick sort | ||
- 분할 정복 알고리즘 | ||
- pivot을 지정하고 지정 된 pivot과의 대소를 기준으로 포인터 2개를 이동하며 정렬 | ||
- 평균적으로 O(nlogn)의 성능. 드물게 pivot이 한쪽으로 치우치는 경우 O(n^2) | ||
- https://github.com/MuseopKim/data-structures/blob/master/rewind/src/main/java/quicksort/QuickSort.java | ||
|
||
### Merge sort | ||
- 크기가 N인 컬렉션이 있을 때, N만큼 그룹을 나누고 각 그룹을 병합하면서 정렬하는 방법 | ||
- 시간 복잡도 : O(nlongn) | ||
- quick sort와 비슷한 시간 복잡도를 갖는 것으로 평가 됨 | ||
- 차이점 | ||
- quick sort : pivot에 따라 최악의 시간 복잡도 n^2까지 나타날 수 있음 (극히 드문 경우) | ||
- merge sort : nlogn의 고정 된 시간복잡도를 갖게 되지만, 병합 할 배열을 따로 선언해야 하여 quick sort보다 더 많은 메모리 사용 | ||
- https://github.com/MuseopKim/data-structures/blob/master/rewind/src/main/java/mergesort/MergeSort.java | ||
|
||
|
||
## Python | ||
### Sort | ||
- Python 내부에서는 Tim sort를 사용한다고 한다. | ||
- https://d2.naver.com/helloworld/0315536 | ||
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. 👍 |
||
|
||
## 다시 볼 문제 | ||
- [1517](https://www.acmicpc.net/problem/1517) | ||
- merge sort 정렬 과정에 버블 정렬이 적용되는 것을 제대로 이해하고 있지 못하였다. | ||
|
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이런식으로 선언할수도 있군요!