18 년의 나
// 18년의 나.. C++
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int N= progresses.size();
bool publish[10000]={false};
bool flag= true;
while(flag)
{
int pubNum=0;
for(int i=0;i<N;i++)
{
if(!publish[i]){
progresses.at(i)+= speeds.at(i);
if(progresses.at(i)>=100){
if(i==0||publish[i-1]) { // 첫 또는 이전 원소가 출시되었을 때
pubNum++;
publish[i]=true;
}
}
}
}
if(pubNum>0)
answer.push_back(pubNum);
if(publish[N-1]) // the end.
flag= false;
}
return answer;
}
import java.util.*;
class Solution {
class Program{
int now;
int speed;
public Program(int now, int speed){
this.now = now;
this.speed = speed;
}
public void develop(){
now+= speed;
}
}
public int[] solution(int[] progresses, int[] speeds) {
Vector<Integer> v = new Vector();
Queue <Program> q = new LinkedList<>();
for(int i=0; i<progresses.length; i++){
q.offer(new Program(progresses[i], speeds[i]));
}
while (!q.isEmpty()){
int today =0;
while(true){
if(q.peek() !=null && q.peek().now >= 100){
q.poll();
today++;
}else{
break;
}
}
for(Program t : q){
t.develop();
}
if(today>0){
v.add(today);
}
}
int[] answer = new int[v.size()];
// answer = (int[])v.toArray(answer); // + 이러한 방법도 있음.. 하지만Integer, int 때문에 불가능한듯 하다
for(int i =0; i<answer.length;i++){
answer[i] = v.elementAt(i);
}
return answer;
}
}
과거 c++ 소스 보고 수정해봄
import java.util.*;
class Solution {
public int [] solution(int[] progresses, int[] speeds) {
Vector<Integer> v = new Vector();
int len = progresses.length;
boolean [] isPublished = new boolean [len*100];
while (true){
int today =0;
for(int i =0; i<progresses.length;i++){
if(!isPublished[i]){
progresses[i]+= speeds[i];
if( (progresses[i] >=100)){
if(i == 0){
isPublished[0] = true;
today++;
}else{
if(isPublished[i-1]){
isPublished[i] = true;
today++;
}
}
}
}
}
if(today>0){
v.add(today);
}
if(isPublished[len -1])
break;
}
int[] answer = new int[v.size()];
for(int i =0; i<answer.length;i++){
answer[i] = v.elementAt(i);
}
return answer;
}
}
'DEV > 그냥 알고리즘공부' 카테고리의 다른 글
C++ 백준 9095 (0) | 2021.03.20 |
---|