반응형

Props

📢 Props란 부모 컴포넌트에서 자식 컴포넌트로 데이터를 전달할때 사용되는 단방향 데이터 전달 방식이며
반대 방향으로는 전달되지 않습니다.

또한, 부모 컴포넌트가 업데이트 될 때 마다 자식 요소 prop들도 최신 값으로 업데이트 됩니다.

<div id="app">
    <!-- <app-header v-bind:프롭스 속성 이름="상위 컴포넌트의 데이터이름"></app-header> -->
    <app-header v-bind:propsdata="nameText"></app-header>
</div>

// 자식 컴포넌트
var appHeader = {
    template: '<h1>{{propsdata}}</h1>',
    props: ['propsdata']
}

// 부모 컴포넌트
// 
new Vue({
    el: '#app',
    components: {
        'app-header': appHeader
    },
    nameText: "blackmount22"
})

부모 컴포넌트인 Root 컴포넌트에는 nameText라는 데이터명으로 “blackmount22” 문자열을 갖고 있고, 이를 자식 컴포넌트인 AppHeader 컴포넌트에 데이터를 전달할 수 있습니다.

아래 소스에서 부모 컴포넌트의 nameText 데이터를 자식 컴포넌트의 프롭스 속성 이름인 propsdata에 바인딩 하는 부분입니다.

<app-header v-bind:propsdata="nameText"></app-header>

상위 컴포넌트 (Root 컴포넌트)

하위 컴포넌트 (AppHeader 컴포넌트)

Root 컴포넌트의 nameText: “blackmount22” 값이 AppHeader 컴포넌트의 propsdata에 들어간 것을 확인 할 수 있습니다.

참고

https://kr.vuejs.org/v2/guide/components.html

반응형

'프로그래밍 > Vue' 카테고리의 다른 글

[Vue3] onMounted  (0) 2024.05.14
[Vue3] Computed  (0) 2024.04.26
Vue.js 2.0 라이프 사이클  (0) 2024.02.21
구구단 게임  (0) 2020.07.13
반응형

시작점에서 끝점까지 도달하는 경로의 개수를 세는 문제로 깊이 우선 탐색 알고리즘으로 풀이(DFS)

하지만, DFS로만 풀게 되면 시간 초과가 발생한다.

이를 위해 이미 탐색해서 경로의 개수가 파악된 경로를 재 탐색하는 것을 방지하기 위해 다이나믹 프로그래밍 알고리즘을 이용한다.

  • dp[x][y]는 최종 경로까지 이동 경로의 개수를 더하며, 이미 최종 경로까지의 이동 경로가 정해진 경우 dp 값을 활용하여 탐색 없이 이동 경로의 개수만 구할 수 있도록 처리한다.
// input
2 2 
20 10
12 7

// 배열[x][y] 부터 배열[m-1][n-1] 까지의 이동 경로 개수(dp[x][y])는 아래와 같다
2 1
1 1
import java.util.Scanner;

public class Main {

    static int n;
    static int m;
    static int[][] map;
    static int[][] visit;
    static int[][] dp;

    static int[] xx = {-1, 0, 1, 0};
    static int[] yy = {0, 1, 0, -1};

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);

        m = sc.nextInt();
        n = sc.nextInt();

        map = new int[m][n];
        visit = new int[m][n];
        dp = new int[m][n];

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                map[i][j] = sc.nextInt();
            }
        }

        dp[m-1][n-1] = 1;
        getDFS(0,0);

        System.out.println(dp[0][0]);

    }

    public static void getDFS(int x, int y) {
        if(x == m-1 && y == n-1) return;

        if(visit[x][y] == 1) return;

        visit[x][y] = 1;

        for(int i=0;i<4;i++) {
            int ax = x+xx[i];
            int ay = y+yy[i];

            if(ax >= 0 && ax < m && ay >= 0 && ay<n && map[x][y] > map[ax][ay]) {
                if(dp[ax][ay] != 0) {
                    dp[x][y] += dp[ax][ay];
                    continue;
                } else {
                    getDFS(ax,ay);
                    dp[x][y] += dp[ax][ay];
                }
            }
        }
    }

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #2869 달팽이는 올라가고 싶다  (0) 2022.07.04
[Baekjoon] #10872 팩토리얼  (0) 2022.07.03
[Baekjoon] #11651 좌표 정렬하기2  (0) 2022.06.28
[Baekjoon] #10815 숫자 카드  (0) 2022.06.22
[Baekjoon] #10828 스택  (0) 2022.06.22
반응형

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const n = input.shift();
const arr = [];

