def solution(people, limit):
answer = 0
# 먼저 people을 오름차순으로 정렬한다
people.sort();
# 초기값 설정.
start = 0;
end = len(people) - 1
pair_count = 0;
while(True):
if start >= end:
break;
weight = people[start] + people[end]
if weight <= limit:
pair_count+=1
start+=1
end-=1
else:
end-=1
answer = len(people) - (pair_count*2) + pair_count
return answer
def solution2(people, limit) :
answer = 0
people.sort()
a = 0
b = len(people) - 1
while a < b :
if people[b] + people[a] <= limit :
a += 1
answer += 1
b -= 1
return len(people) - answer
def solution3(people, limit):
answer = 0
people.sort();
start = 0;
end = len(people) - 1
pair_count = 0;
while(start < end):
if people[start] + people[end] <= limit:
pair_count+=1
start+=1
end-=1
answer = len(people) - pair_count
return answer
1) 아주 똑같은 로직이지만, 코드를 얼마나 줄일 수 있는지,
2) 변수 명은 명시적으로 한 내가 더 괜찮은 것 처럼 보이지만 몇 줄 되지도 않는데 의미가 있을까싶지만, 항상 습관화 하기 위한 방법이라고 생각한다.
1) 무한 루프는 피하는게 좋은데, 당연히 break되는 것을 첫 조건문으로 적었으면 되었음
2) 불필요한 변수(weight) 자제하기
3) 동일한 반복이라면 밖에 두고 한줄로 줄이기 end -= 1