一位雇主想要招聘一个工人,有个人前来应聘,这位雇主让每个人写下期望的薪资,然后再进行选择。
因为这个雇主十分狡猾,ta会选择期望薪资最低的人,但是这个人可能不是唯一的,为了避免纠纷,ta会选择在没有和其他人提出的期望薪资相同的情况下期望薪资最低的人录用。
求这个最低薪资,如果没有合适的人,则输出
第一行一个整数第二行个整数表示个人提出的期望薪资保证所有输入的数为正整数且不超过
一行一个整数表示答案
3 3 2 1
1
6 1 1 4 5 1 4
5
3 4 4 4
-1
n = int(input()) inp = input().split() list2 = [] for i in range(n): inp[i] = int(inp[i]) i = 0 while i < len(inp)-1: poi = 0 j = i+1 while j < len(inp): if inp[i] == inp[j]: poi = 1 inp.pop(j) j = i+1 if poi == 1: inp.pop(i) elif poi == 0: break i = 0 if len(inp) != 0: inp.sort() print(inp[0]) elif len(inp) == 0: print(-1)
const int Max = 9999; int main() { int n; int num[Max]{0}; cin >> n; //人数 for (int i = 0; i < n; i++) { int x; cin >> x; num[x]++; } int res = -1; for (int i = 0; i < Max; i++) { if (num[i] == 1) { res = i; break; } } cout << res << endl; system("pause>nul"); return 0; }不用那么麻烦,直接用数组记录x出现的次数,每重复一次就让num[x]++,再让游标从小到大遍历数组,第一次访问到数组数量等于1的就说明是最小且仅出现过一次的数。
#include <iostream> #include <algorithm> #include <vector> #include <unordered_set> #include <set> using namespace std; int main() { int n; cin >> n; set<int> noseen; unordered_set<int> seen; for (int i = 0; i < n; i++) { int galary; cin >> galary; if (seen.count(galary)) { continue; } else if (noseen.count(galary)) { noseen.erase(galary); seen.insert(galary); } else { noseen.insert(galary); } } if (noseen.empty()) { cout << -1 << endl; } else { auto it = noseen.begin(); cout << *it << endl; } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.HashMap; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] rawExpect = br.readLine().split(" "); int[] expect = new int[n]; HashMap<Integer, Integer> counter = new HashMap<>(); for(int i = 0; i < n; i++) { expect[i] = Integer.parseInt(rawExpect[i]); if(counter.containsKey(expect[i])) counter.put(expect[i], counter.get(expect[i]) + 1); else counter.put(expect[i], 1); } Arrays.sort(expect); for(int i = 0; i < n; i++){ if(counter.get(expect[i]) == 1){ System.out.println(expect[i]); return; } } System.out.println(-1); } }
importjava.util.HashSet; importjava.util.PriorityQueue; importjava.util.Scanner; importjava.util.Set; publicclassMain { publicstaticvoidmain(String[] args) { Scanner s =newScanner(System.in); intx = s.nextInt(); Set<Integer> set =newHashSet(); PriorityQueue<Integer> p =newPriorityQueue(); for(inti =0; i < x; i++) { inttemp = s.nextInt(); if(set.add(temp)){ p.add(temp); }else{ p.remove(temp); } } if(p.size()==0) {System.out.println(-1);} else{System.out.println(p.poll());} } }
n = int(input()) n_list = list(map(int,input().split())) while True: n -= 1 min_num = 0 n_list.sort() j = len(n_list) if j == 1: min_num = n_list[0] break else: if n_list[0] == n_list[j - 1]: min_num = -1 break else: min_num = min(n_list) m = n_list.count(min_num) if n_list.count(min_num) == 1: break else: i = 1 for i in range(m): n_list.remove(min_num) print(min_num) if语句进行逐一判断,只有一个元素时,直接返回这个值;多个元素相等时,返回-1; 多个元素不等时,选择列表中最小的值,判断隔个数,大于1就全部删除,重新判断;
#include<iostream> #include<vector> #include<unordered_map> #include<limits.h> using namespace std; int main() { int n; cin >> n; vector<int> nums(n); unordered_map<int, int> map; for (int i = 0; i < n; ++i){ cin >> nums[i]; map[nums[i]]++; } int min = INT_MAX; for (auto [a, b] : map){ if (b == 1 && a < min) { min = a; } } int ans = min==INT_MAX ? -1 : min; cout << ans; return 0; }
let n = Number(readline()); let nums = readline().split(' ').map(Number); nums.sort((a,b) =>{return a-b;}); let set = {}; let min = Infinity; for(let i=0; i<n; i++){ set[nums[i]] = (set[nums[i]] || 0) + 1; } if(set[nums[0]] == 1){ console.log(nums[0]); }else{ for(let key in set){ if(set[key] == 1){ min = Math.min(min ,key); } } if(min == Infinity){ console.log(-1); }else{ console.log(min); } }
// 先排序,再遍历,时间复杂度nlogn package main import ( "fmt" "sort" ) func main(){ var n int fmt.Scan(&n) sl := make([]int, n) for i:=0;i<n;i++{ var t int fmt.Scan(&t) sl[i] = t } sort.Ints(sl) a := sl[0] chose := -1 for i:=1;i<n;i++ { if sl[i] == a { chose = 1 continue }else if sl[i] != a { if chose == 1 { chose = -1 a = sl[i] }else if chose == -1 { break } } } if chose == -1 { fmt.Print(a) }else{ fmt.Print(-1) } }
将数组排序,二分查找元素的最后索引。
排序:O(nlogn),查找: O(nlogn)
package main import ( "fmt" "sort" ) func binSearch(arr []int, key int) int { lo, hi := 0, len(arr)-1 for lo < hi { mid := lo + (hi - lo) >> 1 if key < arr[mid] { hi = mid-1 } else { if lo == mid { break } lo = mid } } if arr[hi] == key { return hi } else { return lo } } func main() { var n int fmt.Scan(&n) // 从min到max,找到第一个没有重复的数字 arr := make([]int, n) for i := 0; i < n; i++ { fmt.Scan(&arr[i]) } sort.Ints(arr) ret := -1 for i := 0; i < n; { idx := binSearch(arr, arr[i]) if idx == i { ret = arr[i] break } i = idx+1 } fmt.Println(ret) }
public class Main{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); int []curr=new int[10010]; int n=scan.nextInt(); for(int i=0;i<n;i++){ int idx=scan.nextInt(); curr[idx]++; } for(int i=0;i<10001;i++){ if(curr[i]==1){System.out.print(i);return;} } System.out.print(-1); } }