-
-
Notifications
You must be signed in to change notification settings - Fork 195
[친환경사과] Week 5 #864
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
[친환경사과] Week 5 #864
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 두 거래일 사이 최고의 수익을 구하는 문제 | ||
* 시간 복잡도: O(n^2) | ||
* -> 모든 경우의 수를 순회하며 거래일 사이 최고 가격을 구하는 로직: O(n^2) | ||
* 공간 복잡도: O(1) | ||
* 아래 로직은 시간 초과 발생 O(n^2)의 복잡도를 줄여야함. | ||
* */ | ||
fun maxProfit(prices: IntArray): Int { | ||
var result = Int.MIN_VALUE | ||
|
||
for (i in prices.indices) { | ||
for (j in i + 1 until prices.size) { | ||
if (prices[i] < prices[j]) { | ||
if (result < prices[j] - prices[i]) { | ||
result = prices[j] - prices[i] | ||
} | ||
} | ||
} | ||
} | ||
if (result == Int.MIN_VALUE) return 0 | ||
return result | ||
} | ||
|
||
/* | ||
* 가장 작은 값을 저장하는 변수와 가장 큰 수익을 갖는 변수를 두고 문제 해결 | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(1) | ||
* */ | ||
fun maxProfit2(prices: IntArray): Int { | ||
var minValue = Int.MAX_VALUE | ||
var maxValue = 0 | ||
|
||
for (price in prices) { | ||
if (price < minValue) { | ||
minValue = price | ||
} else if (price - minValue > maxValue) { | ||
maxValue = price - minValue | ||
} | ||
} | ||
return maxValue | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 문자열 인코딩 디코딩 문제 | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(n) | ||
* */ | ||
fun encoding(strs: List<String>): String { | ||
var result = "" | ||
if (strs.isEmpty()) return result | ||
for (index in 0 until strs.size - 1) { | ||
if (strs[index] == ":") { | ||
result += strs[index] + "::;" | ||
} else { | ||
result += strs[index] + ":;" | ||
} | ||
} | ||
result += strs[strs.size - 1] | ||
return result | ||
} | ||
|
||
fun decoding(str: String): List<String>{ | ||
val splitedStrList = str.split(":;") | ||
val result = mutableListOf<String>() | ||
for (splitStr in splitedStrList){ | ||
if (splitStr == "::") { | ||
result.add(":") | ||
} else { | ||
result.add(splitStr) | ||
} | ||
} | ||
return result | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package leetcode_study | ||
|
||
/* | ||
* 주어진 문자열 배열에서 anagram을 그룹핑하는 문제 | ||
* 자료구조 Map을 사용해 문제 해결. 오름차순으로 정렬한 char type을 재조합해 key 값으로 사용. value는 조회 대상 문자열 | ||
* 시간 복잡도: O(n) | ||
* 공간 복잡도: O(n) | ||
* */ | ||
fun groupAnagrams(strs: Array<String>): List<List<String>> { | ||
val result = mutableMapOf<String, MutableList<String>>() | ||
|
||
for (str in strs) { | ||
val key = str.toCharArray().sorted().joinToString("") | ||
result.computeIfAbsent(key) { mutableListOf() }.add(str) | ||
} | ||
|
||
if (strs.isEmpty()) return listOf(listOf()) | ||
return result.values.toList() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
시간 복잡도를 효과적으로 줄이신것 같습니다 :)