Skip to content
Open
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
7 changes: 0 additions & 7 deletions week2/3. 주식가격/Solution.java

This file was deleted.

91 changes: 91 additions & 0 deletions week2/3. 주식가격/Solution2_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
3. 주식 가격
https://programmers.co.kr/learn/courses/30/lessons/42584
*/
public class Solution2_3 {
public static void main(String[] args) {
int[] prices = { 1, 2, 3, 2, 3 };

System.out.println(Arrays.toString(solution(prices)));
}

public static int[] solution(int[] prices) {

int[] timeCountArray = new int[prices.length];

for (int i = 0; i < prices.length; i++) {
int timer = 0;

for (int j = i + 1; j < prices.length; j++) {

timer++;
timeCountArray[i] = timer;

if (prices[i] > prices[j])
break;

}
}

return timeCountArray;
}

// 처음 문제 풀이 방법
// public static int[] solution(int[] prices) {

// List<Price> priceList = new ArrayList<>();

// for (int i = 0; i < prices.length; i++) {

// for (Price price : priceList) {
// price.comparePriceAndAddTime(prices[i]);
// }

// priceList.add(new Price(prices[i]));
// }

// return priceList.stream().mapToInt(price -> price.getTime()).toArray();
// }

}

class Price {

private int price;
private int time;
private boolean isStopTimer;

public int getTime() {
return this.time;
}

Price(int price) {
this.price = price;
this.time = 0;
this.isStopTimer = false;
}

public void comparePriceAndAddTime(int otherPrice) {

if (this.isStopTimer) {
return;
}

if (this.price <= otherPrice) {
this.time++;
return;
}

if (this.price > otherPrice) {
this.time++;
this.isStopTimer = true;
return;
}

}

}