京东算法笔试求交流

1.股票,求最小损失。时间超时,27%
n,m = list(map(int,input().strip().split()))
readarr = list(map(int,input().strip().split()))[:n]
num = int(input().strip())
readarr.sort()
try:
    for j in range(num):
        choose = int(input().strip())
        arr = readarr[:choose][::-1]
        sum = 0
        for i in range(len(arr)):
            sum += ((i // m) + 1) * arr[i]
        print(str(int(sum)))
except:
    pass
2.求点组成的平行线段个数。通过67%,感觉自己题意理解的有点问题
#!/bin/python
# -*- coding: utf8 -*-
import sys
import os
import re


def fun(n):
    temp = []
    re = {}
    re['inf']=0
    for i in range(n):
        x, y = list(map(int, input().strip().split()))[:2]
        if len(temp)==0:
            temp.append([x,y])
        else:
            for tem in temp:
                if (tem[0]-x)==0:
                    re['inf'] += 1
                elif (tem[1]-y)/(tem[0]-x) not in re:
                    re[(tem[1]-y)/(tem[0]-x)] = 1
                else:
                    re[(tem[1] - y) / (tem[0] - x)] += 1
            temp.append([x, y])
    count = 0
    m = 0
    for k,v in re.items():
        if v>m:
            m = v
    print((m)*(m-1)//2)

if __name__=="__main__":
    n = int(input().strip())
    fun(n)




#京东笔试##京东##笔试题目#
全部评论
第一题心态爆炸,怎么算都是27
点赞 回复
分享
发布于 2020-04-18 21:01
股票我也27、、
点赞 回复
分享
发布于 2020-04-18 21:06
滴滴
校招火热招聘中
官网直投
第二题呢
点赞 回复
分享
发布于 2020-04-18 21:38

相关推荐

1 1 评论
分享
牛客网
牛客企业服务