-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bd2be0
commit 4507db1
Showing
15 changed files
with
147 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { dayOfYear, dayOfYear2 } from './dayOfYear'; | ||
|
||
describe('1154. Day of the Year', () => { | ||
test('dayOfYear', () => { | ||
expect(dayOfYear('2019-01-09')).toBe(9); | ||
expect(dayOfYear('2019-02-10')).toBe(41); | ||
}); | ||
|
||
test('dayOfYear2', () => { | ||
expect(dayOfYear2('2019-01-09')).toBe(9); | ||
expect(dayOfYear2('2019-02-10')).toBe(41); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
type DayOfYear = (date: string) => number; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const dayOfYear: DayOfYear = (date) => { | ||
const target = new Date(date); | ||
const start = new Date(target.getFullYear(), 0, 0); | ||
const diff = target.getTime() - start.getTime(); | ||
return Math.floor(diff / (24 * 60 * 60 * 1000)); | ||
}; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const dayOfYear2: DayOfYear = (date) => { | ||
const [yearStr, monthStr, dayStr] = date.split('-'); | ||
const [year, month, day] = [Number(yearStr), Number(monthStr), Number(dayStr)]; | ||
|
||
// Array with days in each month for a non-leap year | ||
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | ||
|
||
// Check if the year is a leap year | ||
const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; | ||
|
||
// If it's a leap year, February has 29 days | ||
if (isLeapYear) daysInMonth[1] = 29; | ||
|
||
// Calculate the day of year | ||
let dayOfYear = day; | ||
|
||
for (let i = 0; i < month - 1; i++) { | ||
dayOfYear += daysInMonth[i]; | ||
} | ||
|
||
return dayOfYear; | ||
}; |
2 changes: 1 addition & 1 deletion
2
...rangements/num-prime-arrangements.test.ts → ...Arrangements/numPrimeArrangements.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
...me Arrangements/num-prime-arrangements.ts → ...rime Arrangements/numPrimeArrangements.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
src/page-11/1176. Diet Plan Performance/diet-plan-performance.test.ts
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
src/page-11/1176. Diet Plan Performance/diet-plan-performance.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { dayOfTheWeek, dayOfTheWeek2 } from './dayOfTheWeek'; | ||
|
||
describe('1185. Day of the Week', () => { | ||
test('dayOfTheWeek', () => { | ||
expect(dayOfTheWeek(31, 8, 2019)).toBe('Saturday'); | ||
expect(dayOfTheWeek(18, 7, 1999)).toBe('Sunday'); | ||
expect(dayOfTheWeek(15, 8, 1993)).toBe('Sunday'); | ||
}); | ||
|
||
test('dayOfTheWeek2', () => { | ||
expect(dayOfTheWeek2(31, 8, 2019)).toBe('Saturday'); | ||
expect(dayOfTheWeek2(18, 7, 1999)).toBe('Sunday'); | ||
expect(dayOfTheWeek2(15, 8, 1993)).toBe('Sunday'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
type DayOfTheWeek = (day: number, month: number, year: number) => string; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const dayOfTheWeek: DayOfTheWeek = (day, month, year) => { | ||
const weekList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
return weekList[new Date(`${year}-${month}-${day}`).getDay()]; | ||
}; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const dayOfTheWeek2: DayOfTheWeek = (day, month, year) => { | ||
const weekList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; | ||
|
||
// Array with days in each month for a non-leap year | ||
const daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | ||
|
||
// Check if the year is a leap year | ||
function isLeapYear(year: number): boolean { | ||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; | ||
} | ||
|
||
// Number of days from reference day (Jan 1, 1971 which was a Friday) | ||
let daysPassed = 0; | ||
|
||
// Calculate days passed from 1971 to the start of given year | ||
for (let y = 1971; y < year; y++) { | ||
daysPassed += isLeapYear(y) ? 366 : 365; | ||
} | ||
|
||
// Calculate days passed in the current year up to the given month | ||
for (let m = 0; m < month - 1; m++) { | ||
daysPassed += daysInMonths[m]; | ||
} | ||
|
||
// Add days in the current month | ||
daysPassed += day - 1; | ||
|
||
// Adjust for leap year if current year is leap and after February | ||
if (month > 2 && isLeapYear(year)) daysPassed += 1; | ||
|
||
// Calculate day of the week using reference day and days passed | ||
const dayIndex = (daysPassed + 5) % 7; // January 1, 1971 was a Friday, so 5 is offset | ||
if (dayIndex < 0) return weekList[dayIndex + 7]; | ||
return weekList[dayIndex]; | ||
}; |
20 changes: 0 additions & 20 deletions
20
src/page-12/1232. Check If It Is a Straight Line/check-straight-line.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...Straight Line/check-straight-line.test.ts → ...a Straight Line/checkStraightLine.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/page-12/1232. Check If It Is a Straight Line/checkStraightLine.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
type CheckStraightLine = (coordinates: number[][]) => boolean; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const checkStraightLine: CheckStraightLine = (coordinates) => { | ||
// If there are less than 2 points, cannot form a line | ||
if (coordinates.length < 2) return false; | ||
|
||
// Extract first two points | ||
const [x1, y1] = coordinates[0]; | ||
const [x2, y2] = coordinates[1]; | ||
|
||
// Check if all points lie on the same line | ||
for (let i = 2; i < coordinates.length; i++) { | ||
const [x, y] = coordinates[i]; | ||
|
||
// Calculate slope of the line formed by (x1, y1) and (x2, y2) | ||
// slope = (y2 - y1) / (x2 - x1) | ||
// Avoid division by zero by checking x2 - x1 | ||
if ((y2 - y1) * (x - x1) !== (y - y1) * (x2 - x1)) return false; | ||
} | ||
|
||
// All points are on the same line | ||
return true; | ||
}; |