Skip to content
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

[Week1] 박서윤: 차이를 최대로, 부분수열의 합, 마인크래프트, 퇴사, 소수찾기 #5

Merged
merged 5 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/1week/seoyoon/FindingPrimeNum.kt
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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BooleanArray 기본값이 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
}
}
32 changes: 32 additions & 0 deletions src/1week/seoyoon/MaximizeDiff.kt
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
}
}
}
49 changes: 49 additions & 0 deletions src/1week/seoyoon/Minecraft.kt
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")
}
22 changes: 22 additions & 0 deletions src/1week/seoyoon/QuitTheJob.kt
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])
}
25 changes: 25 additions & 0 deletions src/1week/seoyoon/SumOfSubsequences.kt
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()}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.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);
}