From 24c9ea91924a024321b752d54169d992057e0dcd Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Fri, 5 Jun 2026 01:15:24 +0530 Subject: [PATCH] feat: POTD 2026-06-04 solved --- src/3600-3699/3635.cpp | 2 +- src/3700-3799/3751.cpp | 154 +++++++++++++++++++++++++++++++++++++++ tests/3700-3799/3751.txt | 20 +++++ 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 src/3700-3799/3751.cpp create mode 100644 tests/3700-3799/3751.txt diff --git a/src/3600-3699/3635.cpp b/src/3600-3699/3635.cpp index 355430a..862dff7 100644 --- a/src/3600-3699/3635.cpp +++ b/src/3600-3699/3635.cpp @@ -6,7 +6,7 @@ array, greedy Problem Description -3635. Earliest Finish Time for Land and Water Rides I +3635. Earliest Finish Time for Land and Water Rides II You are given two categories of theme park attractions: land rides and water rides. Land rides diff --git a/src/3700-3799/3751.cpp b/src/3700-3799/3751.cpp new file mode 100644 index 0000000..8449825 --- /dev/null +++ b/src/3700-3799/3751.cpp @@ -0,0 +1,154 @@ +/* +Tags +level-medium +dp, digit-dp, math + + +Problem Description +3751. Total Waviness of Numbers in Range I +You are given two integers num1 and num2 representing an inclusive range [num1, num2]. +The waviness of a number is defined as the total count of its peaks and valleys: +A digit is a peak if it is strictly greater than both of its immediate neighbors. +A digit is a valley if it is strictly less than both of its immediate neighbors. +The first and last digits of a number cannot be peaks or valleys. +Any number with fewer than 3 digits has a waviness of 0. +Return the total sum of waviness for all numbers in the range [num1, num2]. + +Example 1: +Input: num1 = 120, num2 = 130 +Output: 3 +Explanation: +In the range [120, 130]: +120: middle digit 2 is a peak, waviness = 1. +121: middle digit 2 is a peak, waviness = 1. +130: middle digit 3 is a peak, waviness = 1. +All other numbers in the range have a waviness of 0. +Thus, total waviness is 1 + 1 + 1 = 3. + +Example 2: +Input: num1 = 198, num2 = 202 +Output: 3 +Explanation: +In the range [198, 202]: +198: middle digit 9 is a peak, waviness = 1. +201: middle digit 0 is a valley, waviness = 1. +202: middle digit 0 is a valley, waviness = 1. +All other numbers in the range have a waviness of 0. +Thus, total waviness is 1 + 1 + 1 = 3. + +Example 3: +Input: num1 = 4848, num2 = 4848 +Output: 2 +Explanation: +Number 4848: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2. + +Constraints: +1 <= num1 <= num2 <= 10^5 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../core/core.h" +using namespace std; + +using ll = long long; +class Solution { +private: + ll memoCount[16][10][10]; + ll memoSum[16][10][10]; + string s; +public: + pair dfs(int pos, int prev, int cur, bool isLimit, bool isLeading) + { + if (pos == s.length()) + { + return { 1,0 }; + } + + if (!isLimit && !isLeading && prev >= 0 && cur >= 0) + { + if (memoCount[pos][prev][cur] != -1) + { + return { memoCount[pos][prev][cur], memoSum[pos][prev][cur] }; + } + } + + ll count = 0; + ll sum = 0; + int limitDigit = isLimit ? (s[pos] - '0') : 9; + for (int digit = 0; digit <= limitDigit; digit++) + { + bool nextLeading = isLeading && (digit == 0); + int nextPrev = cur; + int nextCur = nextLeading ? -1 : digit; + bool nextLimit = isLimit && (digit == limitDigit); + + pairresult = this->dfs(pos + 1, nextPrev, nextCur, nextLimit, nextLeading); + ll subCount = result.first; + ll subSum = result.second; + + if (!nextLeading && prev >= 0 && cur >= 0) + { + bool isPeak = (prevdigit); + bool isTrough = (prev > cur && cur < digit); + + if (isPeak || isTrough) + { + sum += subCount; + } + } + + count += subCount; + sum += subSum; + } + + if (!isLimit && !isLeading && prev >= 0 && cur >= 0) + { + memoCount[pos][prev][cur] = count; + memoSum[pos][prev][cur] = sum; + } + + return { count, sum }; + } + ll solve(int num) + { + if (num < 100) + { + return 0; + } + s = to_string(num); + memset(memoCount, -1, sizeof(memoCount)); + memset(memoSum, -1, sizeof(memoSum)); + pair result = this->dfs(0, -1, -1, true, true); + return result.second; + } + int totalWaviness(int num1, int num2) { + return (int)(this->solve(num2) - this->solve(num1 - 1)); + } +}; + +int main() +{ + int num1, num2; + cin >> num1 >> num2; + Solution sol; + int result = sol.totalWaviness(num1, num2); + cout << result << "\n"; + return 0; +} \ No newline at end of file diff --git a/tests/3700-3799/3751.txt b/tests/3700-3799/3751.txt new file mode 100644 index 0000000..4451c12 --- /dev/null +++ b/tests/3700-3799/3751.txt @@ -0,0 +1,20 @@ +Test Case 1: +Input: +120 +130 +Output: +3 + +Test Case 2: +Input: +198 +202 +Output: +3 + +Test Case 3: +Input: +4848 +4848 +Output: +2 \ No newline at end of file