How to find the minimum positive integer in a list of lists in O(n)?

A. G

I am trying to write a program in python that consumes a list that contains as many inner lists as the length of the outer list. For example,

L = [[-10, -9,    99,   100],
     [ -6, -3,   100,   101],
     [ -1,  0,  1000,  1010],
     [ -1, 10, 10000, 24852]]

and it outputs the smallest positive integer. It outputs -1 is there if all elements are negative or 0. The output for the list above would be 10. The elements are also always sorted in ascending order, so both the rows and the columns are sorted in ascending order. What this means is that if you look at any row or any column, it will be sorted from left-to-right and top-to-bottom, respectively.

The issue is I have to do this in O(n) efficiency (n referring to the length of the outer list) but every solution I come up with involves nested loops and thus the efficiency becomes O(n^2).

How can I achieve this in O(n) efficiency in python?

Edit: I have written the following code which works for some cases but doesn't work for others

def min_positive(L): 
i = 0
n = len(L)
j = len(L) - 1
min_pos = L[0][j]
while ( i < n and j >= 0 ): 
    if (L[i][j] < min_pos and L[i][j] > 0): 
        min_pos = L[i][j]
    if (L[i][j] >= min_pos): 
        j = j - 1 
    i = i + 1
if min_pos <= 0:
    min_pos = -1 
return min_pos

This works for the following list

L = [[-10, -9,    99,   100],
 [ -6, -3,   100,   101],
 [ -1,  0,  1000,  1010],
 [ -1, 10, 10000, 24852]]

but doesn't work for the list

L = [[-10, -9,    99,   100],
     [ -6, -3,   100,   101],
     [ -1,  0,  1000,  1010],
     [ 1, 10, 10000, 24852]]

ie. output should be 1 but it's still 10 Feel like I'm close so any help would be appreciated!

Vighnesh Raut

This is a worst case, O(M + N) solution where M is the number of rows and N is the number of columns.

L = [[-10, -9,    99,   100],
     [ -6, -3,   100,   101],
     [ -1,  0,  1000,  1010],
     [ -1, 10, 10000, 24852]]

def get_least_positive(list_of_lists):
    minimum = float("inf")

    # start from top right
    row = 0
    column = len(list_of_lists[0]) - 1

    # follow the staicase
    while row < len(list_of_lists) and column >= 0:
        elem = list_of_lists[row][column]
        if elem > 0:
            minimum = min(minimum, elem)
            column -= 1
        else:  # found Negative, go to next row.
            row += 1

    return minimum if minimum != float('inf') else -1


print(get_least_positive(L))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find the first positive integer number that is not in the list

From Dev

How to find Negative maximum and positive minimum in Oracle?

From Dev

How to find Negative maximum and positive minimum in Oracle?

From Dev

How to find the positive numbers in a list in Prolog?

From Dev

How to find an element in a list of lists

From Dev

How to find a minimum in a List using only reduce?

From Dev

How to find the number of nested lists in a list?

From Dev

How to find out if there are any duplicates in list of lists

From Dev

Find lowest positive missing integer in array

From Dev

Find the smallest positive number not in list

From Dev

how to find members that exist in at least two lists in a list of lists

From Dev

how to split a list into two lists in which the first has the positive entries and the second has non-positive entries-SML

From Dev

Finding the index of the minimum of each list in a list of lists

From Dev

Find smallest integer greater or equal than x (positive integer) multiple of z (positive integer, probably power of 2)

From Dev

How do I find all positive integer solutions to 7x+2y=n

From Dev

BinaryOpertor for List<Integer> to add the lists

From Dev

How can I find the local minimum values and of a list in Mathematica?

From Dev

How to find the minimum number of groups needed for grouping a list of elements?

From Dev

How to get a list of date time objects in python and find out the minimum

From Dev

How to write a function that takes a positive integer N and returns a list of the first N natural numbers

From Dev

Iteratively find minimum values in sub-lists

From Dev

How to determine how many unique other items an integer has been with in a list of lists of lists?

From Dev

Find length of a list of lists

From Dev

Find a number in list of lists

From Dev

Calculating minimum length among the lists inside a list

From Dev

find the list with the minimum size over a list of list

From Dev

List all negative numbers and minimum positive number from table

From Java

How to set column value to last positive integer

From Java

How to inverse a positive integer in C without * or - operators?

Related Related

  1. 1

    How to find the first positive integer number that is not in the list

  2. 2

    How to find Negative maximum and positive minimum in Oracle?

  3. 3

    How to find Negative maximum and positive minimum in Oracle?

  4. 4

    How to find the positive numbers in a list in Prolog?

  5. 5

    How to find an element in a list of lists

  6. 6

    How to find a minimum in a List using only reduce?

  7. 7

    How to find the number of nested lists in a list?

  8. 8

    How to find out if there are any duplicates in list of lists

  9. 9

    Find lowest positive missing integer in array

  10. 10

    Find the smallest positive number not in list

  11. 11

    how to find members that exist in at least two lists in a list of lists

  12. 12

    how to split a list into two lists in which the first has the positive entries and the second has non-positive entries-SML

  13. 13

    Finding the index of the minimum of each list in a list of lists

  14. 14

    Find smallest integer greater or equal than x (positive integer) multiple of z (positive integer, probably power of 2)

  15. 15

    How do I find all positive integer solutions to 7x+2y=n

  16. 16

    BinaryOpertor for List<Integer> to add the lists

  17. 17

    How can I find the local minimum values and of a list in Mathematica?

  18. 18

    How to find the minimum number of groups needed for grouping a list of elements?

  19. 19

    How to get a list of date time objects in python and find out the minimum

  20. 20

    How to write a function that takes a positive integer N and returns a list of the first N natural numbers

  21. 21

    Iteratively find minimum values in sub-lists

  22. 22

    How to determine how many unique other items an integer has been with in a list of lists of lists?

  23. 23

    Find length of a list of lists

  24. 24

    Find a number in list of lists

  25. 25

    Calculating minimum length among the lists inside a list

  26. 26

    find the list with the minimum size over a list of list

  27. 27

    List all negative numbers and minimum positive number from table

  28. 28

    How to set column value to last positive integer

  29. 29

    How to inverse a positive integer in C without * or - operators?

HotTag

Archive