Skip to content

[이유진] Week1 #674

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

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions contains-duplicate/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package leetcode_study

class SolutionContainsDuplicate {
fun containsDuplicate(nums: IntArray): Boolean {
val size = nums.size
val numsToSet = nums.toSet()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 ^^
이 부분은 Set으로 중복 값을 제거하신거군요!
풀이가 간결해서 좋아보입니다 👍


return size != numsToSet.size
}
}
24 changes: 24 additions & 0 deletions longest-consecutive-sequence/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package leetcode_study
class SolutionLongestConsecutiveSequence {
fun longestConsecutive(nums: IntArray): Int {
nums.sort()
var cnt = 0
var maxCnt = 0
nums.forEachIndexed { i, _ ->
if (i == 0) {
cnt = 1
}
else if (nums[i-1] == nums[i] - 1) {
cnt++
}
else if (nums[i - 1] == nums[i]) {
return@forEachIndexed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 kotlin 을 잘 몰라서 질문드립니다 😅 이 부분은 어떠한 작업인가요?
인덱스를 건너뛰는 작업인걸가요? ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와, 신기한 코드네요 ㅋㅋ 저도 궁금합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach 문으로 return 해줍니다~
말씀해주신대로 해당 인덱스를 건너뛰는 의도로 작성했습니다 :)

} else {
cnt = 1
}
maxCnt = maxOf(maxCnt, cnt)
}

return maxCnt
}
}
15 changes: 15 additions & 0 deletions top-k-frequent-elements/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package leetcode_study
class SolutionTopKFrequentElements {
fun topKFrequent(nums: IntArray, k: Int): IntArray {
val numsCount = mutableMapOf<Int, Int>()
for (num in nums) {
val value = numsCount.getOrDefault(num, 0)
numsCount[num] = value + 1
}
val sortedNumsCount = numsCount.entries
.sortedByDescending { it.value }
.associate { it.toPair() }

return sortedNumsCount.keys.take(k).toIntArray()
}
}
27 changes: 27 additions & 0 deletions valid-palindrome/Real-Reason.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package leetcode_study

class SolutionValidPalindrome {
fun isPalindrome(s: String): Boolean {
val sToCharArray = s.toCharArray()
var startIndex = 0
var endIndex = sToCharArray.size - 1
while (startIndex < endIndex) {
if (!sToCharArray[startIndex].isLetterOrDigit() || sToCharArray[startIndex].isWhitespace()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 글자 부분인지 확인하는 if문으로 보이는데요~
앞단에서 특수문자, 공백을 제거하는것도 좋을것 같습니다! ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

흠... 🤔 코드는 좀 더 복잡해도 저는 현재 방법이 더 효율적일 수 있다는 생각이 들어요. 앞단에서 특수문자, 공백을 제거하려면 입력 문자열을 2번 스캔해야하니까요. 물론 대세(빅오 표현법 상으로는)에는 지장이 없겠지만요 😄

startIndex++
continue
}
if (!sToCharArray[endIndex].isLetterOrDigit() || sToCharArray[endIndex].isWhitespace()) {
endIndex--
continue
}
if (sToCharArray[startIndex].lowercase() == sToCharArray[endIndex].lowercase()) {
startIndex++
endIndex--
} else {
return false
}
}

return true
}
}
Loading