Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.21 KB

Q28.md

File metadata and controls

40 lines (32 loc) · 1.21 KB
public static void main(String[] args) {
        System.out.println(convertTo24HoursFormat("01:00"));
        System.out.println(convertTo24HoursFormat("02:00"));


        String[] a = {"01:00", "01:30", "02:00", "03:00", "03:30", "03:40"};
        System.out.println(solveQ4(3, 60, 6, a));
    }

    public static int convertTo24HoursFormat(String twelveHourTime) {
        List<String> a = Arrays.asList(twelveHourTime.split(":"));
        int time = Integer.parseInt(a.get(0)) * 60 + Integer.parseInt(a.get(1));
        return time;

    }

    public static String solveQ4(int X, int Y, int N, String[] alarmList) {
        String output = "";

        if (N < X) {
            output = "cannot wake!";

        } else if (X == 1) {
            System.out.println(alarmList[0]);
        } else {
            for (int i = 0; i < alarmList.length - X; i++) {
                if ((convertTo24HoursFormat(alarmList[i + X - 1]) - convertTo24HoursFormat(alarmList[i]) + 1) <= Y) {
                    output = alarmList[i + X - 1];
                    break;
                } else {
                    output = "cannot wake!";
                    break;
                }

            }
        }
        return output;
    }