Eric Chen Mock Interview

 1 Given an array with integers.
 2 Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest.
 3 Return the largest difference.
 4 1,-2,3,-1
 5 1,3 A
 6 -1 B
 7 7
 8
 9 public int maxDiffSubArrays(int[] nums) {
10 // write your code here
11
12 }
13
14 Solution:
15
16 public int maxDiffSubArrays(ArrayList<Integer> nums) {
17         // write your code
18         if (nums==null || nums.size()==0) return 0;
19         int len = nums.size();
20         int[] lGlobalMax = new int[len];
21         int[] lGlobalMin = new int[len];
22         int lLocalMax = nums.get(0);
23         int lLocalMin = nums.get(0);
24         lGlobalMax[0] = lLocalMax;
25         lGlobalMin[0] = lLocalMin;
26         for (int i=1; i<len; i++) {
27             lLocalMax = Math.max(lLocalMax+nums.get(i), nums.get(i));
28             lGlobalMax[i] = Math.max(lLocalMax, lGlobalMax[i-1]);
29             lLocalMin = Math.min(lLocalMin+nums.get(i), nums.get(i));
30             lGlobalMin[i] = Math.min(lLocalMin, lGlobalMin[i-1]);
31      }
32         int[] rGlobalMax = new int[len];
33         int[] rGlobalMin = new int[len];
34         int rLocalMax = nums.get(len-1);
35         int rLocalMin = nums.get(len-1);
36         rGlobalMax[len-1] = rLocalMax;
37         rGlobalMin[len-1] = rLocalMin;
38      for (int i=len-2; i>=0; i--) {
39             rLocalMax = Math.max(rLocalMax+nums.get(i), nums.get(i));
40             rGlobalMax[i] = Math.max(rLocalMax, rGlobalMax[i+1]);
41             rLocalMin = Math.min(rLocalMin+nums.get(i), nums.get(i));
42             rGlobalMin[i] = Math.min(rLocalMin, rGlobalMin[i+1]);
43         }
44
45         int maxDiff = Integer.MIN_VALUE;
46         for (int i=0; i<len-1; i++) {
47             if (maxDiff < Math.abs(lGlobalMax[i]-rGlobalMin[i+1]))
48                 maxDiff = Math.abs(lGlobalMax[i]-rGlobalMin[i+1]);
49
50             if (maxDiff < Math.abs(lGlobalMin[i]-rGlobalMax[i+1]))
51                 maxDiff = Math.abs(lGlobalMin[i]-rGlobalMax[i+1]);
52         }
53         return maxDiff;
54}
时间: 2024-11-04 19:41:33

Eric Chen Mock Interview的相关文章

Dropbox Interview – Design Hit Counter

Dropbox Interview – Design Hit Counter It starts with a simple question – if you are building a website, how do you count the number of visitors for the past 1 minute? "Design hit counter" problem has recently been asked by many companies includ

投资银行的IT部门——不同之处与常见误解

投资银行的IT部门——不同之处与常见误解 说了这么多投资银行,投行里面的IT部门究竟是做什么的呢?在过去,投资银行仅靠纸.笔.计算器就能做生意了.但是在今天,所有的部门都要依靠IT技术.交易部门甚至是严重依赖IT技术. 我们可以从两个方面来看IT部门. 从工作的内容上,可以分为系统支持(System Analyst)和开发员(Application Developer).前者和其他公司的IT基本相同,需要维护公司内部的局域网.设备等等.与开发员相比,系统支持的数量要少很多.开发员的职责是开发公司

attr和data

attr()获得属性值 通过attr()获得属性时,可以这样$(selector).attr(attrName).注意,其返回值始终是string型!看下面的例子.通过attr()得到的属性age的值是”31”,属性isstd的值是”false”.另外需要注意的是,attr()对传入的attrName大小写不敏感,就是说attr(’name’)和attr(’NAME’)都能得到期望的返回值. attr()添加和修改属性 attr修改属性支持三种方式,即可以向API传递三种形参:attr(attr

WebApi与手机客户端通信安全机制

WebApi与手机客户端通信安全机制 最近公司有几个项目需要开发手机客户端,服务器端选用WebApi,那么如何保证手机客户端在请求服务器端时数据不被篡改,如何保证一个http请求的失效机制,下面总结一下我们在项目中针对这两个问题的解决方案. 基本思路如下: 用户在成功登陆app客户端之后,手机客户端向服务器端发出的所有的http请求在请求头(HttpHeader)上都会带上下面三个参数:1.Uid(用户ID),2.Ts(时间戳),3.Sign(签名).其中Ts是当前时间减去1970-1-1得到的

jQuery对象data函数使用注意事项

HTML代码: <div class="box" id="jquery-api-data" data-name="chen" data-AGE="31" data-isStd="false" data-func="function(){}" data-list='{"listname":"eric chen"}' data-undefined

Largest product from 3 integers

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Ride%20Share%20Start-Up%20Company/Ride%20Share%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/On-Site%20Question%201%20-%20SOLUT

Hot OS&#39;15 summary

My OS Ought to Know Me Better: In-app Behavioural Analytics as an OS Service Earlence Fernandes, University of Michigan; Oriana Riva and Suman Nath, Microsoft Research Amber: Decoupling User Data from Web Applications Tej Chajed, Jon Gjengset, Jelle

Java简单的数字处理

import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java

Java实现Vector主要操作

1 package Eric.ADT; 2 3 import org.junit.Test; 4 5 /** 6 * <p> 7 * Title:MyVector 8 * </p> 9 * <p> 10 * Description:Implement vector by java 11 * </p> 12 * <p> 13 * Location:Frostburg 14 * </p> 15 * 16 * @author: Eric.C