본문 바로가기

전체 글657

[leetcode] 131. Palindrome Partitioning 문제 : https://leetcode.com/problems/palindrome-partitioning/ 문자열 s가 입력으로 주어지면 파티션의 모든 하위 문자열이 palidrome 이 되도록 하는 모든 경우를 구하라. 문자열을 앞뒤로 읽었을 때 같은 문자열인 경우 palidrome이라 한다. DP + backtracking 팰린드롬인지 구하는 함수. DP // dp[s][e] = str[s ~ e]가 palidrome인지 fun isPalidrome(str, s, e) { if(dp[s][e]가 없으면) dp[s][e] = str[s]와 str[e]가 같은 문자이며 isPalidrome(str, s+1, s-1) 인 경우 return dp[s][e]; } 팰린드롬 하위 문자열들을 만들어서 partit.. 2022. 1. 6.
[Kotlin] Collection - associateBy, associateWith, associate AssociateBy collection 요소에 파라미터 함수에서 반환된 값을 키 값, 요소를 value로 하는 map을 반환한다. data class Member( val no: Int, val name: String ) fun main() { val members = listOf( Member(no=10, name="A"), Member(no=20, name="B"), Member(no=30, name="C"), Member(no=40, name="D"), Member(no=50, name="E"), ) val associateMembers = members.associateBy{it.no} println(""" ${associateMembers::class.simpleName} keys : ${as.. 2021. 12. 30.
[Kotlin] Collection - all, any, count, find All collection 의 원소들이 모두 조건을 만족하는지 println(""" 모든 원소가 조건을 만족하는지 홀수인지 [1, 3, 5, 7] : ${listOf(1, 3, 5, 7).all{ it%2 == 1 }} 홀수인지 [1, 3, 5, 7, 8] : ${listOf(1, 3, 5, 7, 8).all{ it%2 == 1 }} """.trimIndent()) 모든 원소가 조건을 만족하는지 홀수인지 [1, 3, 5, 7] : true 홀수인지 [1, 3, 5, 7, 8] : false Any collection 안에 조건을 만족하는 원소가 존재하는지 println(""" 홀수가 있는지 [1, 3, 5, 6] : ${listOf(1, 3, 5, 6).any{ it%2 == 0 }} [1, 3, 5] .. 2021. 12. 29.
[Leetcode] 1496. Path Crossing 문제 : https://leetcode.com/problems/path-crossing/ path를 탐색하며 현재 위치 좌표를 갱신한다. 갱신하면서 set에 현재 위치의 좌표에 온적있는지 확인한다. 만일 탐색한적 있다면 true를 반환한다. 탐색한적 없다면 set에 현재 위치를 추가하고 탐색을 계속한다. 모든 path를 탐색할때까지 중목된 좌표를 탐색한 적 없다면 false를 반환한다. 시간/공간복잡도는 O(N). 소스코드 : https://github.com/fpdjsns/Algorithm/blob/master/leetcode/easy/1496.%20Path%20Crossing.cpp 2021. 12. 28.
[Kotlin] Sealed Class Sealed Class 의 구현체들은 컴파일 시간에 알 수 있습니다. sealed class SealClass { abstract val no: Int } class A: SealClass() { override val no: Int = 12 } fun main() { // Sealed types cannot be instantiated // val sealClass = SealClass() val a = A() .also { println(it.no) } } sealed class는 abstract 멤버를 가질 수 있습니다. sealed class는 그자체로 객체가 될 수 없습니다. (Sealed types cannot be instantiated 에러 뱉음) internal class SealedCl.. 2021. 12. 28.
[Leetcode] 56. Merge Intervals 문제 : https://leetcode.com/problems/merge-intervals/ 간격 start, end 를 저장한 interval 배열이 주어질 때, 겹치는 모든 간격을 병합하여 간격이 겹치지 않게 만든 뒤 간격 배열을 반환하라. intervals 배열을 start 오름차순으로 정렬한다. start, end 변수를 만들고 겹치는 간격을 만나면 end 변수를 갱신해간다. 정렬한 배열을 앞에서부터 탐색하며 종료 간격 end가 탐색하는 간격의 start보다 같거나 크다면 합쳐질 수 있는 경우이므로 end를 갱신한다. 만일 그렇지 않다면 start, end를 정답 배열에 추가한다. [[1,3],[2,6],[8,10],[15,18]] 예시 intervals[i] start end answer 배열 .. 2021. 12. 24.