【每日一题20220308】极小较大值

:woman_mage:给定一个数字列表和目标索引值,请编写一个函数,返回大于指定索引值对应元素的所有元素中,值最小的那个的索引值。如果不存在则返回-1

示例:
输入: [4, 1, 3, 5, 6],0
输出:3
因为索引0对应的元素值是4,最小的大于4的数是5,5的索引是3,因此返回3。

题目难度:简单
题目来源:CodeWars:Least Larger

def solution(nums: list, i: int) -> int:
    # your code here

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, i: int) -> int:
    # your code here
    list1 = list(set(nums))
    for j, k in enumerate(list1):
        if k == nums[i]:
            if j + 1 < len(list1):
                num = list1[j + 1]
                break
            else:
                return -1
        if j == len(list1) - 1:
            return -1
    for j, k in enumerate(nums):
        if k == num:
            return j
def solution(nums: list, i: int) -> int:
    num = nums[i]
    # 如果num是列表中最大值,返回-1
    if num == max(nums):
        return -1
    else:
        # 列表升序排序,赋值新列表
        nums_sort =sorted(nums)
        # 新列表中获取大于num的最小值的索引,num的索引值+1
        n_index = nums_sort.index(num)+1
        # 新列表中获取大于num的最小值
        result = nums_sort[n_index]
        # 旧列表中返回大于num的最小值的索引
        return nums.index(result)

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3

2 个赞
def least_larger(a, i): 
    n = a[i]
    min = n-1
    min_index = -1
    for i,num in enumerate(a):
        if num > n and (min < n or num < min):
                min = num
                min_index = i
    return min_index
1 个赞
def solution(nums: list, i: int) -> int:
    # your code here
    list_num = []
    for j in nums:
        if j > nums[i]:
            list_num.append(j)
    if not list_num:
        return -1
    else:
        return nums.index(min(list_num))


assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
1 个赞
def solution(nums: list, i: int) -> int:
    # 0308 极小较大值
    res = [x for x in nums if x > nums[i]]
    if len(res) > 0 :
        return nums.index(sorted(res)[0])
    return -1

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
1 个赞
    def solution(nums: list, i: int) -> int:
        lis = nums[i+1:]
        if lis == []:
            return -1
        else:
            if max(lis) > nums[i]:
                for p in range(len(lis)):
                    if lis[p] > nums[i]:
                        mi = lis[p]
                        ind = p
                        break
                for q in range(len(lis)):
                    if lis[q] > nums[i] and lis[q] < mi:
                        mi = lis[q]
                        ind = q
                return  ind + 1 + i
            else:
                return -1

‘’’
【input】
nums: list
i:int
【output】
int
‘’’
def solution(nums, i):
datas = [x for x in nums if x > nums[i]]
if datas != :
return nums.index(min(datas))
else:
return -1

def solution(nums: list, i: int) -> int:
    num = [j for j in nums if nums[i] < j]
    if num != []:
        return nums.index(min(num))
    else:
        return -1

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, i: int) -> int:
    return -1 if max(nums) == nums[i] else nums.index(min([j for j in nums if j > nums[i]]))

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
    def solution(nums: list, i: int) -> int:
        res = {}
        if i >= len(nums) - 1:
            return -1
        for index,value in enumerate(nums):
            if nums[index] > nums[i]:
                res[nums[index]] = index
        return res[min([key for key in res])]
    assert solution([4, 1, 3, 5, 6], 0) == 3
    assert solution([4, 1, 3, 5, 6], 4) == -1
    assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, i: int) -> int:
    target = nums[i]
    result = dict()
    if target == max(nums):
        return -1
    else:
        for k,v in enumerate(nums):
            if v > target and v not in result:
                result[v] = k
        return result[sorted(result.keys())[0]]

def miniMax(li, i):
n = li[i]
li1 = [j for j in li if n < j]
if li1 == :
return -1
else:
return li.index(min(li1))

assert miniMax([4, 1, 3, 5, 6], 0) == 3
assert miniMax([4, 1, 3, 5, 6], 4) == -1
assert miniMax([4, 1, 3, 8, 7, 6, 5], 0) == 6

def miniMax1(li, n):
m = li[n]
li1 =
for i in li:
if i > m:
li1.append(i)
if li1 == :
return -1
else:
return li.index(min(li1))

assert miniMax1([4, 1, 3, 5, 6], 0) == 3
assert miniMax1([4, 1, 3, 5, 6], 4) == -1
assert miniMax1([4, 1, 3, 8, 7, 6, 5], 0) == 6

def solution(nums: list, i: int) -> int:
    if nums[i] == max(nums):
        return -1
    else:
        a = nums[i]
        ls = [x for x in nums if x >a ]
        min_num =min(ls)
        return nums.index(min_num)

如果列表中有重复的元素,这样写是有问题的

def solution(nums: list, i: int) -> int:
    # your code here
    if nums[i] == max(nums):
        return -1

    sort_nums = set(sorted(nums))
    for idx,num in enumerate(sort_nums):
        if num > nums[i]:
            return nums.index(num)
        

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3
def solution(nums: list, i: int) -> int:
    res_list = [nums[j] for j in range(len(nums)) if nums[j] > nums[i]]
    if res_list == []:
        return -1
    else:
        res_list.sort()
        return nums.index(res_list[0])

assert solution([4, 1, 3, 5, 6], 0) == 3
assert solution([4, 1, 3, 5, 6], 4) == -1
assert solution([1, 3, 5, 2, 4], 0) == 3