Skip to content

Commit 98f61f0

Browse files
committed
280423
1 parent 55995f6 commit 98f61f0

File tree

2 files changed

+94
-27
lines changed

2 files changed

+94
-27
lines changed

src/Main.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
1-
import leetcode.editor.en.Q1014.BestSightseeingPair;
2-
import leetcode.editor.en.Q1051.HeightChecker;
3-
import leetcode.editor.en.Q115.DistinctSubsequences;
4-
import leetcode.editor.en.Q1218.LongestArithmeticSubsequenceOfGivenDifference;
5-
import leetcode.editor.en.Q122.BestTimeToBuyAndSellStockIi;
6-
import leetcode.editor.en.Q1416.RestoreTheArray;
7-
import leetcode.editor.en.Q152.MaximumProductSubarray;
8-
import leetcode.editor.en.Q1548.TheMostSimilarPathInAGraph;
9-
import leetcode.editor.en.Q1567.MaximumLengthOfSubarrayWithPositiveProduct;
10-
import leetcode.editor.en.Q1639.NumberOfWaysToFormATargetStringGivenADictionary;
11-
import leetcode.editor.en.Q2218.MaximumValueOfKCoinsFromPiles;
12-
import leetcode.editor.en.Q2402.MeetingRoomsIii;
13-
import leetcode.editor.en.Q2607.MakeKSubarraySumsEqual;
14-
import leetcode.editor.en.Q2641.CousinsInBinaryTreeIi;
15-
import leetcode.editor.en.Q459.RepeatedSubstringPattern;
16-
import leetcode.editor.en.Q461.HammingDistance;
17-
import leetcode.editor.en.Q516.LongestPalindromicSubsequence;
18-
import leetcode.editor.en.Q63.UniquePathsIi;
19-
import leetcode.editor.en.Q646.MaximumLengthOfPairChain;
20-
import leetcode.editor.en.Q673.NumberOfLongestIncreasingSubsequence;
21-
import leetcode.editor.en.Q71.SimplifyPath;
22-
import leetcode.editor.en.Q727.MinimumWindowSubsequence;
23-
import leetcode.editor.en.Q740.DeleteAndEarn;
24-
import leetcode.editor.en.Q879.ProfitableSchemes;
25-
import leetcode.editor.en.Q946.ValidateStackSequences;
1+
import leetcode.editor.en.Q1752.CheckIfArrayIsSortedAndRotated;
262
import org.json.JSONArray;
273
import org.json.JSONTokener;
284

295
import java.io.IOException;
306
import java.io.InputStream;
317
import java.util.ArrayList;
32-
import java.util.Arrays;
338
import java.util.LinkedList;
349
import java.util.List;
3510

3611

3712
public class Main {
3813
public static void main(String[] args) throws IOException {
3914

40-
System.out.println(new LongestArithmeticSubsequenceOfGivenDifference().longestSubsequence(toIntArray("[7,-2,8,10,6,18,9,-8,-5,18,13,-6,-17,-1,-6,-9,9,9]\n"), 1));
15+
System.out.println(new CheckIfArrayIsSortedAndRotated().check(toIntArray("[3,4,5,1,2]")));
4116

4217
}
4318

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package leetcode.editor.en.Q1752;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//Given an array nums, return true if the array was originally sorted in non-
8+
//decreasing order, then rotated some number of positions (including zero).
9+
//Otherwise, return false.
10+
//
11+
// There may be duplicates in the original array.
12+
//
13+
// Note: An array A rotated by x positions results in an array B of the same
14+
//length such that A[i] == B[(i+x) % A.length], where % is the modulo operation.
15+
//
16+
//
17+
// Example 1:
18+
//
19+
//
20+
//Input: nums = [3,4,5,1,2]
21+
//Output: true
22+
//Explanation: [1,2,3,4,5] is the original sorted array.
23+
//You can rotate the array by x = 3 positions to begin on the the element of
24+
//value 3: [3,4,5,1,2].
25+
//
26+
//
27+
// Example 2:
28+
//
29+
//
30+
//Input: nums = [2,1,3,4]
31+
//Output: false
32+
//Explanation: There is no sorted array once rotated that can make nums.
33+
//
34+
//
35+
// Example 3:
36+
//
37+
//
38+
//Input: nums = [1,2,3]
39+
//Output: true
40+
//Explanation: [1,2,3] is the original sorted array.
41+
//You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
42+
//
43+
//
44+
//
45+
// Constraints:
46+
//
47+
//
48+
// 1 <= nums.length <= 100
49+
// 1 <= nums[i] <= 100
50+
//
51+
//
52+
// 👍 1723 👎 80
53+
54+
55+
//leetcode submit region begin(Prohibit modification and deletion)
56+
class Solution {
57+
public boolean check(int[] nums) {
58+
int min = nums[0];
59+
int minIdx = 0;
60+
61+
for (int i = 1; i < nums.length; i++) {
62+
if (nums[i] < min) {
63+
minIdx = i;
64+
min = nums[i];
65+
}
66+
}
67+
int modIdx = minIdx;
68+
int prev = min;
69+
70+
int end = nums.length - 1;
71+
while (end > 0 && nums[0] == nums[end]) {
72+
end--;
73+
}
74+
if (nums.length == 1) return true;
75+
76+
for (int i = 0; i <= end; i++) {
77+
modIdx = (modIdx + 1) % (end + 1);
78+
if (modIdx == minIdx) break;
79+
if (nums[modIdx] < prev) {
80+
return false;
81+
}
82+
prev = nums[modIdx];
83+
}
84+
return true;
85+
86+
}
87+
}
88+
//leetcode submit region end(Prohibit modification and deletion)
89+
90+
91+
public class CheckIfArrayIsSortedAndRotated extends Solution {
92+
}

0 commit comments

Comments
 (0)