-
-
Notifications
You must be signed in to change notification settings - Fork 245
[gmlwls96] Week3 #764
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
[gmlwls96] Week3 #764
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f3cdc7b
[Week3](gmlwls96) two-sum
gmlwls96 471cb9d
[Week3](gmlwls96) two-sum code.
gmlwls96 26b6991
[Week3](gmlwls96) reverse-bits
gmlwls96 6e058a5
[Week3](gmlwls96) Product of array except self
gmlwls96 e990fe7
[Week3](gmlwls96) Combination Sum
gmlwls96 d4b4783
[Week3](gmlwls96) Maximum-subarray
gmlwls96 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,50 @@ | ||
class Solution { | ||
// 시간 : O(c^t), 공간 : O(t) | ||
// 알고리즘 : dfs | ||
val answerList = mutableSetOf<List<Int>>() | ||
|
||
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> { | ||
candidates.sort() | ||
combination( | ||
candidates = candidates, | ||
target = target, | ||
current = 0, | ||
currentList = listOf() | ||
) | ||
return answerList.toList() | ||
} | ||
|
||
private fun combination( | ||
candidates: IntArray, | ||
target: Int, | ||
current: Int, | ||
currentList: List<Int> | ||
) { | ||
candidates.forEach { // candidates를 한개씩 꺼내 | ||
val sum = current + it // 현재값을 더했을때 | ||
when { | ||
sum == target -> { // sum이 target과 동일한 값이면 answer 에 추가. | ||
answerList.add( | ||
currentList.toMutableList().apply { | ||
add(it) | ||
sort() | ||
} | ||
) | ||
} | ||
|
||
sum < target -> { // sum이 모자르면 다른 조합을 찾기 위해 재귀 호출. | ||
combination( | ||
candidates = candidates, | ||
target = target, | ||
current = sum, | ||
currentList = currentList.toMutableList().apply { | ||
add(it) | ||
} | ||
) | ||
} | ||
|
||
else -> return | ||
} | ||
} | ||
} | ||
} |
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,22 @@ | ||
class Solution { | ||
fun maxSubArray(nums: IntArray): Int { | ||
val dp = Array(nums.size) { y -> | ||
IntArray(nums.size) { x -> | ||
if (y == x) { | ||
nums[y] | ||
} else { | ||
0 | ||
} | ||
} | ||
} | ||
|
||
var max = dp[0][0] | ||
for (y in nums.indices) { | ||
for (x in y + 1..nums.lastIndex) { | ||
dp[y][x] = dp[y][x - 1] + nums[x] | ||
max = max(max, dp[y][x]) | ||
} | ||
} | ||
return max | ||
} | ||
} |
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,20 @@ | ||
class Solution { | ||
// 시간 : O(2n) = O(n) ,공간 : O(1) | ||
fun productExceptSelf(nums: IntArray): IntArray { | ||
val answer = IntArray(nums.size) { 1 } | ||
|
||
var n = 1 | ||
for (i in 0 until nums.lastIndex) { | ||
n *= nums[i] | ||
answer[i + 1] = n | ||
} | ||
println(answer.toList()) | ||
|
||
n = 1 | ||
for (i in nums.lastIndex downTo 1) { | ||
n *= nums[i] | ||
answer[i - 1] *= n | ||
} | ||
return answer | ||
} | ||
} |
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,14 @@ | ||
class Solution { | ||
// you need treat n as an unsigned value | ||
fun reverseBits(n: Int): Int { | ||
var bitString = Integer.toBinaryString(n) | ||
bitString = CharArray(32 - bitString.length) { '0' }.concatToString() + bitString | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var result = 0 | ||
var scale = 1 | ||
bitString.forEach { | ||
result += it.digitToInt() * scale | ||
scale *= 2 | ||
} | ||
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,29 @@ | ||
class Solution { | ||
// 시간 : O(NlogN)-정렬하는데 드는 시간복잡도., 공간(2N) | ||
fun twoSum(nums: IntArray, target: Int): IntArray { | ||
val sortNums = List(nums.size) { listOf(nums[it], it) }.sortedBy { it[0] } | ||
// 1. list( list('값', 'index')) 형태의 list를 만들고 값을 기준으로 정렬한다. | ||
|
||
var i = 0 | ||
var j = sortNums.lastIndex | ||
// 2. 2포인터 방식으로 두 값을 합했을때 target이 되는 값을 찾는다. | ||
while (i < j) { | ||
val sum = sortNums[i][0] + sortNums[j][0] | ||
when { | ||
sum == target -> { // target과 sum이 일치할시 바로 return. | ||
return intArrayOf( | ||
min(sortNums[i][1], sortNums[j][1]), | ||
max(sortNums[i][1], sortNums[j][1]) | ||
) | ||
} | ||
sum < target -> { // sum이 target보다 값이 작은경우 i를 한칸씩 더한다. | ||
i++ | ||
} | ||
sum > target -> { // sum이 target보다 값이 큰경우 j를 한칸씩 내린다. | ||
j-- | ||
} | ||
} | ||
} | ||
return intArrayOf() | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.