Skip to content

Instantly share code, notes, and snippets.

@kanglicheng
Last active April 6, 2018 01:31
Show Gist options
  • Save kanglicheng/e3343af8122b60c68cbdd3ce9c938c1e to your computer and use it in GitHub Desktop.
Save kanglicheng/e3343af8122b60c68cbdd3ce9c938c1e to your computer and use it in GitHub Desktop.
practice with bruce
function findMaxHalfSum(arr) {
let max = 0;
let answer;
let hash = {};
for (let k = 0; k <= arr.length/2; k++) {
let leftHalf = arr.slice(0, k).reduce((a,b) => {return a+b}, 0);
let rightHalf = arr.slice(arr.length/2 + k).reduce((a,b) => {return a+b}, 0);
if (leftHalf + rightHalf > max) {
max = leftHalf + rightHalf;
answer = k;
}
}
return answer;
}
console.log(findMaxHalfSum([1,5,2,3]));
class Solution(object):
def __init__(self, nums):
self.nums = nums
def pick(self, target):
from random import randint
hash = {}
hash[target] = []
for i in range(len(self.nums)):
if self.nums[i] == target:
hash[target].append(i)
indices = hash[target]
random_index= randint(0, len(indices)-1)
return indices[random_index]
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment