Total Years of Work Experience
Problem statement
A profile lists a user's jobs. experiences[i] = [start, end] means the user worked from month start through month end, both included. An end of "Present" means the job is ongoing and lasts through currentMonth.
Jobs may overlap, and a month in which the user held several jobs counts only once. Count the distinct months worked and return the total years of experience as a whole number: the number of months divided by 12, rounded down.
Function
totalYearsOfExperience(experiences: String[][], currentMonth: String) → intExamples
Example 1
experiences = [["2015-01","2017-12"],["2017-06","2019-12"],["2021-01","Present"]]currentMonth = "2026-06"return = 10- The first two jobs overlap, so together they cover January 2015 to December 2019: 60 months.
- The third job runs from January 2021 to the current month, June 2026: 66 months.
- The total is 126 months, which is 10 full years (126 / 12 rounded down).
Example 2
experiences = [["2020-03","2020-03"]]currentMonth = "2024-01"return = 0- One month of work is 0 full years.
Example 3
experiences = [["2010-01","2010-12"],["2011-01","2011-12"]]currentMonth = "2012-05"return = 2- Back-to-back jobs cover 24 months with no gap: 2 years.
Constraints
1 <= experiences.length <= 200experiences[i].length == 2- Every month is written
"YYYY-MM"with a year from1970to2099; an end may also be"Present". - For every job,
start <= end <= currentMonth(with"Present"read ascurrentMonth).