for(let i=0;i<n; i++){
    arr.push(input[i].split(' ').map(strNum => parseInt(strNum)));
}

let result = '';

arr.sort((a, b) => {
    if(a[1] !== b[1]) {
        return a[1] - b[1];
    }
    return a[0] - b[0];
})
.forEach(ar => (result += `${ar[0]} ${ar[1]}\n`));

console.log(result);
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #10872 팩토리얼  (0) 2022.07.03
[Baekjoon] #1520 내리막 길  (0) 2022.06.29
[Baekjoon] #10815 숫자 카드  (0) 2022.06.22
[Baekjoon] #10828 스택  (0) 2022.06.22
[Baekjoon] #10989 수 정렬하기3  (0) 2022.06.22
반응형

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	static ArrayList<Integer> arr = new ArrayList<Integer>();
	static ArrayList<Integer> result = new ArrayList<Integer>();
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		for(int i=0;i<n;i++) {
			int number = sc.nextInt();
			arr.add(number);
		}
		
		Collections.sort(arr);
		int m = sc.nextInt();
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		for(int i=0;i<m;i++) {
			int number = sc.nextInt();
			numbers.add(number);
		}
		
		for(int i=0;i<numbers.size();i++) {
			BinarySearch(numbers.get(i), 0, arr.size());
			System.out.print(result.get(i)+ " ");
		}
	}
	
	public static void BinarySearch(int target, int left, int right) {
		if(left > right) {
			result.add(0);
			return;
		}
		
		int mid = (left+right) / 2;
		
		if(mid >= arr.size()) {
			result.add(0);
			return;
		}
		
		if(arr.get(mid) == target) {
			result.add(1);
			return;
		}
		else if(arr.get(mid) > target) {
			BinarySearch(target, left, mid-1);
		} 
		else {
			BinarySearch(target, mid+1, right);
		}
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #1520 내리막 길  (0) 2022.06.29
[Baekjoon] #11651 좌표 정렬하기2  (0) 2022.06.28
[Baekjoon] #10828 스택  (0) 2022.06.22
[Baekjoon] #10989 수 정렬하기3  (0) 2022.06.22
[Baekjoon] #11047 동전 0  (0) 2022.06.22
반응형

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		Stack<Integer> stack = new Stack<>();
		StringBuilder sb = new StringBuilder();
		
		int n = sc.nextInt();
		for(int i=0;i<n;i++) {
			String command = sc.next();
			
			if(command.equals("push")) {
				int value = sc.nextInt();
				stack.push(value);
			} else if(command.equals("pop")){
				if(stack.empty()) {
					sb.append(-1 +"\n");
				} else {
					sb.append(stack.pop()+"\n");
				}
			} else if(command.equals("size")) {
				sb.append(stack.size()+"\n");
			} else if(command.equals("empty")) {
				if(stack.empty()) {
					sb.append(1 +"\n");
				} else {
					sb.append(0 +"\n");
				}
			} else if(command.equals("top")) {
				if(stack.empty()) {
					sb.append(-1 +"\n");
				} else {
					sb.append(stack.peek()+"\n");
				}
			}
		}
		
		System.out.println(sb);
	}

}
반응형

'알고리즘 > Baekjoon' 카테고리의 다른 글

[Baekjoon] #11651 좌표 정렬하기2  (0) 2022.06.28
[Baekjoon] #10815 숫자 카드  (0) 2022.06.22
[Baekjoon] #10989 수 정렬하기3  (0) 2022.06.22
[Baekjoon] #11047 동전 0  (0) 2022.06.22
[Baekjoon] #11050 이항 계수 1  (0) 2022.06.22

+ Recent posts