ขนาดวิดีโอ: 1280 X 720853 X 480640 X 360
แสดงแผงควบคุมโปรแกรมเล่น
เล่นอัตโนมัติ
เล่นใหม่
BRUTE FORCE:int missingNumber(vector &arr) { unordered_set set; for(auto& num : arr){ if(num > 0) set.insert(num); } int numToFind = 1; while(set.count(numToFind)) numToFind += 1; return numToFind; }OPTIMISED:int missingNumber(vector &arr) { int n = arr.size(); for(int i = 0; i < n; ++i){ while(arr[i] > 0 && arr[i] < n && arr[arr[i] - 1] != arr[i]){ swap(arr[i], arr[arr[i] - 1]); } } for(int i = 0; i < n; ++i){ if(arr[i] != i + 1) return i + 1; } return n + 1; }
BRUTE FORCE:
int missingNumber(vector &arr) {
unordered_set set;
for(auto& num : arr){
if(num > 0) set.insert(num);
}
int numToFind = 1;
while(set.count(numToFind)) numToFind += 1;
return numToFind;
}
OPTIMISED:
int missingNumber(vector &arr) {
int n = arr.size();
for(int i = 0; i < n; ++i){
while(arr[i] > 0 && arr[i] < n && arr[arr[i] - 1] != arr[i]){
swap(arr[i], arr[arr[i] - 1]);
}
}
for(int i = 0; i < n; ++i){
if(arr[i] != i + 1) return i + 1;
}
return n + 1;
}