Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm writing a function that is supposed to recursively find the smallest positive number in a give vector of ints.

Right now my code looks like this:

 #include <vector>
 using namespace std;
 int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
    return -1;
    } else {
        int min = rec_min_pos(nums, size - 1);
        if(min < 0){
            if(nums[size - 1] <= 0){
                return -1;
            }else{
                return nums[size-1];
            }
        }else {
            if (min < nums[size - 1]) {
                return min;
            } else {
                return nums[size - 1];
            }
        }
    }
}
int main(){
    vector<int> nums = {2, -3, 6, -21, 10, -80, 8};
    cout << rec_min_post(nums, nums.size());
}

If I run this, I get 8, which is presumably because the next element is negative.

The previous version:

#include <vector>
using namespace std;
int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
        return -1;
    } else {
        int min = rec_min_pos(nums, size - 1);

        if (min < nums[size - 1]) {
            return min;
        } else {
            return nums[size - 1];
        }
    }
}
int main(){
    vector<int> nums = {2, -3, 6, -21, 10, -80, 8};
    cout << rec_min_post(nums, nums.size());
}

I would get -80 which is in fact the smallest value, but I need the smallest positive value.

What can I do to get the smallest positive value from the integer vector?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
252 views
Welcome To Ask or Share your Answers For Others

1 Answer

def SPN(nums, s):
  if s == 0:
    # Empty array
    return +∞
  if nums[s-1] > 0:
    # num[s] is admissible, recurse and keep the smallest
    return min(nums[s-1], SPN(nums, s-1))
  else:
    # Just recurse
    return SPN(nums, s-1)

print SPN(nums, len(nums)

The c++ version:

#include <vector>
using namespace std;
int rec_min_pos(const vector<int> & nums, int size) {
    if (size < 1) {
        return INT_MAX;
    }
    if(nums[size-1] > 0){
        return min(nums[size-1], rec_min_pos(nums, size-1));
    }
    else{
        return rec_min_pos(nums, size-1);
    }
}

Update:

Assuming that reserving INT_MAX is not allowed to represent +∞, we can use a negative instead, say -1, with the convention that min(x,-1) = x.

Infty= -1 # Conventional +∞

def Min(a, b):
  if b == Infty:
    return a
  else:
    return min(a, b)

def SPN(nums, s):
  if s == 0:
    # Empty array
    return Infty 
  if nums[s-1] > 0:
    # num[s] is admissible, recurse and keep the smallest
    return Min(nums[s-1], SPN(nums, s-1))
  else:
    # Just recurse
    return SPN(nums, s-1)

That leads us to a more elegant version if we use the convention that any negative value represents +∞:

def Min(a, b):
  if a < 0:
    return b
  if b < 0:
    return a
  return min(a, b)

def SPN(nums, s):
  if s == 1:
    return nums[0] # Assumes len(nums) > 0

  return Min(nums[s-1], SPN(nums, s-1))

Elegant version in C++:

#include <vector>
using namespace std;
int minimum(int a, int b){
    if(a < 0){
        return b;
    }
    if(b < 0){
        return a;
    }
    return min(a, b);
}
int SPN(vector<int> nums, int size){
    if(size == 1){
        return nums[0];
    }
    return minimum(nums[size-1], SPN(nums, size-1));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...