![[Lv. 0] 마지막 두 원소](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FlI4Cc%2FbtseK756pZe%2Fp57L20nv9j1witIeaw2MIk%2Fimg.png)

[Lv. 0] 마지막 두 원소프로그래머스/Level 02023. 5. 11. 16:04
Table of Contents
문제 출처 : https://school.programmers.co.kr/learn/courses/30/lessons/181927
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
난이도 : Level.0
언어 : Kotlin
문제 설명 :
정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
제한 사항 :
- 2 ≤ num_list의 길이 ≤ 10
- 1 ≤ num_list의 원소 ≤ 9
입출력 예 :
num_list | result |
[2, 1, 6] | [2, 1, 6, 5] |
[5, 2, 1, 7, 5] | [5, 2, 1, 7, 5, 10] |
풀이 :
class Solution {
fun solution(num_list: IntArray): IntArray = if (num_list[num_list.lastIndex] > num_list[num_list.lastIndex - 1]) num_list + (num_list[num_list.lastIndex] - num_list[num_list.lastIndex - 1]) else num_list + (num_list[num_list.lastIndex] * 2)
}
'프로그래머스 > Level 0' 카테고리의 다른 글
[Lv. 0] 원소들의 곱과 합 (0) | 2023.05.12 |
---|---|
[Lv. 0] 이어 붙인 수 (0) | 2023.05.12 |
[Lv. 0] 수 조작하기 1 (0) | 2023.05.11 |
[Lv. 0] 수 조작하기 2 (0) | 2023.05.11 |
[Lv. 0] 수열과 구간 쿼리 3 (0) | 2023.05.11 |

@스몰스테핑 :: 작은 발걸음
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!