Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- Queue
- 알고리즘
- dynamic programming
- 프로그래머스
- lastIndex
- 동적계획법
- Java
- Recursion
- contentToString
- GREEDY
- indices
- Kotlin
- Main
- 2D Array
- 2020
- Developer
- Util
- sortedBy
- foreach
- heap
- report
- PriorityQueue
- hackerrank
- booleanarray
- Poll
- intarray
- programmers
- 코틀린
- dp
- solution
Archives
- Today
- Total
Code in
프로그래머스 해시 - 위장 with Kotiln 본문
** 중간에 Intelli J Kotlin을 업데이트하고 나서 에러가 났었는데
에러 내용: Kotlin: [Internal Error] java.rmi.UnmarshalException: ~~~
Build -> Rebuild Project 하고 나서 에러가 해결됐어요!
프로그래머스 해시부분 위장 문제입니다.

IntelliJ에서의 풀이입니다. Hash를 쓰려고 노력했습니다!
실제로 HashMap을 출력하면 다음 처럼 출력됩니다.
{-1290360528=1, -1115211921=2}
{3135069=3}
val clothes: Array<Array<String>> = arrayOf(
arrayOf("yellow_hat", "headgear"), arrayOf("blue_sunglasses", "eyewear"),
arrayOf("green_turban", "headgear")
)
val clothes2: Array<Array<String>> = arrayOf(
arrayOf("crow_mask", "face"), arrayOf("blue_sunglasses", "face"),
arrayOf("smoky_makeup", "face")
)
fun solution(clothes: Array<Array<String>>): Int {
var answer = 1
val kindOf = HashMap<Int, Int>()
clothes.forEach {
val kindHash: Int = it[1].hashCode()
if (kindOf.containsKey(kindHash)) {
val value = kindOf[kindHash]!!.plus(1)
// !!.은 Not Null 표시입니다.
kindOf[kindHash] = value
} else kindOf[kindHash] = 1
}
kindOf.values.forEach {
answer *= (it + 1)
}
return answer - 1
}
fun main() {
println(solution(clothes)) // 5
println(solution(clothes2)) // 3
}
URL: https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr
다른 분 풀이입니다! gropuBy와 map, reduce, minus를 사용해서 풀이할 수 있습니다.
더보기
다른 분 풀이인데 Kotlin에 대해 더 배우기 좋아서 나중에 보려고 저장합니다...
class Solution {
fun solution(clothes: Array<Array<String>>) = clothes
.groupBy { it[1] }.values // group by type of clothes, keep only names of clothes
.map { it.size + 1 } // number of things to wear in a group (including wearing nothing)
.reduce(Int::times) // combine
.minus(1) // remove the case where the spy wears nothing
}
'알고리즘 스터디_문제풀이' 카테고리의 다른 글
[프로그래머스] 2020 카카오 인턴십 키패드 누르기 (0) | 2020.11.17 |
---|---|
프로그래머스 깊이/너비 우선 탐색 여행경로 with Kotlin (0) | 2020.08.30 |
프로그래머스 깊이/너비 우선 탐색 단어 변환 with Kotlin (1) | 2020.08.26 |
프로그래머스 깊이/너비 우선 탐색 네트워크 with Kotlin (1) | 2020.08.26 |
프로그래머스 깊이/너비 우선 탐색 타겟 넘버 with kotlin (1) | 2020.08.25 |