{"ScriptPreparationCode":" const expenses = {\r\n \u00272023-01\u0027: {\r\n \u002701\u0027: {\r\n food: [22.11, 43, 11.72, 2.2, 36.29, 2.5, 19],\r\n fuel: [210.22],\r\n },\r\n \u002709\u0027: {\r\n food: [11.9],\r\n fuel: [190.22],\r\n },\r\n },\r\n \u00272023-03\u0027: {\r\n \u002707\u0027: {\r\n food: [20, 11.9, 30.2, 11.9],\r\n },\r\n \u002704\u0027: {\r\n food: [10.2, 11.5, 2.5],\r\n fuel: [],\r\n },\r\n },\r\n \u00272023-04\u0027: {},\r\n };\r\n\r\n function getFirstSunday(monthOfYear) {\r\n let firstSunday = 1;\r\n const weekday = new Date(\u0060${monthOfYear}-01\u0060).getDay();\r\n if (weekday !== 0) {\r\n // Offsetujemy dzie\u0144, bo getDay() zwraca 0 dla niedzieli\r\n firstSunday = 8 - weekday;\r\n }\r\n return firstSunday;\r\n }\r\n\r\n function solution1(expenses) {\r\n let result = null;\r\n let allExpenses = [];\r\n\r\n for (const monthOfYear in expenses) {\r\n const month = expenses[monthOfYear];\r\n const daysWithExpenses = Object.keys(month).map(Number);\r\n const firstSunday = getFirstSunday(monthOfYear);\r\n const daysUpToFirstSunday = daysWithExpenses.filter(\r\n day =\u003E day \u003C= firstSunday\r\n );\r\n if (daysUpToFirstSunday.length === 0) continue;\r\n for (const day of daysUpToFirstSunday) {\r\n const allDayExpenses = month[\u00600${day}\u0060];\r\n for (const dayExpenses of Object.values(allDayExpenses)) {\r\n for (const expense of dayExpenses) {\r\n allExpenses.push(expense);\r\n }\r\n }\r\n }\r\n }\r\n // Je\u015Bli nie ma wydatk\u00F3w to nie ma z czego liczy\u0107 median\u0119, wi\u0119c zwracamy null\r\n if (allExpenses.length === 0) return result;\r\n allExpenses.sort((a, b) =\u003E a - b);\r\n\r\n const midIndex = Math.floor(allExpenses.length / 2);\r\n if (allExpenses.length % 2 === 0) {\r\n const midValue1 = allExpenses[midIndex - 1];\r\n const midValue2 = allExpenses[midIndex];\r\n result = (midValue1 \u002B midValue2) / 2;\r\n } else {\r\n result = allExpenses[midIndex];\r\n }\r\n\r\n return result;\r\n }\r\n\r\n function findMedian(arr) {\r\n const midIndex = Math.floor(arr.length / 2);\r\n\r\n if (arr.length % 2 === 1) return quickSelect(arr, midIndex);\r\n return (\r\n (quickSelect(arr, midIndex - 1) \u002B quickSelect(arr, midIndex)) / 2\r\n );\r\n }\r\n\r\n function quickSelect(arr, k) {\r\n return quickSelectHelper(arr, 0, arr.length - 1, k);\r\n }\r\n\r\n function quickSelectHelper(arr, left, right, k) {\r\n if (left === right) return arr[left];\r\n\r\n const pivotIndex = partition(arr, left, right);\r\n\r\n if (k === pivotIndex) return arr[k];\r\n if (k \u003C pivotIndex)\r\n return quickSelectHelper(arr, left, pivotIndex - 1, k);\r\n return quickSelectHelper(arr, pivotIndex \u002B 1, right, k);\r\n }\r\n\r\n function partition(arr, left, right) {\r\n const pivot = arr[right];\r\n let i = left - 1;\r\n\r\n for (let j = left; j \u003C right; j\u002B\u002B) {\r\n if (arr[j] \u003C= pivot) {\r\n i\u002B\u002B;\r\n [arr[i], arr[j]] = [arr[j], arr[i]];\r\n }\r\n }\r\n\r\n [arr[i \u002B 1], arr[right]] = [arr[right], arr[i \u002B 1]];\r\n return i \u002B 1;\r\n }\r\n\r\n function solution2(expenses) {\r\n let result = null;\r\n let allExpenses = [];\r\n\r\n for (const monthOfYear in expenses) {\r\n const month = expenses[monthOfYear];\r\n const firstSunday = getFirstSunday(monthOfYear);\r\n\r\n for (const day in month) {\r\n if (Number(day) \u003E firstSunday) continue;\r\n const allDayExpenses = month[day];\r\n for (const dayExpenses of Object.values(allDayExpenses)) {\r\n if (dayExpenses.length === 0) continue;\r\n for (const expense of dayExpenses) {\r\n allExpenses.push(expense);\r\n }\r\n }\r\n }\r\n }\r\n // Je\u015Bli nie ma wydatk\u00F3w to nie ma z czego liczy\u0107 median\u0119, wi\u0119c zwracamy null\r\n if (allExpenses.length === 0) return null;\r\n\r\n // Kalkulujemy median\u0119 u\u017Cywaj\u0105c algorytmu Quick Select\r\n // Obra\u0142em dan\u0105 metodologi\u0119, gdy\u017C jest to algorytm najcz\u0119\u015Bciej operuj\u0105cy ze z\u0142o\u017Cono\u015Bci\u0105 O(n)\r\n // Jest to zatem bardziej optymalny spos\u00F3b, ni\u017C zwyk\u0142e sortowanie arraya i wybranie z niego \u015Brodkowych element\u00F3w\r\n // Minusem jest nieco bardziej skomplikowany kod oraz rzadki przypadek, w kt\u00F3rym z\u0142o\u017Cono\u015B\u0107 algorytmu wynosi O(n^2)\r\n result = findMedian(allExpenses);\r\n return result;\r\n }","TestCases":[{"Name":"S1","Code":"solution1(expenses);","IsDeferred":false},{"Name":"S2","Code":"solution2(expenses);","IsDeferred":false}]}