[리뷰] 그리디 - 모험가 길드

2020. 10. 3. 14:34알고리즘과 자료구조/[리뷰]이것이 취업을 위한 코딩 테스트다

반응형

github.com/ndb796/python-for-coding-test

 

ndb796/python-for-coding-test

[한빛미디어] "이것이 취업을 위한 코딩 테스트다 with 파이썬" 전체 소스코드 저장소입니다. - ndb796/python-for-coding-test

github.com

제한시간은 30분을 준다. 코드는 간결하지만 생각을 조금 해야 문제를 풀 수 있다. 때로는 문제의 설명글이 도움을 주지만 때로는 사람을 낚을 수도 있다. 

 

파이썬

import sys
n = int(input())

horror_list = list(map(int,sys.stdin.readline().rstrip().split()))

horror_list.sort()
i =0
result = 0
cnt = 0

/* original 코드
while i<n:
  horror = horror_list[i]
  cnt+=1
  if cnt <horror:
    i+=1
  else:
    cnt = 0
    result +=1
    i+=1*/
    
# 정리한 코드
for horror in horror_list:
  cnt +=1
  if cnt >= horror:
    result +=1
    cnt = 0 
print(result)

 

c++

#include <bits/stdc++.h>


using namespace std;

int main() {
  int n;
  vector<int> v;
  int cnt = 0;
  int result = 0;

  cin >>n; //입력받기

  for(int i=0;i<n;i++){  //리스트 담기
    int tmp;
    cin >>tmp;
    v.push_back(tmp);
  }
  sort(v.begin(), v.end()); //정렬

  for(int i =0; i<n; i++){
    cnt++;
    if (cnt>= v[i]){
      cnt = 0;
      result ++;
    }
  }
  printf("%d", result);
}
반응형