Skip to content

Commit cc065e5

Browse files
committed
1878-CHECK-IF-ARRAY-IS-SORTED-AND-ROTATED Stats: Time: 0 ms (100%), Space: 11.3 MB (6.53%) - LeetHub
1 parent 2c88a80 commit cc065e5

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
bool check(vector<int>& nums) {
4+
int count = 0, n = nums.size();
5+
for (int i = 0; i < n; i++)
6+
if (nums[i] > nums[(i+1)%n] && ++count > 1) return false;
7+
return true;
8+
}
9+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h2><a href="https://leetcode.com/problems/check-if-array-is-sorted-and-rotated">1878. Check if Array Is Sorted and Rotated</a></h2><h3>Easy</h3><hr><p>Given an array <code>nums</code>, return <code>true</code><em> if the array was originally sorted in non-decreasing order, then rotated <strong>some</strong> number of positions (including zero)</em>. Otherwise, return <code>false</code>.</p>
2+
3+
<p>There may be <strong>duplicates</strong> in the original array.</p>
4+
5+
<p><strong>Note:</strong> An array <code>A</code> rotated by <code>x</code> positions results in an array <code>B</code> of the same length such that <code>A[i] == B[(i+x) % A.length]</code>, where <code>%</code> is the modulo operation.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [3,4,5,1,2]
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong> [1,2,3,4,5] is the original sorted array.
14+
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> nums = [2,1,3,4]
21+
<strong>Output:</strong> false
22+
<strong>Explanation:</strong> There is no sorted array once rotated that can make nums.
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> nums = [1,2,3]
29+
<strong>Output:</strong> true
30+
<strong>Explanation:</strong> [1,2,3] is the original sorted array.
31+
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
39+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
40+
</ul>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ this is readme. here i will make
1515
| [1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid](https://github.com/TheVinaySagar/Leetcode/tree/master/1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid) |
1616
| [1524-string-matching-in-an-array](https://github.com/TheVinaySagar/Leetcode/tree/master/1524-string-matching-in-an-array) |
1717
| [1876-map-of-highest-peak](https://github.com/TheVinaySagar/Leetcode/tree/master/1876-map-of-highest-peak) |
18+
| [1878-check-if-array-is-sorted-and-rotated](https://github.com/TheVinaySagar/Leetcode/tree/master/1878-check-if-array-is-sorted-and-rotated) |
1819
| [1895-minimum-number-of-operations-to-move-all-balls-to-each-box](https://github.com/TheVinaySagar/Leetcode/tree/master/1895-minimum-number-of-operations-to-move-all-balls-to-each-box) |
1920
| [2145-grid-game](https://github.com/TheVinaySagar/Leetcode/tree/master/2145-grid-game) |
2021
| [2292-counting-words-with-a-given-prefix](https://github.com/TheVinaySagar/Leetcode/tree/master/2292-counting-words-with-a-given-prefix) |

stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"leetcode":{"shas":{"README.md":{"":"c003e4f1f2b3a1d55cb088c833d7104fdf20df39"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"","difficulty":"medium"}},"solved":39,"easy":8,"medium":24,"hard":7}}
1+
{"leetcode":{"shas":{"README.md":{"":"5de96da35de469b0800cb4a8e0761096bd7cb322"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"2c88a80dfe972059ae86c77fdc3f00c62982cd4a","difficulty":"medium"},"1878-check-if-array-is-sorted-and-rotated":{"sha":"","difficulty":"easy"}},"solved":40,"easy":9,"medium":24,"hard":7}}

0 commit comments

Comments
 (0)