首页 > 试题广场 >

狡猾的雇主

[编程题]狡猾的雇主
  • 热度指数:949 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
一位雇主想要招聘一个工人,有个人前来应聘,这位雇主让每个人写下期望的薪资,然后再进行选择。
因为这个雇主十分狡猾,ta会选择期望薪资最低的人,但是这个人可能不是唯一的,为了避免纠纷,ta会选择在没有和其他人提出的期望薪资相同的情况下期望薪资最低的人录用。
求这个最低薪资,如果没有合适的人,则输出

输入描述:
第一行一个整数
第二行个整数表示个人提出的期望薪资
保证所有输入的数为正整数且不超过


输出描述:
一行一个整数表示答案
示例1

输入

3
3 2 1

输出

1
示例2

输入

6
1 1 4 5 1 4

输出

5
示例3

输入

3
4 4 4

输出

-1
Python版本,供参考
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)


发表于 2022-03-17 11:54:34 回复(0)
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的就说明是最小且仅出现过一次的数。
发表于 2022-03-10 16:35:40 回复(0)
用两个set计数,一个保存只看过一次的,一个保存看过多次的;
#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;
    }
}

发表于 2021-08-25 23:38:48 回复(0)
对期望薪资进行计数,并对期望薪资数组进行升序排列,然后顺序遍历期望薪资,第一个计数为1的薪资即为所求。
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);
    }
}

发表于 2021-07-22 17:39:52 回复(0)
用set判断这个元素是否有多个,省去一点时间,然后有多个的移出有序队列中,最终有序队列poll一个出来就是所需要的了
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());}
    }
 
}


发表于 2021-05-15 10:41:59 回复(0)
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就全部删除,重新判断;


发表于 2022-04-26 21:07:45 回复(0)
c++ unoreded_map
#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;
}


发表于 2022-04-27 11:47:35 回复(0)
索引定位计数法
n = int(input())
money_hope = list(map(int,input().split()))
res = []
count = [0]*10001
for i in range(n):
    count[money_hope[i]] += 1
try:
    res.append(count.index(1))
except:
    pass
if res == []:
    print(-1)
else:
    print(min(res))

发表于 2022-04-07 16:25:02 回复(0)
JS V8版本
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);
    }
}

发表于 2022-04-07 10:06:59 回复(0)
// 先排序,再遍历,时间复杂度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)
    }
}


发表于 2022-03-05 01:52:35 回复(0)

将数组排序,二分查找元素的最后索引。
排序: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)
}
发表于 2021-11-25 18:20:53 回复(0)
python 字典计数
n = int(input())
nums = list(map(int, input().split()))
dic = dict()
ans = []
for num in nums:
    dic[num] = dic.get(num, 0) + 1
for key, value in dic.items():
    if value > 1: continue
    ans.append(key)
if ans:
    print(sorted(ans)[0])
else:
    print("-1")


发表于 2021-09-21 10:45:57 回复(0)
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);
         
    }
     
}

编辑于 2021-09-20 20:01:59 回复(0)