Day of the Week - Array - Easy - LeetCode
💻 coding

Day of the Week - Array - Easy - LeetCode

1 min read 254 words
1 min read
ShareWhatsAppPost on X
  • 1The function takes three integers representing day, month, and year to determine the corresponding day of the week.
  • 2Valid input dates range from the year 1971 to 2100.
  • 3The solution has a time complexity of O(1) and a space complexity of O(1).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The function takes three integers representing day, month, and year to determine the corresponding day of the week."

Day of the Week - Array - Easy - LeetCode

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Example 1:

Input: day = 31, month = 8, year = 2019

Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999

Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993

Output: "Sunday"

Constraints:

The given dates are valid dates between the years 1971 and 2100.

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class DayOfTheWeekSoln
 {
 public string DayOfTheWeek(int day, int month, int year)
 {
 var date = new DateTime(year, month, day);
 return date.DayOfWeek.ToString();
 }
 }
}

Time Complexity: O(1)

Space Complexity: O(1)

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class DayOfTheWeekSolnTests
 {
 [TestMethod]
 public void DayOfTheWeekSoln_First()
 {
 var day = 31;
 var month = 8;
 var year = 2019;

 var output = "Saturday";

 var res = new DayOfTheWeekSoln().DayOfTheWeek(day, month, year);

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void DayOfTheWeekSoln_Second()
 {
 var day = 18;
 var month = 7;
 var year = 1999;

 var output = "Sunday";

 var res = new DayOfTheWeekSoln().DayOfTheWeek(day, month, year);

 Assert.AreEqual(output, res);
 }

 [TestMethod]
 public void DayOfTheWeekSoln_Third()
 {
 var day = 15;
 var month = 8;
 var year = 1993;

 var output = "Sunday";

 var res = new DayOfTheWeekSoln().DayOfTheWeek(day, month, year);

 Assert.AreEqual(output, res);
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 12 June 2020 · 1 min read · 254 words

Part of AskGif Blog · coding

You might also like