gpt4 book ai didi

algorithm - 我有一个集合,每个集合的元素都有值,我想按值将元素尽可能均匀地分布在 N block 上

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:53 25 4
gpt4 key购买 nike

<分区>

我有一个集合,每个集合的元素都有值,我想按值将元素尽可能均匀地分布在 N 个片段上。

我试过用scala代码完成如下结果,一直没找到好方法,很容易死循环

像这样:

Map("table1"->100,"table2"->500,"table3"->20000,"table4"->10,"table5"->1000,"table6"->200,"table7"->10000) 

对于4件,我希望得到尽可能平均的结果。

Map("table7"->10000)
Map("table3"->20000)
Map("table1"->100,"table2"->500,"table4"->10,"table4"->10,"table5"->1000,"table6"->200)
Map()

或者,很完美

Map("table7"->10000)
Map("table3"->20000)
Map("table1"->100,"table2"->500,"table4"->10,"table4"->10,"table6"->200)
Map("table5"->1000)

我用了@Krzysztof Atłasik的代码,得到的结果是重复的。10 个桶。公式数据为:

val testMap=Map("test_7d_all_qr "->1,"test_daily_advertise_position "->1,"test_7d_province "->94,"test_city_statistics "->34916,"test_30days_page_view "->11,"test_30days_ad_anomaly_auth_analyze "->26,"tt_user_grouping "->1,"oauth_refresh_tokens "->42,"tt_daily_share "->4476,"test_ad_anomaly_ip_analyze "->148,"test_7d_ad_anomaly_time_analyze "->15,"share_tool_template "->1,"test_30days_visit_duration "->70,"tt_qr_group_01 "->1,"tt_seven_user_tmp "->35890,"test_qr_code_statistics_temporary "->1,"tt_cms_scene "->1,"test_30days_terminal_analysis "->1406,"test_7d_terminal_analysis "->765,"test_7d_single_scene "->51,"test_30days_single_scene "->77,"test_monitor_daily_new_user "->1,"tt_ad_authorize "->1,"tt_sdk_version_appcount "->1,"test_ad_anomaly_auth_analyze "->428,"tt_attribute_relationship "->1,"test_7d_page_view "->9,"tt_ad_user_info "->1,"phone_model "->1,"share_tool_var "->1,"share_tool_type_template "->1,"tt_stay_logs_link "->8,"test_advertise_home_hour "->4,"test_monitor_hourly "->1,"test_7d_phonebrand "->71,"tt_cms_system "->1,"test_7d_city_statistics "->297,"test_link_cheat_protect "->1,"test_daily_advertise_link "->1,"test_30days_trend_analysis "->17,"tt_cms_log "->1,"test_hourly_advertise_position "->6,"push_join_info "->1,"test_monitor_daily_app "->5,"test_7d_event "->491,"cms_boss_words "->1,"test_daily_error "->13903,"test_user_activity "->569,"tt_event "->127,"test_link_summary_hour "->290,"test_7d_single_qr_group "->1,"tt_sdk_log "->1,"tt_code "->42,"test_page_view "->11689,"tt_7d_device_statistics "->709,"test_7d_ad_anomaly_ip_analyze "->15,"cms_wx_data "->1,"tt_sdk_version_apps "->311,"test_qr_code_statistics "->138,"test_daily_link "->709,"test_30days_event "->974,"test_7d_error "->1,"test_30days_city_statistics "->527,"tt_link_trace "->8,"test_7d_single_position "->1,"test_30days_single_link "->5,"test_daily_event "->33480,"tt_share_page "->53,"tt_30days_hierarchy_share "->1,"tt_stay_logs "->17149,"test_dailyshare_gender "->282,"test_hourly_advertise_media "->6,"test_monitor_daily_link "->4,"tt_device_statistics "->66629,"oauth_authorization_codes "->73,"test_ad_anomaly_all_analyze "->5488,"test_30days_userShare_top10 "->943,"test_daily_funnel "->782,"oauth_scopes "->1,"top100070922 "->0,"test_7d_userShare_top10 "->639,"tt_hierarchy_share "->148,"oauth_clients "->1,"user_phone "->0,"tt_sms_queue "->2,"tt_cms_article "->1,"test_monitor_daily "->1,"cms_source_type "->1,"test_30days_entrance_page "->11,"test_30days_ad_anomaly_all_analyze "->636,"oauth_jwt "->1,"tt_cms_user "->1,"tt_ci_sessions "->38,"test_7d_link_monitor "->1,"test_daily_link_monitor "->2717,"test_hourly_qr_group "->239,"tt_7d_hierarchy_share "->1,"test_30days_event_paras "->1,"tt_event_link_funnel "->2,"test_blackIP_setting "->1,"test_visit_duration "->2476,"tt_qr_group "->19,"test_hourly_scene "->41057,"test_30days_ad_anomaly_time_analyze "->26,"test_hourly_scene_group "->24908,"tt_smartLink_dict_name "->1,"test_scene_statistics "->7868,"test_30days_single_entrance_page "->59,"test_online_status "->1,"convert_path "->1,"user_group_funnel_daily "->7,"test_daily_media "->238,"test_grey_ak "->1,"cms_market "->1,"test_dailyshare_top10 "->10329,"test_hourly_qr "->334,"test_monitor_daily_trend "->8,"test_dailyshare_source "->224,"test_ad_anomaly_time_analyze "->158,"test_30days_scene_group "->34,"test_hourly_share_summary "->2895,"test_30days_single_page_view "->107,"service_user_view "->1,"tt_funnel "->1,"test_30days_single_qr_group "->1,"test_monitor_hourly_link "->12,"test_30days_error "->1,"test_daily_scene_group "->3702,"test_smartLink_day_analysis "->5770,"tt_prevent_cheat "->1,"test_hourly_advertise_link "->8,"tt_wechat_user_bind "->440049,"user_apps "->110,"test_dailyshare_page "->1394,"test_link_summary "->57,"test_smartLink_summary_analysis "->352,"share_tool_code "->1,"test_7d_trend_analysis "->15,"test_7d_event_paras "->1,"tt_sdk_history "->1,"tt_mini_code_authorize "->1,"test_visit_depth "->1852,"test_daily_entrance_page "->1254,"tt_private_construct_info "->1,"test_monitor_hourly_trend "->14,"tt_user_etl "->12914,"test_7d_scene_group "->26,"test_30days_link_summary "->1,"test_7d_visit_depth "->43,"tt_task "->1,"tt_code_share "->29,"tt_authorization "->1,"test_daily_advertise_media "->1,"share_tool_user_template "->1,"tt_30days_device_statistics "->1301,"tt_code_tool "->21,"test_30days_single_media "->2,"tt_scene_link "->1,"tt_code_sem "->2,"tt_stay_logs_media "->4,"tt_mini_radio "->1,"tt_sdk_notice "->1,"test_entrance_page "->5209,"test_terminal_analysis "->91350,"test_7d_single_entrance_page "->39,"test_7d_visit_duration "->57,"cms_analy_bind "->1,"test_30days_link_monitor "->1,"test_hourly_trend_analysis_debug "->1,"test_advertise_home "->1,"tt_stay_logs_position "->4,"test_30days_single_position "->1,"tt_smartLink_dict "->114,"test_ad_anomaly_all_analyze_hour "->1,"test_monitor_hourly_qr "->1,"test_smartLink_hour_analysis "->39287,"test_hourly_position "->843,"tt_cms_scene_type "->1,"test_dailyshare_city "->2915,"test_operation_log "->1,"oauth_access_tokens "->42,"share_tool_type "->1,"test_hourly_media "->1432,"tt_mini_routine_push "->6,"test_daily_position "->92,"test_app_summary "->1,"test_trend_analysis_debug "->1,"test_daily_phonebrand "->6565,"test_daily_share_summary "->371,"test_third_setup "->1,"user_app_relations "->20,"test_7d_single_link "->2,"tt_field_mapping "->1,"test_event_paras "->684271,"test_7d_entrance_page "->9,"test_dailyshare_user "->548969,"test_region_statistics "->11658,"test_7d_ad_anomaly_all_analyze "->396,"test_30days_visit_depth "->53,"tt_authorize "->6,"test_30days_province "->148,"tt_ad_activity_hourly "->714,"test_7d_link_summary "->1,"oauth_users "->1,"test_monitor_daily_qr "->1,"test_30days_phonebrand "->117,"test_7d_single_media "->1,"tt_db_split "->1,"test_30days_ad_anomaly_ip_analyze "->25,"service_user "->1,"test_monitor_hourly_share "->34,"test_hourly_trend_analysis "->16825,"test_30days_all_qr "->1,"test_trend_analysis "->2196,"aladdin_user "->4,"test_30days_single_qr "->3,"test_daily_page_view "->1295,"tt_stay_logs_bak "->250,"test_hourly_link "->3199,"tt_media "->3,"test_7d_ad_anomaly_auth_analyze "->15,"test_7d_single_qr "->1,"test_daily_qr_group "->37,"test_7d_single_page_view "->70,"user_feedback "->1,"test_user_activity_details "->2486,"test_monitor_daily_share "->1)

