728x90
직사각형 넓이 구하기
import kotlin.math.absoluteValue
class Solution {
fun solution(dots: Array<IntArray>): Int {
var x = intArrayOf()
var y = intArrayOf()
for (i in dots){
x = x.plus(i[0])
y = y.plus(i[1])
}
x.sort()
y.sort()
val xLen = x[3] - x[0]
val yLen = y[3] - y[0]
return (xLen * yLen).absoluteValue
}
}
캐릭터의 좌표
class Solution {
fun solution(keyinput: Array<String>, board: IntArray): IntArray {
var x = 0
var y = 0
for (i in keyinput){
when(i){
"left" -> x -= 1
"right" -> x += 1
"up" -> y += 1
"down" -> y -= 1
}
if (x>board[0]/2) x = board[0]/2
if (x<-board[0]/2) x = -board[0]/2
if (y>board[1]/2) y = board[1]/2
if (y<-board[1]/2) y = -board[1]/2
}
return intArrayOf(x,y)
}
}
최댓값 만들기 (2)
class Solution {
fun solution(numbers: IntArray): Int {
numbers.sort()
var result1 = numbers[0] * numbers[1]
var result2 = numbers[numbers.size-1] * numbers[numbers.size-2]
if (result1 > result2) return result1
return result2
}
}
다항식 더하기
class Solution {
fun solution(polynomial: String): String {
var num = 0
var x = 0
var answer = ""
val poly = polynomial.split(" ")
for (i in poly){
if (i.contains("x")){
var temp = i.replace("x", "")
if (temp.length > 0) x += temp.toInt()
else x++
}else if (i != "+") num += i.toInt()
}
if (x > 0){
if (num > 0) {
if (x == 1) answer = "x + $num"
else answer = "${x}x + $num"
}else{
if (x == 1) answer = "x"
else answer = "${x}x"
}
}else{
if (num > 0) {
answer = "$num"
}
}
return answer
}
}
728x90