注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
JP Morgan Chase 24 Summer Intern OA 新鲜面经!
新人求米~
附正确答案,test cases ALL PASS
2道题60分钟
1.Maximum Index Problem
There is an infinite array of integers numbered consecutively from 0. At each step, a pointer can move from index i to index i+j, or remain where it is. The value of i begins at 0. The value of j begins at 1 and at each step, j increments by 1. There is one known index that must be avoided. Determine the highest index that can be reached in a given number of steps.
Example:
steps = 4, badElement = 6 The pointer is limited to 4 steps and should avoid the bad item 6. Scenario 1: In the first step, j starts at 1. Move 1 unit to index 0+1=1 and j=2. At step 2, move 2 units to index 1+2=3 and j/=3. At step 3, do not move. Otherwise, the pointer will move 3 units to the bad item 6. Now i=4. At step 4, move 4 units to item 3+4=7. Scenario 2: At step 1, remain at index 0. Now j=2. At step 2, move 2 units to index 0+2=2 and j=3. At step 3, move 3 units to index 2+3=5 and j=4. At step 4, move 4 units to index 5+9. The maximum index that can be reached is 9.
答案:
2. Balanced Or Not?
Consider a string, expression consisting of the characters < and > only. We consider the string to be balanced if each < always appears before (i.e., to the left of) a corresponding > character (they do not need to be adjacent). Moreover, each < and > act as t color="#ff8c00">)
def balancedOrNot(expressions, maxReplacements):
results = []
for i in range(len(expressions)):
expression = expressions[i]
replacements = maxReplacements[i]
count = 0
unbalanced = False
for char in expression:
if char == "<":
count += 1
elif char == ">":
if count == 0:
if replacements <= 0:
unbalanced = True
break
replacements -= 1
else:
count -= 1
if count == 0 and not unbalanced:
results.append(1)
else:
results.append(0)
return results
谢谢各位客官赏大米~
END |