我得到了这个结果

Map(test_event_paras  -> 684271)
Map(test_dailyshare_user -> 548969)
Map(tt_wechat_user_bind -> 440049)
Map(test_terminal_analysis -> 91350)
Map(test_daily_qr_group -> 37, tt_stay_logs_bak -> 250, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_monitor_hourly_trend -> 14, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_30days_trend_analysis -> 17, test_7d_terminal_analysis -> 765)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_advertise_home_hour -> 4, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_media -> 3)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_event_link_funnel -> 2)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_sdk_history -> 1)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, user_phone -> 0, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, top100070922 -> 0, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765)

我按照Krzysztof Atłasik的方式重写了一个方法, 结果正确。

def distributeMy(elements: Map[String, Long], bucketCount: Long) = {
implicit val ordering: Ordering[(Long, Map[String, Long])] = Ordering.by(-_._1)
val map = elements.toList.sortBy(-_._2)
var bucket =new mutable.TreeSet[(Long,Map[String,Long])]

map.foreach{
case (k:String,v:Long) =>{
if(bucket.size < bucketCount){
bucket.add((v,Map(k->v)))
}
else{
val element = bucket.last
bucket.remove(bucket.last)
bucket.add((element._1 + v ,element._2 ++ Map(k->v) ))
}
}
}
bucket.toSet
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com