본문 바로가기

Algorithm/Leet Code

LeetCode #506 RelativeRanks. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

LeetCode #506 RelativeRanks. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접,연결리스트

 

 

 Performance 10%

 Memory both 30%

 

Super bad

 

 

LeetCode #506

Q.

 Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

 

 N 개 운동점수가 주어지는데, 상대적인 순위랑 탑3 사람들을 찾아라. 메달 받는 사람들은 "Gold Medal", "Silver Medal", "Bronze Medal" 로 들어간다.

 

 

Example :

 

Input: [5, 4, 3, 2, 1]

Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]

 

Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".

 

높은 점수 3명은 메달로 받고, 낮은점수는 순위로 매겨진다.

 

For the left two athletes, you just need to output their relative ranks according to their scores.

 

 

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

노트:

 1. N 은 10000을 넘지않는 양의정수

 2. 모든 운동선수 점수는 중복되지 않는다.

 

Process

// Process
//1. Input athletes score vector
//2. Make index vector
//3. Sort score vector and index vector
//4. Make resultVector
//4. Return resultVector

 

 

// 처리과정

//1. 운동점수 벡터 입력받는다.

//2. 인덱스 벡터 만든다.

//3. 운동점수 벡터 오름차순 정렬하면서 인덱스벡터도 오름차순 정렬한다.

//4. 결과 벡터 만든다.

//5. 결과벡터 반환한다.

 

 

Code.. lemme see example code!!!

코드.. 예제코드를 보자!!!

 

 

 

class Solution {
public:
	vector<string> findRelativeRanks(vector<int>& nums) {
		vector<string> resultVector;

		vector<int> indexVector;
		for (int i = 0; i < nums.size(); ++i) {
			indexVector.push_back(i);
		}

		for (int i = 0; i < nums.size(); ++i) {
			for (int j = 0; j < nums.size() - 1 - i; ++j) {
				if (nums[j] > nums[j + 1]) {
					int buf = nums[j];
					nums[j] = nums[j + 1];
					nums[j + 1] = buf;

					buf = indexVector[j];
					indexVector[j] = indexVector[j + 1];
					indexVector[j + 1] = buf;
				}
			}
		}

		for (int i = 0; i < indexVector.size(); ++i) {
			nums[indexVector[i]] = indexVector.size() - i;
		}

		for (int i = 0; i < nums.size(); ++i) {
			if (nums[i] == 1) {
				resultVector.push_back("Gold Medal");
			}
			else if (nums[i] == 2) {
				resultVector.push_back("Silver Medal");
			}
			else if (nums[i] == 3) {
				resultVector.push_back("Bronze Medal");
			}
			else {
				resultVector.push_back(itos(nums[i]));
			}
		}

		return resultVector;
	}
    
private:
	string itos(int integer) {
		std::stringstream ss;
		ss << integer;
		return ss.str();
	}
};

 

 

 

Something else you might like...?

 

 

2019/04/28 - [Algorithm/Leet Code] - LeetCode #121 BestTimeToBuyAndSellStock. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/24 - [Algorithm/Leet Code] - LeetCode #119 Pascal'sTriangle2. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 파스칼..

2019/04/24 - [Algorithm/Leet Code] - LeetCode #104 MaximumDepthOfBinaryTree. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/22 - [Algorithm/Leet Code] - LeetCode #118 Pascal'sTriangle. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/17 - [Algorithm/Leet Code] - LeetCode #88 MergeSortedAray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/04/17 - [Algorithm/Leet Code] - LeetCode #021 MergeTwoSortedLists. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

 

 

2019/04/14 - [Programming/C++] - C++ Math - sqrt (square root, 제곱근, 루트). stl, math.h, 씨쁠쁠, example code, 예제코드

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #56 digitsProduct. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

2019/01/04 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #55 differentSquares. Algorithm, 알고리즘, Codefights, CodeSignal, 코드파이트, 코드시그널, 예제,문제해결능력,example, c++ java c# scalar

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법