-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyunjung-choi] WEEK 03 solutions #1780
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d39ddcd
Add valid palindrome solution
hyunjung-choi ee4469d
Add number of 1 bits solution
hyunjung-choi 0f731bc
Add combination sum solution
hyunjung-choi 40601be
Add decode ways solution
hyunjung-choi abd7113
Add maximum subarray solution
hyunjung-choi 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 @@ | ||
class Solution { | ||
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> { | ||
val result = mutableListOf<List<Int>>() | ||
val currentCombination = mutableListOf<Int>() | ||
|
||
candidates.sort() | ||
|
||
backtrack(candidates, target, 0, currentCombination, result) | ||
|
||
return result | ||
} | ||
|
||
private fun backtrack( | ||
candidates: IntArray, | ||
remainingTarget: Int, | ||
startIndex: Int, | ||
currentCombination: MutableList<Int>, | ||
result: MutableList<List<Int>> | ||
) { | ||
if (remainingTarget == 0) { | ||
result.add(currentCombination.toList()) | ||
return | ||
} | ||
|
||
if (remainingTarget < 0) { | ||
return | ||
} | ||
|
||
for (i in startIndex until candidates.size) { | ||
val candidate = candidates[i] | ||
|
||
if (candidate > remainingTarget) { | ||
break | ||
} | ||
|
||
currentCombination.add(candidate) | ||
|
||
backtrack(candidates, remainingTarget - candidate, i, currentCombination, result) | ||
|
||
currentCombination.removeAt(currentCombination.size - 1) | ||
} | ||
} | ||
} |
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,27 @@ | ||
class Solution { | ||
fun numDecodings(s: String): Int { | ||
if (s.isEmpty() || s[0] == '0') return 0 | ||
|
||
val n = s.length | ||
val dp = IntArray(n + 1) | ||
|
||
dp[0] = 1 | ||
dp[1] = 1 | ||
|
||
for (i in 2..n) { | ||
val currentChar = s[i - 1] | ||
val prevChar = s[i - 2] | ||
|
||
if (currentChar != '0') { | ||
dp[i] += dp[i - 1] | ||
} | ||
|
||
val twoDigit = (prevChar - '0') * 10 + (currentChar - '0') | ||
if (twoDigit in 10..26) { | ||
dp[i] += dp[i - 2] | ||
} | ||
} | ||
|
||
return dp[n] | ||
} | ||
} |
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,13 @@ | ||
class Solution { | ||
fun maxSubArray(nums: IntArray): Int { | ||
var currentSum = nums[0] | ||
var maxSum = nums[0] | ||
|
||
for (i in 1 until nums.size) { | ||
currentSum = if (currentSum < 0) nums[i] else currentSum + nums[i] | ||
maxSum = maxOf(currentSum, maxSum) | ||
} | ||
|
||
return maxSum | ||
} | ||
} |
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,3 @@ | ||
class Solution { | ||
fun hammingWeight(n: Int) = Integer.bitCount(n) | ||
} |
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,9 @@ | ||
import java.util.Locale.getDefault | ||
|
||
class Solution { | ||
fun isPalindrome(s: String): Boolean { | ||
if (s.isBlank()) return true | ||
val cleanedString = s.trim().filter { it.isLetterOrDigit() }.lowercase(getDefault()) | ||
return cleanedString == cleanedString.reversed() | ||
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. 깔끔하네용 |
||
} | ||
} |
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.
코틀린에 깔쌈한 내장함수가있나보군요
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.
넵 저도 이번에 처음 알았습니다 ㅎㅎ