Skip to content

Commit

Permalink
[Week3](gmlwls96) Product of array except self
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlwls96 committed Dec 23, 2024
1 parent 26b6991 commit b1a3b4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions product-of-array-except-self/gmlwls.kt
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
}
}
2 changes: 1 addition & 1 deletion two-sum/gmlwls96.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Solution {
// ์‹œ๊ฐ„ : O(logN), ๊ณต๊ฐ„(2N)
// ์‹œ๊ฐ„ : 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๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
Expand Down

0 comments on commit b1a3b4b

Please sign in to comment.