12
返回列表 发新帖
楼主: 文体两开花
跳转到指定楼层
上一主题 下一主题
收起左侧

eBay新鲜面经

🔗
seasean 2017-11-15 13:02:13 | 只看该作者
全局:
格格笑 发表于 2017-11-14 23:53
backend  烙印黑的飞起来~

感谢楼主分享,能问一下就是backend组么,还是sre
回复

使用道具 举报

🔗
desperatelife 2017-11-17 13:03:02 | 只看该作者
全局:
  1. int result = Integer.MIN_VALUE;
  2.                 for (int i = 0; i < n - k; i++) {
  3.                         int currMax = nums[i];
  4.                         for (int j = i + 1; j < i + k; j++) {
  5.                                 currMax = Math.max(nums[i], currMax + nums[j]);
  6.                                 result = Math.max(result, currMax);
  7.                         }
  8.                 }
复制代码

只想到了nk的解法
回复

使用道具 举报

🔗
jsgq 2017-11-23 08:40:37 | 只看该作者
全局:
求问面的是哪个组的backend
回复

使用道具 举报

全局:
抄的geeks for geeks 烙印解法,o(n)
public class LongestSubArrayHavingSumK {
         public static int atMostSum(int[] nums,int k){
         int sum = 0;
         int cnt = 0, maxcnt = 0;
         for (int i = 0; i < nums.length; i++) {
             if ((sum + nums[i]) <= k) {
                 sum += nums[i];
                 cnt++;
             }
             else if(sum!=0){
                 sum = sum - nums[i - cnt] + nums[i];
             }
             maxcnt = Math.max(cnt, maxcnt);
         }
         return maxcnt;
     }

     public static void main(String[] args){
         int[] nums = { 1, 2, 1, 0, 1, 1, 0 };
         int k = 4;
         System.out.print(atMostSum(nums,k));
     }
}
回复

使用道具 举报

🔗
 楼主| 文体两开花 2017-12-8 03:00:40 | 只看该作者
全局:
翻到自己这页,给大家发个我当时写的完整版吧 233

import java.util.*;

public class eBayIllustration{
    public static void main(String[] args) {
        int[] input = new int[]{10, 30, -40, 3};
        eBayIllustration sol = new eBayIllustration();
        int[] res = sol.maxSubarrayAtMostK(input, 2);
        if (res == null) {
            System.out.println("no one was selected");
        } else {
            System.out.println(res[0] + "~" + res[1]);
        }
    }

    // basically, my idea is maintain a deque to contains currently valid min value
    // valid means the min value must be in the scope of k from current i
    // Time: O(N)
    // Space: O(N)
    public int[] maxSubarrayAtMostK(int[] input, int k) {
        if (input == null || input.length == 0) {
            return new int[0];
        }
        // Preprocessing
        // nums[i]  represents the sum : input[0] + ... input[i - 1]
        int[] nums = new int[input.length + 1];
        for (int i = 1; i < nums.length; i++) {
            nums[i] = nums[i - 1] + input[i - 1];
        }
        Deque<Integer> deque = new LinkedList<>();
        int max = 0;
        int[] candidate = null;
        for (int i = 0; i < nums.length; i++) {
            while (!deque.isEmpty() && nums[deque.peekLast()] >= nums[i]) {
                deque.pollLast();
            }
            deque.addLast(i);
            // out of scope
            if (deque.peekFirst() < i - k) {
                deque.pollFirst();
            }
            // if better subarray appear, update the result/candidate
            if (nums[i] - nums[deque.peekFirst()] > max) {
                max = nums[i] - nums[deque.peekFirst()];
                candidate = new int[]{deque.peekFirst(), i - 1}; //inclusive idx
            }
        }
        return candidate;// e.g  [1,3] 1 means the left bound 3 means the right bound of the subarray of input, bothe left and right bound are inclusive.
    }
}
回复

使用道具 举报

🔗
cicean 2017-12-11 02:37:36 | 只看该作者
全局:
楼主这题是 莉蔻 上 尔三舅 么?
Sliding Window Maximum
回复

使用道具 举报

🔗
cicean 2017-12-11 02:41:19 | 只看该作者
本楼:
全局:
是道hard
回复

使用道具 举报

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

本版积分规则

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