๋ฐ์ํ
p.181 ์ ๋ ฌ ์ค์ ๋ฌธ์
def solution():
n = int(input())
student = []
for i in range(n):
name,grade = input().split()
student.append([name,int(grade)])
student =sorted(student,key = lambda x : x[1] )
for i in student:
print(i[0],end=' ')
solution()
์ ๋ง์ ๋ง ๊ฐ๋คํ ๋ฌธ์ ๋ฐ ๋จ์ํ ์ค์๋ฅผ ํด์ ๊ธฐ๋กํ๋ค.
๋ฐฐ์ด์
1. ์ ๋ ฅ ๋ฐ์ ๊ฐ์ด ์ ์์ธ์ง ๋ฌธ์์ธ์ง ๊ตฌ๋ถํ๊ธฐ
2. sorted๋ฅผ ํ๊ณ ๋ ๊ฒฐ๊ณผ๋ ๋ค์ ๋ฐ๊ธฐ (sort์ ๋ค๋ฅด๋ค! ์ ๋ ฌํ ๊ฐ์ ์ฌํ ๋น ํด์ค์ผ ํ๋ค)
p.182 ๋ ๋ฐฐ์ด์ ์์ ๊ต์ฒด
์๊ฐ ๋ณต์ก๋ ์๊ฐ ์ํ๊ณ ํผ ํ์ด
def solution():
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
for i in range(k):
minA = min(A)
maxB = max(B)
A[A.index(minA)],B[B.index(maxB)]=B[B.index(maxB)],A[A.index(minA)]
print(A)
print(sum(A))
solution()
def solution ():
n,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A.sort()
B.sort(reverse=True)
for i in range(k):
if A[i]<B[i]:
A[i],B[i]=B[i],A[i]
else:
break
print(sum(A))
solution()
๋ฐฐ์ด์
1. ์๊ฐ ํจ์จ์ฑ์ ๊ณ ๋ คํ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํ์.
2. ์กฐ๊ฑด์ด ์๋๋ฉด breakํด์ ๋ค์ํ test case์ ์๊ฐํจ์จ์ ์ค์ด์
p.197 ๋ถํ์ฐพ๊ธฐ
์ด์งํ์
def solution():
n = int(input())
store_part =list(map(int,input().split()))
m = int(input())
find_part =list(map(int,input().split()))
def bin_search(target,data,start,end):
while start <= end:
mid = (start+end)//2
if data[mid]== target:
return mid
elif data[mid]<target:
start = mid+1
elif data[mid]>target:
end = mid-1
return None
for i in range(m):
result = bin_search(find_part[i],store_part,0,n-1)
if result != None:
print('yes',end= ' ')
else:
print('no',end= ' ')
๊ณ์ํ์
def solution():
n = int(input())
# store_part =list(map(int,input().split()))
arr = [0]*1000000
for i in input().split():
arr[int(i)] = 1
m = int(input())
find_part =list(map(int,input().split()))
for f in find_part:
if arr[f] ==1:
print('yes',end= ' ')
else:
print('no',end= ' ')
p.201 ๋ก๋ณถ์ด ๋ง๋ค๊ธฐ
๋ด ํ์ด
def solution():
n,m = map(int,input().split())
high= list(map(int,input().split()))
def bin_search(target,data,start,end):
while start<=end:
mid = (start+end)//2
pri = data[mid]
sumRice = sum([d-pri for d in data if d-pri>0])
if sumRice>= target:
return mid
else:
start = mid+1
high.sort()
result = bin_search(m,high,0,n-1)
print(high[result])
solution()
๋์ ํ์ด๊ฐ ๋ค๋ฅธ ํ์ด์ ๋ค๋ฅธ์ ์
- sort๋ฅผ ํด์ ๋ฐ๋ก mid๋ฅผ return ํ๋ฉด ๋๋ค๋ ์
- sumRice๋ผ๋ ๋ฐฐ์ด์ ๋ฃ์ด๋๊ณ ํฉ์ ๊ตฌํ๋๋ฐ ๊ตณ์ด ๋ฐฐ์ด์ ๋ฃ์ง ์๊ณ total ์ ๋ํด๋๋ฉด ๋๋ค.
'๐ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด์ฝ๋ค] ๊ธฐ์ถ๋ฌธ์ - ๊ทธ๋ฆฌ๋ (0) | 2021.08.25 |
---|---|
[์ด์ฝ๋ค] ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ (0) | 2021.08.23 |
[์ด์ฝ๋ค] ๊ทธ๋ฆฌ๋ค & ๊ตฌํ ์ค์ ๋ฌธ์ (0) | 2021.07.23 |
[codility] Lesson 4) FrogRiverOne MaxCounter (0) | 2021.07.10 |
[codility] Lv2- OddOccurrencesInArray,Lv3-TapeEquilibrium ,TapeEquilibrium (0) | 2021.06.21 |
๋๊ธ