算法测试系列:Packing Melons

Packing Melons

Before you are two assembly lines, one with boxes and one with watermelons, both of varying size. Your desire is to put as many watermelons into boxes as possible. You can pick where you
start taking watermelons from, but once you start, all melons going past you must be placed, or you must stop. We want you to calculate how many watermelons you can place.

As input, you are given two lists, one of box sizes and one of watermelon sizes. A watermelon will fit into a box with a number greater than or equal to the melon’s. Only one melon can go
into a box. You can hold onto the melon and skip a box to place it in a later one. Calculate how many watermelons can be placed.

  • Output Format

A list of labels.

  • Sample Input
trades = [[99.0, 5.0, 20.0], # green (good trade)
	      [95.0, 15.0, 10.0], # green (good trade)
	      [5.0, 80.0, 40.0], # red (bad trade)
	      [3.0, 92.0, 20.0]] # red (bad trade)

labels = ['green', 'green', 'red', 'red']

new_ trades = [[90.0, 10.0, 15.0],
	           [10.0, 98.0, 50.0]]
  • Sample Output
['green', 'red']
  • Techniques

We’re just looking for a simple, moderately effective, solution.
Test cases can be passed with fewer than 30 lines of code.

import sys, os

def melon_count(boxes,melons):
     boxes.sort()
     melons.sort()
     
     result = 0
     i = 0
     j = 0
     
     while(i != boxes.__len__() and j != melons.__len__()):
          if boxes[i] >= melons[j]:
               result = result+1
               i = i+1
               j= j+1
          else:
               i = i+1
     return result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值