샌드위치와 학생배열중 맨앞이 같으면 제거,다르면 학생배열의 맨앞을 맨뒤로 넣음
이렇게 학생배열 한바퀴를 돌아도 샌드위치를 하나못없앴으면 학생배열남은거 리턴
정답
class Solution {
fun countStudents(students: IntArray, sandwiches: IntArray): Int {
val sandwichList = sandwiches.toMutableList()
val studentList = students.toMutableList()
var count=0
while (true){
if (sandwichList.isEmpty()){
break
}
if (sandwichList[0]==studentList[0]){
sandwichList.removeAt(0)
studentList.removeAt(0)
count=0
continue
}
count+=1
if (count==studentList.size){
return studentList.size
}
val tempInt = studentList.removeAt(0)
studentList.add(tempInt)
}
return 0
}
}
'알고리즘' 카테고리의 다른 글
릿코드 950. Reveal Cards In Increasing Order 코틀린 (0) | 2024.04.11 |
---|---|
릿코드 2073. Time Needed to Buy Tickets 코틀린 (0) | 2024.04.09 |
릿코드 678. Valid Parenthesis String 코틀린 (0) | 2024.04.07 |
릿코드 1249. Minimum Remove to Make Valid Parentheses 코틀린 (0) | 2024.04.06 |
릿코드 1544. Make The String Great 코틀린 (0) | 2024.04.05 |