查看: 1139| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

[找工就业] amazon oa解法求讨论

全局:

2025(10-12月)-CS硕士+短暂实习或全职不超过3个月 | 网上海投|BayArea湾区 码农类General全职@Amazon


如图 想问问大家的解法,我现在想到的是用map来算出每一个元素出现的频率 然后sort之后一个个相乘可能性,求更好的方法

本帖子中包含更多资源

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x

上一篇:Oracle OFSAA Consulting
下一篇:感觉地里很多内推是骗简历
🔗
sun090807 2024-10-31 06:18:47 | 只看该作者
全局:
排序,treemap 找到附近的+-wt元素,然后排列组合, 过程中取余。
回复

使用道具 举报

🔗
 楼主| 小亩_aw5mueh 2024-10-31 06:31:31 | 只看该作者
全局:
sun090807 发表于 2024-10-30 15:18
排序,treemap 找到附近的+-wt元素,然后排列组合, 过程中取余。

求问这里的排列组合应该是n!吧
回复

使用道具 举报

🔗
sun090807 2024-10-31 06:37:11 | 只看该作者
全局:
小亩_aw5mueh 发表于 2024-10-30 17:31. Waral dи,
求问这里的排列组合应该是n!吧

为什么是n! ?,2 2 2 3 3 4, wt =1,. Waral dи,
treemap:. Χ
<element count>
<2 3>
<3 2>
<4 1>. check 1point3acres for more.
对于第一个2而言 可能的组合不就是 (3-1)+2+1吗。。。这里感觉是不是用segment tree 更好,因为要求和。
回复

使用道具 举报

🔗
 楼主| 小亩_aw5mueh 2024-10-31 06:44:07 | 只看该作者
全局:
import java.util.HashMap;
import java.util.Map;

public class ParcelPairing {
    static final int MOD = 1_000_000_007;

    public static int countBalancedGroups(int[] weights, int wt) {
        int n = weights.length;

        // If odd number of parcels, no valid grouping possible
        if (n % 2 != 0) return 0;. 1point 3 acres
. Χ
        // Create frequency map of weights
        Map<Integer, Integer> freqMap = new HashMap<>();. 1point3acres.com
        for (int weight : weights) {
            freqMap.put(weight, freqMap.getOrDefault(weight, 0) + 1);
        }
. 1point 3acres
        long result = 1;-baidu 1point3acres
. .и
        // Process each unique weight
        for (int weight : freqMap.keySet()) {
            int currentFreq = freqMap.get(weight);

            // Check for pairs with weight + wt.google  и
            int targetWeight = weight + wt;. ----
            if (freqMap.containsKey(targetWeight)) {
                int targetFreq = freqMap.get(targetWeight);
                if (currentFreq != targetFreq) return 0; // Must be equal to pair all
                result = (result * factorial(currentFreq)) % MOD;. 1point3acres
                freqMap.put(targetWeight, 0); // Mark as processed
            }
. 1point3acres.com
            freqMap.put(weight, 0); // Mark as processed
        }

        return (int) result;.
    }
-baidu 1point3acres
    // Calculate factorial modulo MOD
    private static long factorial(int n) {
        long result = 1;
        for (int i = 1; i <= n; i++) {
            result = (result * i) % MOD;
        }
        return result;
    }
.--
    public static void main(String[] args) {
        int[] weights = {4, 5, 5, 4, 4, 5, 2, 3};
        int wt = 1;
        System.out.println(countBalancedGroups(weights, wt)); // Should print 6
    }
}gpt给的算法但是不对
回复

使用道具 举报

🔗
 楼主| 小亩_aw5mueh 2024-10-31 06:52:39 | 只看该作者
全局:
sun090807 发表于 2024-10-30 15:37
为什么是n! ?,2 2 2 3 3 4, wt =1,
treemap:
. ----
主要是在考虑如果看n+1,n-1的话就要讨论两种情况了,如果n+1满足会如何,n-1满足会如何,因为只有一个n,并且如果说出现了n和n+1,n和n-1都满足的情况 11222333444 对于3来说就都满足,此时应该就直接return 0了
回复

使用道具 举报

🔗
shuow 2024-10-31 08:59:25 来自APP | 只看该作者
全局:
楼主有更多test吗? gpt错在没有更新map吧,key从小往大扫一遍,用过了要扣掉freq
  1. class Solution {
  2.     static int MOD = 1_000_000_000 + 7;. 1point 3 acres
  3.     public static int countBalancedGroups(int[] weights, int wt) {
  4.         long ans = 1;
  5. . ----
  6.         int n = weights.length;
  7.         if (n % 2 == 1) {
  8.             return 0; // impossible
  9.         }

  10.         Map<Integer, Integer> map = new TreeMap<>();
  11.         for (int i = 0; i < weights.length; i++) {
  12.             int freq = map.getOrDefault(weights[i], 0);. .и
  13.             map.put(weights[i], freq + 1);. ----
  14.         }

  15.         for (int key: map.keySet()) {
  16.             // check
  17.             int curr_freq = map.get(key);. 1point 3acres
  18.             if (curr_freq == 0) {
  19.                 continue;
  20.             }

  21.             if (!map.containsKey(key + wt)) {
  22.                 return 0;
  23.             } ..
  24. .--
  25.             int next_freq = map.get(key + wt);
  26.             // impossible
  27.             if (curr_freq > next_freq) {
  28.                 return 0;. 1point3acres.com
  29.             }

  30.             map.put(key+wt, next_freq-curr_freq);
  31. . 1point 3acres
  32.             ans *= nChooseK(next_freq, curr_freq) * factorial(curr_freq);
  33.             ans %= MOD;

  34.         }
  35.         return (int) (ans % MOD);
  36.     }

  37.     public static long factorial(int n) {
  38.         if (n == 1 || n == 0) {
  39.             return 1;
  40.         }
  41.         return n * factorial(n-1);
  42.     }

  43.     public static long nChooseK(int n, int k) {
  44.         return factorial(n) / (factorial(n-k) * factorial(k));
  45.     }
  46. }
复制代码

补充内容 (2024-10-31 09:02 +08:00):

而且 222 3333 4 这种情况得是4个3选三个跟2匹配(有4选3种选法) * (有3!种匹配法)吧
数学不是很好,不太会算
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表