230530_Tue

KH·2023년 5월 30일
0

TIL

목록 보기
4/58
post-thumbnail

Problems

  1. 웹개발 종합반 2일차

  2. 백준 요세푸스, 최소 힙, 약수

Attempts

  1. JavaScript 입문, jQuery 사용해보기, 서버-클라이언트 통신, fetch, json, api 알아보기

  2. LinkedList, PriorityQueue, Stream

Results

  1. 2일차 수강완료
      .bike_count_low{
            color: red;
            font-style: italic;
        }
function 함수이름() {
            fetch("URL넣으셈").then(res => res.json()).then(data => { 
                let rows = data['key이름1']['key이름2']
                $('#클래스이름').empty() // $('#')은 jQuery 문법임
                rows.forEach((a) => { // arrow function => 표기법에도 익숙해지자
                    let station_name = a['stationName']
                    let rack_total_count = a['rackTotCnt']
                    let parking_bike_total_count = a['parkingBikeTotCnt']
                    let row_to_add = ``

                    if(parking_bike_total_count<5){
                        row_to_add = `<tr class="bike_count_low">
                                        <td>${station_name}</td>
                                        <td>${rack_total_count}</td>
                                        <td>${parking_bike_total_count}</td>a
                                    </tr>`
                    }
                    else{
                        row_to_add = `<tr>
                                        <td>${station_name}</td>
                                        <td>${rack_total_count}</td>
                                        <td>${parking_bike_total_count}</td>a
                                    </tr>`
                    }
                    
                    $('#클래스이름').append(row_to_add)
                })
            })

        }

이러한 패턴은 앞으로 자주 볼 예정이라고 한다.


2.1. 요세푸스 문제는 LinkedList 클래스를 사용하여 K번째 전 K-1개의 원소들을 변수.offer(변수.poll())으로 조작함.

for (int i = 0; i < K - 1; i++) 리스트이름.offer(리스트이름.poll());

2.2. 최소 힙은 우선순위 큐로 최대 힙 구현할 때 사용한 Collections.reverseOrder()만 지우고 제출하여 통과.

//PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> pq = new PriorityQueue<>();

2.3. 약수는 3가지 방법이 있음

a. Arrays.stream(배열이름).min().getAsInt() 으로 min or max 값을 받을 수 있다.

b. min과 max를 result[0]으로 초기화한 뒤 루프에서 result[i]와 비교하며 갱신

String input = br.readLine();
String[] result = input.split(" ");
int min = Integer.parseInt(result[0]);
int max = Integer.parseInt(result[0]);
for (int i = 0; i < N; i++) {
	int val = Integer.parseInt(result[i]);
	if(max < val){ max = val;}
	if(min > val){ min = val;}
    }

c. min과 max를 각각 가능한 큰 값과 작은값으로 초기화후 루프에서 갱신

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

Insights

수일에 걸쳐 Stack, Queue, Deque, PriorityQueue, LinkedList를 다루어 보았다.
한편 LinkedList의 경우 Queue나 Deque에서 본 메서드들을 사용할 수 있었는데, 이는 LinkedList가 이들을 Implement하기 때문이다.
이전에 작성한 LinkedList 게시글에서 다음과 같은 Hierarchy를 조사한 적이 있었다.

오라클 문서에서도 확인 가능하다.
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html

예를 들어 LinkedList
List를 implement하므로 add() 를 사용할 수 있다.
Queue를 implement하므로 offer()도 사용할 수 있다.
Deque을 implement하므로 addFirst()도 사용할 수 있다.

한편 비슷한 동작을 하지만 이름이 다른 poll()remove()의 경우 Queue에서 정의되며 다음과 같은 차이가 있다.
remove throws an exception if this queue is empty.
pull returns null if this queue is empty.
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html

profile
What, How, Why

0개의 댓글