From 7cf0b5d0fd454cacc3daa90ce0c1f7c9c7c7fd4c Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Sun, 27 Feb 2022 21:37:36 -0800 Subject: [PATCH] 228 --- .../README.md | 2 +- leetcode/228. Summary Ranges/README.md | 44 +++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/leetcode/2188. Minimum Time to Finish the Race/README.md b/leetcode/2188. Minimum Time to Finish the Race/README.md index 7431d2d5..f9be6ba5 100644 --- a/leetcode/2188. Minimum Time to Finish the Race/README.md +++ b/leetcode/2188. Minimum Time to Finish the Race/README.md @@ -72,7 +72,7 @@ public: int N = A.size(), len = 0; vector best(numLaps, LONG_MAX), dp(numLaps + 1, INT_MAX); for (int i = 0; i < N; ++i) { - long f = A[i][0], r = A[i][1], sum = change, p = 1; // We assume we also need `change` time to use the first tire so that we don't need to care the first tire as a special case + long f = A[i][0], r = A[i][1], sum = change, p = 1; // We assume we also need `change` time to use the first tire so that we don't need to treat the first tire as a special case for (int j = 0; j < numLaps; ++j) { sum += f * p; if (f * p >= f + change) break; // If using the same tire takes no less time than changing the tire, stop further using the current tire diff --git a/leetcode/228. Summary Ranges/README.md b/leetcode/228. Summary Ranges/README.md index c48cb19c..32a49e1b 100644 --- a/leetcode/228. Summary Ranges/README.md +++ b/leetcode/228. Summary Ranges/README.md @@ -33,24 +33,6 @@ [8,9] --> "8->9" -

Example 3:

- -
Input: nums = []
-Output: []
-
- -

Example 4:

- -
Input: nums = [-1]
-Output: ["-1"]
-
- -

Example 5:

- -
Input: nums = [0]
-Output: ["0"]
-
-

 

Constraints:

@@ -58,9 +40,13 @@
  • 0 <= nums.length <= 20
  • -231 <= nums[i] <= 231 - 1
  • All the values of nums are unique.
  • +
  • nums is sorted in ascending order.
  • +**Companies**: +[Yandex](https://leetcode.com/company/yandex), [Facebook](https://leetcode.com/company/facebook), [Google](https://leetcode.com/company/google) + **Related Topics**: [Array](https://leetcode.com/tag/array/) @@ -89,3 +75,25 @@ public: } }; ``` + +Or + +```cpp +// OJ: https://leetcode.com/problems/summary-ranges/ +// Author: github.com/lzl124631x +// Time: O(N) +// Space: O(N) +class Solution { +public: + vector summaryRanges(vector& A) { + vector ans; + int N = A.size(); + for (int i = 0; i < N; ++i) { + int start = A[i]; + while (i + 1 < N && A[i + 1] == A[i] + 1) ++i; + ans.push_back(to_string(start) + (A[i] == start ? "" : ("->" + to_string(A[i])))); + } + return ans; + } +}; +``` \ No newline at end of file