-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyunjung-choi] WEEK 01 solutions #1684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a4ad8e9
6559206
d3a4c76
50a2ffb
f43803c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Solution { | ||
fun containsDuplicate(nums: IntArray): Boolean { | ||
return nums.size != nums.toSet().size | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package leetcode_study | ||
|
||
class Solution { | ||
fun rob(nums: IntArray): Int { | ||
if (nums.size == 1) return nums[0] | ||
|
||
var prev2 = nums[0] | ||
var prev1 = maxOf(nums[0], nums[1]) | ||
|
||
for (i in 2 until nums.size) { | ||
val current = maxOf(prev1, prev2 + nums[i]) | ||
prev2 = prev1 | ||
prev1 = current | ||
} | ||
|
||
return prev1 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package leetcode_study | ||
|
||
class Solution { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 풀이 지금은 O(nlogn)으로 보이는데 O(n)으로 최적화할 수 있는 문제라고 들었던 것 같습니다! 한번 개선해보시면 좋을 것 같아요 그리고 풀이 가이드에 자신이 생각하는 자신의 풀이 방법과 시간, 공간 복잡도를 적어두는 지침이 있었던 것 같습니다! |
||
fun longestConsecutive(nums: IntArray): Int { | ||
if (nums.isEmpty()) return 0 | ||
|
||
val sortedNums = nums.sorted() | ||
var maxLength = 1 | ||
var currentLength = 1 | ||
|
||
for (i in 1 until sortedNums.size) { | ||
when { | ||
sortedNums[i] == sortedNums[i - 1] -> continue | ||
sortedNums[i] == sortedNums[i - 1] + 1 -> { | ||
currentLength++ | ||
maxLength = maxOf(maxLength, currentLength) | ||
} | ||
else -> currentLength = 1 | ||
} | ||
} | ||
|
||
return maxLength | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
fun topKFrequent(nums: IntArray, k: Int): IntArray { | ||
val frequency = nums.toList().groupingBy { it }.eachCount() | ||
|
||
return frequency | ||
.toList() | ||
.sortedByDescending { it.second } | ||
.take(k) | ||
.map { it.first } | ||
.toIntArray() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 풀이도 해쉬맵을 이용하면 O(n)까지 최적화할 수 있습니다! |
||
fun twoSum(nums: IntArray, target: Int): IntArray { | ||
val result = IntArray(2) | ||
|
||
for (i in nums.indices) { | ||
for (j in i + 1 until nums.size) { | ||
if (nums[i] + nums[j] == target) { | ||
result[0] = i | ||
result[1] = j | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.