Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Flutter
- livedata
- Coroutines
- textfield
- Button
- android
- TEST
- Dialog
- 테스트
- textview
- scroll
- 앱바
- DART
- intent
- tabbar
- binding
- CustomScrollView
- Navigation
- Kotlin
- Compose
- LifeCycle
- data
- 계측
- 앱
- ScrollView
- appbar
- activity
- viewmodel
- drift
- 안드로이드
Archives
- Today
- Total
Study Record
[프로그래머스] Level1 - 키패드 누르기 본문
728x90
#include <string>
#include <vector>
using namespace std;
int getDistance(int a, int b){
if(a == 0) a = 11;
if(b == 0) b = 11;
int distance = b - a;
if(distance < 0) distance = a - b;
return distance / 3 + distance % 3;
}
string solution(vector<int> numbers, string hand) {
string answer = "";
int nowLeft = 10; // *
int nowRight = 12; // #
for(int i=0; i<numbers.size(); i++){
int number = numbers[i];
if(number == nowLeft || number == 1 || number == 4 || number == 7){
nowLeft = number;
answer += 'L';
} else if(number == nowRight || number == 3 || number == 6 || number == 9){
nowRight = number;
answer += 'R';
} else {
// 2, 5, 8, 0 일 경우
int leftDistance = getDistance(nowLeft, number);
int rightDistance = getDistance(nowRight, number);
if(leftDistance > rightDistance){
nowRight = number;
answer += 'R';
} else if(leftDistance < rightDistance){
nowLeft = number;
answer += 'L';
} else {
if(hand == "right"){
nowRight = number;
answer += 'R';
} else {
nowLeft = number;
answer += 'L';
}
}
}
}
return answer;
}
728x90
'알고리즘' 카테고리의 다른 글
[프로그래머스] Level1 - 크레인 인형뽑기 게임 <스택> (0) | 2021.12.02 |
---|---|
[프로그래머스] Level2 - 오픈채팅방 (0) | 2021.12.01 |
[프로그래머스] Level1 - 숫자 문자열과 영단어 (0) | 2021.11.30 |
[프로그래머스] Level2 - 문자열 압축 (0) | 2021.11.30 |
[프로그래머스] Level1 - 신규 아이디 (0) | 2021.11.30 |