Skip to content

Commit 2c6902a

Browse files
committed
feat(leetcode): add No.1053
1 parent 07b89fd commit 2c6902a

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

1052.Grumpy-Bookstore-Owner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# https://leetcode.com/problems/grumpy-bookstore-owner/
2-
# Easy (46.84%)
2+
# Medium (46.84%)
33
# Total Accepted: 2,136
44
# Total Submissions: 4,560
55
# beats 100.0% of python submissions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// https://leetcode.com/problems/previous-permutation-with-one-swap/
2+
//
3+
// algorithms
4+
// Medium (39.45%)
5+
// Total Accepted: 1,588
6+
// Total Submissions: 4,025
7+
// beats 100.0% of java submissions
8+
9+
10+
class Solution {
11+
public int[] prevPermOpt1(int[] A) {
12+
int len = A.length;
13+
if (len < 2) {
14+
return A;
15+
}
16+
17+
int i = len - 2;
18+
for (; i >= 0; i--) {
19+
if (A[i] > A[i + 1]) {
20+
break;
21+
}
22+
}
23+
24+
if (i == -1) {
25+
return A;
26+
}
27+
28+
swap(A, i, searchIdx(A, i + 1, len - 1, A[i]));
29+
30+
return A;
31+
}
32+
33+
public void swap(int[] A, int i, int j) {
34+
int tmp = A[i];
35+
A[i] = A[j];
36+
A[j] = tmp;
37+
}
38+
39+
public int searchIdx(int[] A, int i, int j, int target) {
40+
int idx = j;
41+
for (; idx >= i; idx--) {
42+
if (A[idx] < target) {
43+
break;
44+
}
45+
}
46+
47+
while (idx > i && A[idx - 1] == A[idx]) {
48+
idx--;
49+
}
50+
51+
return idx;
52+
}
53+
}

0 commit comments

Comments
 (0)