강의

멘토링

커뮤니티

Inflearn Community Q&A

rlaxogud940418's profile image
rlaxogud940418

asked

Introduction to Algorithm Problem Solving for IT Employment (with C/C++): Coding Test Preparation

75. Maximum income schedule (priority queue greedy: sorting using structure and vector)

75번 정렬 질문입니다!

Written on

·

195

0

75번에서 벡터에 넣고 정렬을 하는데 정렬이 내림차순인데 마지막 부분 (20,1) 이랑 (30,1)은 왜 (20,1) (30,1) 이렇게 나오는 걸까요??

C++코테 준비 같이 해요!

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

struct Data{
	int money;
	int when;
	Data(int a, int b){
		money=a;
		when=b;
	}
	bool operator<(Data &b){
		return when>b.when;
	}	
};

위 코드는 정렬 기준이 날짜에 의해서만 정렬이 됩니다. 돈은 정렬과 상관없습니다.

만약 날짜에 의해서 내림차순 정렬하고 날짜가 같을 경우는 돈에 의해서 내림차순 정렬을 하고 싶으면 아래와 같이 하면 됩니다.

struct Data{
	int money;
	int when;
	Data(int a, int b){
		money=a;
		when=b;
	}
	bool operator<(Data &b){
		if(when==b.when){
			return money>b.money;
		}
		else return when>b.when;
	}	
};
rlaxogud940418's profile image
rlaxogud940418

asked

Ask a question