-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from kogorithm/week1_seoyoon
[Week1] 박서윤: 차이를 최대로, 부분수열의 합, 마인크래프트, 퇴사, 소수찾기
- Loading branch information
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
import kotlin.math.sqrt | ||
|
||
class Solution { | ||
var visited = BooleanArray(7, {false}) | ||
val set: MutableSet<Int> = mutableSetOf() | ||
var depth: Int = 0 | ||
|
||
fun solution(numbers: String): Int { | ||
var answer = 0 | ||
findPrimes(numbers,0, "") | ||
answer = set.size | ||
|
||
return answer | ||
} | ||
|
||
fun findPrimes(numbers: String, depth: Int, cur: String) { | ||
if (depth == numbers.length) return | ||
|
||
for (i in 0 until numbers.length) { | ||
if (visited[i] == true) { | ||
continue | ||
} | ||
else { | ||
var number = cur + numbers[i] | ||
|
||
if (isPrime(number.toInt())) { | ||
set.add(number.toInt()) | ||
} | ||
visited[i] = true | ||
findPrimes(numbers, depth + 1, number) | ||
visited[i] = false | ||
} | ||
} | ||
} | ||
|
||
fun isPrime(num: Int): Boolean { | ||
if (num <= 1) return false | ||
if (num > 1) { | ||
for (i in 2 .. sqrt(num.toDouble()).toInt()) { | ||
if (num % i == 0) return false | ||
} | ||
} | ||
return true | ||
} | ||
} |
This file contains 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,32 @@ | ||
val n = readln().toInt() | ||
val arr = readln().split(" ").map{it.toInt()}.toIntArray() | ||
var newArr = IntArray(n) | ||
var visited = BooleanArray(n, {false}) | ||
var max: Int = Int.MIN_VALUE | ||
|
||
fun main() { | ||
permutation(0) | ||
println(max) | ||
} | ||
|
||
fun permutation(depth: Int) { | ||
if (depth == n) { | ||
var sum = 0; | ||
for (i in 0 until n-1) { | ||
sum += Math.abs(newArr[i] - newArr[i+1]) | ||
} | ||
max = Math.max(max, sum) | ||
} | ||
|
||
for (i in 0 until n) { | ||
if (visited[i] == true) { | ||
continue | ||
} | ||
else { | ||
newArr[depth] = arr[i] | ||
visited[i] = true | ||
permutation(depth + 1) | ||
visited[i] = false | ||
} | ||
} | ||
} |
This file contains 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,49 @@ | ||
import java.util.StringTokenizer | ||
|
||
fun main() { | ||
val (n, m ,b) = readln().split(" ").map{it.toInt()} | ||
val ground = Array(n, {IntArray(m, {0})}) | ||
|
||
var high = Int.MIN_VALUE | ||
var low = Int.MAX_VALUE | ||
|
||
for (i in 0 until n) { | ||
val st = StringTokenizer(readln(), " ") | ||
for (j in 0 until m) { | ||
ground[i][j] = Integer.parseInt(st.nextToken()) | ||
high = Math.max(high, ground[i][j]) | ||
low = Math.min(low, ground[i][j]) | ||
} | ||
} | ||
|
||
solution(n, m, b, ground, high, low) | ||
} | ||
|
||
fun solution(n: Int, m: Int, b: Int, ground: Array<IntArray>, high: Int, low: Int) { | ||
var ansSec = Int.MAX_VALUE | ||
var ansHigh = 0 | ||
|
||
for (x in low .. high) { | ||
var sec = 0 | ||
var tmpB = b | ||
|
||
for (i in 0 until n) { | ||
for (j in 0 until m) { | ||
if (ground[i][j] > x) { | ||
sec += (ground[i][j] - x) * 2 | ||
tmpB += (ground[i][j] - x) | ||
} | ||
else if (ground[i][j] < x) { | ||
sec += (x - ground[i][j]); | ||
tmpB -= (x - ground[i][j]); | ||
} | ||
} | ||
} | ||
|
||
if (tmpB >= 0 && sec <= ansSec) { | ||
ansSec = sec | ||
ansHigh = x | ||
} | ||
} | ||
print("$ansSec $ansHigh") | ||
} |
This file contains 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 @@ | ||
import java.lang.Math.max | ||
|
||
fun main() { | ||
val n = readln().toInt() | ||
val T = IntArray(n + 1) | ||
val P = IntArray(n + 1) | ||
val arr = IntArray(n + 2) | ||
|
||
for (i in 1 .. n) { | ||
val input = readln().split(" ").map{it.toInt()} | ||
T[i] = input[0] | ||
P[i] = input[1] | ||
} | ||
|
||
for (i in 1 .. n) { | ||
if (i + T[i] <= n + 1) { | ||
arr[i + T[i]] = max(arr[i + T[i]], arr[i] + P[i]) | ||
} | ||
arr[i + 1] = max(arr[i + 1], arr[i]) | ||
} | ||
println(arr[n + 1]) | ||
} |
This file contains 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,25 @@ | ||
var ans:Int = 0 | ||
|
||
fun main() { | ||
val (n, s) = readln().split(" ").map{it.toInt()} | ||
val arr = readln().split(" ").map{it.toInt()}.toIntArray() | ||
|
||
subSum(n, s, arr,0, 0, 0) | ||
|
||
if (s == 0) { | ||
ans-- | ||
} | ||
println(ans) | ||
} | ||
|
||
fun subSum(n: Int, s: Int, arr: IntArray, depth: Int, idx: Int, sum: Int) { | ||
if (depth == n) { | ||
if (sum == s) { | ||
ans++ | ||
} | ||
return | ||
} | ||
|
||
subSum(n, s, arr, depth + 1, idx + 1, sum + arr[idx]); | ||
subSum(n, s, arr, depth + 1, idx + 1, sum); | ||
} |