본문 바로가기
Programming/Algorithm

[leetcode] CourseSchedule

by 읽고 쓰는 개발자 2020. 11. 29.

https://leetcode.com/problems/course-schedule/

 

Course Schedule - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

import java.util.ArrayList;
import java.util.List;

public class CourseSchedule {

    public static void main(String[] args) {
        System.out.println(canFinish(2,new int[][] {{0,1}, {1,0}}));
        System.out.println(canFinish(6,new int[][] {{0,1}, {2,3}, {3,5}, {4,5}, {4,2}, {3,4}}));
    }


    static boolean answerFlag ;
    public static boolean canFinish(int numCourses, int[][] prerequisites) {
        boolean answer = true;
        boolean[] flag = new boolean[numCourses];
        List<List<Integer>> courseRelList = new ArrayList<>();
        for (int i = 0; i < numCourses; i++) {
            List<Integer> list = new ArrayList<>();
            courseRelList.add(list);
        }


        for (int[] nowCourse : prerequisites) {
            int postCourse = nowCourse[0];
            int preCourse = nowCourse[1];
            answerFlag = true;
            checkCanFinish(preCourse,courseRelList, postCourse);
            if (!answerFlag) return false;
            else courseRelList.get(postCourse).add(preCourse);
        }

        return answer;
    }

    private static void checkCanFinish(int preCourse, List<List<Integer>> courseRelList, int target) {
        List<Integer> nowList = courseRelList.get(preCourse);
        for (int i : nowList) {
            if (i == target) answerFlag = false;
            checkCanFinish(i, courseRelList, target);
        }
    }
}

'Programming > Algorithm' 카테고리의 다른 글

[leetcode] GameofLife  (0) 2020.11.29
[leetcode] DiameterofBinaryTree  (0) 2020.11.29
[leetcode] CombinationSum  (0) 2020.11.29
[leetcode] WordSearchII  (0) 2020.11.29
[leetcode] WordSearch  (0) 2020.11.29