diff --git a/.gitignore b/.gitignore index 5599549..848b94c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ .obsidian seafile-ignore.txt +.venv +config/ +output/ +index.md diff --git a/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock II.md b/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock II.md new file mode 100644 index 0000000..82da047 --- /dev/null +++ b/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock II.md @@ -0,0 +1,20 @@ +Leetcode #122 | Medium | [[1-D Динамика]] +## Идея +Продолжение задачи [[Best Time to Buy and Sell Stock]], только теперь можно покупать и выкупать сколько угодно раз, главное, чтобы не было пересечений. Идея: для монотонного массива прибыль будет такая же, как прибыль при покупке/продаже каждый день. То есть `[1, 2, 5] - 5-1=4` то же, что и `2-1 + 5-2 = 4`. Тогда просто идем по массиву парами и если есть прибыль - берем к себе +## [[Big-O]] +- Время ```O(N)``` +- Память ```O(1)``` +## Код +```Java +class Solution { + public int maxProfit(int[] prices) { + int res = 0; + for (int i = 1; i < prices.length; i++) { + if (prices[i] > prices[i-1]) { + res += prices[i]-prices[i-1]; + } + } + return res; + } +} +``` \ No newline at end of file diff --git a/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock.md b/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock.md new file mode 100644 index 0000000..1f596e7 --- /dev/null +++ b/АиСД/Задачи/LeetCode/Best Time to Buy and Sell Stock.md @@ -0,0 +1,25 @@ +Leetcode #121 | Easy | [[1-D Динамика]] +## Идея +Ставим левый указатель на 0 и идем по массиву правым. Если цена на левом меньше цены на правом, то можно зафиксировать прибыль - обновляем результат. Если же цена стала меньше либа равна, то двигаем левый указатель на место правого. Мы не может потерять никаких промежуточных сумм на этом действии, потому что в силу монотонности, никаие значения на (l,r) не удовлетворяли `p[l]
nums2.length) { + return findMedianSortedArrays(nums2, nums1); + } + int m = nums1.length; + int n = nums2.length; + int halfLen = (m+n+1) / 2; + + int l = -1; + int r = m+1; + + while (r-l > 1) { + int i = (l+r)/2; + int j = halfLen - i; + + int Aleft = (i==0) ? Integer.MIN_VALUE : nums1[i-1]; + int Bright = (j == n) ? Integer.MAX_VALUE : nums2[j]; + + if (Aleft > Bright) { + r = i; + } else { + l = i; + } + } + + int i = l; + int j = halfLen - i; + + int Aleft = (i == 0) ? Integer.MIN_VALUE : nums1[i - 1]; + int Aright = (i == m) ? Integer.MAX_VALUE : nums1[i]; + int Bleft = (j == 0) ? Integer.MIN_VALUE : nums2[j - 1]; + int Bright = (j == n) ? Integer.MAX_VALUE : nums2[j]; + + if ((m + n) % 2 == 1) { + return Math.max(Aleft, Bleft); + } else { + return (Math.max(Aleft, Bleft) + Math.min(Aright, Bright)) / 2.0; + } + } +} +``` \ No newline at end of file diff --git a/АиСД/Список задач.md b/АиСД/Список задач.md index a5f7721..6c0ab0f 100644 --- a/АиСД/Список задач.md +++ b/АиСД/Список задач.md @@ -43,7 +43,7 @@ [[Intersection of Two Arrays II]] [[Missing Number]] [[Evaluate Reverse Polish Notation]] -Median of Two Sorted Arrays +[[Median of Two Sorted Arrays]] !!!!!!!!!!! [[Simplify Path]] [[Is Subsequence]] [[Squares of a Sorted Array]] @@ -69,4 +69,6 @@ Maximal Rectangle [[Daily Temperatures]] [[Course Schedule]] [[Maximum Subarray]] -[[Course Schedule II]] \ No newline at end of file +[[Course Schedule II]] +[[Best Time to Buy and Sell Stock]] +[[Best Time to Buy and Sell Stock II]] \ No newline at end of file diff --git a/Итмо/Предметы/ВТП.md b/Итмо/Предметы/ВТП.md index fc7ddf1..fa40f72 100644 --- a/Итмо/Предметы/ВТП.md +++ b/Итмо/Предметы/ВТП.md @@ -1 +1,7 @@ -Еще не было \ No newline at end of file +80 баллов до экзамена +экзамен 20 балло в формате квиза +лекции - 24 балла - тесты (внезапные) +практика - 56 баллов +oleg-bichurin@yandex.ru +[материалы лекций](https://drive.google.com/drive/mobile/folders/1BxqAbog16fqChobMVVQNSOb1plYZOItG) +[материалы практик](https://drive.google.com/drive/mobile/folders/14YUIpQ9tNZsPXgcBUL7ToRLCgEKINhHU?usp=drive_link)