Skip to content

Commit

Permalink
Sep4: Sort, permutation [M]
Browse files Browse the repository at this point in the history
basic logic and sort problem, O(NlogN), O(1)
  • Loading branch information
aucker committed Sep 4, 2024
1 parent 132868f commit de55fd8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions daily/Sep4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
/**
* @brief LC2860: happy students [M]
* Permutation, sort, Time: O(NlogN), Space: O(1)
*
* @param nums
* @return int
*/
int countWays(vector<int>& nums) {
int ans = nums[0] > 0; // non selected
for (int i = 1; i < nums.size(); i++) {
ans += nums[i - 1] < i && i < nums[i];
}
return ans + 1; // all selected
}
};

0 comments on commit de55fd8

Please sign in to comment.