2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

paizaレベルアップ問題集 連休を伸ばす1 (paizaランク B 相当)

Posted at

paizaのレベルアップ問題集を解いた

自力で解いたコード
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int N = int.Parse(input[0]), L = int.Parse(input[1]); //N日のリスト、L日まで連休が使える
            input = Console.ReadLine().Split(' '); //1なら休業日、0なら営業日
            int max = 0;
            //連休の初日を1日ずつずらしながら、取れる休みを求める
            for(int i = 0; i < N; i++)
            {
                int temp = 0;
                int paidHolidays = L;
                for(int j = i; j < N; j++)
                {
                    if(input[j] == "1")
                    {
                        temp++;
                    }else if(paidHolidays > 0) { //有休が残っている場合
                        temp++;
                        paidHolidays--;
                    }
                    else
                    {
                        break;
                    }
                }
                if(temp > max)
                {
                    max = temp;
                }
            }
            Console.WriteLine(max);
        }
    }
youtubeの解説動画を視て書いたコード

    class Program
    {
        static int howManyHolidays(int firstDay,int N,int L, int[] list)
        {
            //元々休みの日と有休をとった日の合計を求める
            int one = 0;
            int zero = 0;
            while(firstDay + one + zero < N)
            {
                if(list[firstDay + one + zero] == 1)
                {
                    one++;
                }
                else
                {
                    if(zero == L)
                    {
                        break;
                    }
                    zero++;
                }
            }
            return one + zero;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int N = int.Parse(input[0]), L = int.Parse(input[1]); //N日のリスト、L日まで連休が使える
            input = Console.ReadLine().Split(' '); //1なら休業日、0なら営業日
            int[] list = new int[N];
            for(int i = 0; i < N; i++)
            {
                list[i] = int.Parse(input[i]);
            }
            int max = 0;
            for(int firstDay = 0; firstDay < N; firstDay++)
            {
                int temp = howManyHolidays(firstDay, N,L,list);
                if(temp > max)
                {
                    max = temp;
                }
            }
            Console.WriteLine(max);
        }
    }
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?