Skip to content

Commit de55fd8

Browse files
committed
Sep4: Sort, permutation [M]
basic logic and sort problem, O(NlogN), O(1)
1 parent 132868f commit de55fd8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

daily/Sep4.cc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
/**
8+
* @brief LC2860: happy students [M]
9+
* Permutation, sort, Time: O(NlogN), Space: O(1)
10+
*
11+
* @param nums
12+
* @return int
13+
*/
14+
int countWays(vector<int>& nums) {
15+
int ans = nums[0] > 0; // non selected
16+
for (int i = 1; i < nums.size(); i++) {
17+
ans += nums[i - 1] < i && i < nums[i];
18+
}
19+
return ans + 1; // all selected
20+
}
21+
};

0 commit comments

Comments
 (0)