You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise. 确认在数组中的最大数是否是其余任意数的两倍大及以上,如果是返回索引,如果不是返回-1
示例
Example 1:
Input: nums = [3,6,1,0] Output: 1 Explanation: 6 is the largest integer. For every other number in the array x, 6 is at least twice as big as x. The index of value 6 is 1, so we return 1.
Example 2:
Input: nums = [1,2,3,4] Output: -1 Explanation: 4 is less than twice the value of 3, so we return -1.
EventHandlerGroup<T> createEventProcessors( final Sequence[] barrierSequences, final EventHandler<? super T>[] eventHandlers) { checkNotStarted();
final Sequence[] processorSequences = newSequence[eventHandlers.length]; finalSequenceBarrierbarrier= ringBuffer.newBarrier(barrierSequences);
for (inti=0, eventHandlersLength = eventHandlers.length; i < eventHandlersLength; i++) { final EventHandler<? super T> eventHandler = eventHandlers[i];
publicvoidrun() { if (running.compareAndSet(IDLE, RUNNING)) { sequenceBarrier.clearAlert();
notifyStart(); try { if (running.get() == RUNNING) { processEvents(); } } finally { notifyShutdown(); running.set(IDLE); } } else { // This is a little bit of guess work. The running state could of changed to HALTED by // this point. However, Java does not have compareAndExchange which is the only way // to get it exactly correct. if (running.get() == RUNNING) { thrownewIllegalStateException("Thread is already running"); } else { earlyExit(); } } }
Map<String, Object> propertySourcesPlaceholderPropertyValues = newHashMap<>(); // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer propertySourcesPlaceholderPropertyValues.put("order", 0);
LocalFileConfigRepository createLocalConfigRepository(String namespace) { if (m_configUtil.isInLocalMode()) { logger.warn( "==== Apollo is in local mode! Won't pull configs from remote server for namespace {} ! ====", namespace); returnnewLocalFileConfigRepository(namespace); } // 正常会走这个,因为要从配置中心获取 returnnewLocalFileConfigRepository(namespace, createRemoteConfigRepository(namespace)); }
com.ctrip.framework.apollo.internals.RemoteConfigRepository#loadApolloConfig private ApolloConfig loadApolloConfig() { if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) { //wait at most 5 seconds try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { } } StringappId= m_configUtil.getAppId(); Stringcluster= m_configUtil.getCluster(); StringdataCenter= m_configUtil.getDataCenter(); Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace)); intmaxRetries= m_configNeedForceRefresh.get() ? 2 : 1; longonErrorSleepTime=0; // 0 means no sleep Throwableexception=null;
// 获取配置 List<ServiceDTO> configServices = getConfigServices(); Stringurl=null; for (inti=0; i < maxRetries; i++) { List<ServiceDTO> randomConfigServices = Lists.newLinkedList(configServices); Collections.shuffle(randomConfigServices); //Access the server which notifies the client first if (m_longPollServiceDto.get() != null) { randomConfigServices.add(0, m_longPollServiceDto.getAndSet(null)); }
for (ServiceDTO configService : randomConfigServices) { if (onErrorSleepTime > 0) { logger.warn( "Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
if (response.getStatusCode() == 304) { logger.debug("Config server responds with 304 HTTP status code."); return m_configCache.get(); }
ApolloConfigresult= response.getBody();
logger.debug("Loaded config for {}: {}", m_namespace, result);
return result; } catch (ApolloConfigStatusCodeException ex) { ApolloConfigStatusCodeExceptionstatusCodeException= ex; //config not found if (ex.getStatusCode() == 404) { Stringmessage= String.format( "Could not find config for namespace - appId: %s, cluster: %s, namespace: %s, " + "please check whether the configs are released in Apollo!", appId, cluster, m_namespace); statusCodeException = newApolloConfigStatusCodeException(ex.getStatusCode(), message); } Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(statusCodeException)); transaction.setStatus(statusCodeException); exception = statusCodeException; } catch (Throwable ex) { Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex)); transaction.setStatus(ex); exception = ex; } finally { transaction.complete(); }
// if force refresh, do normal sleep, if normal config load, do exponential sleep onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() : m_loadConfigFailSchedulePolicy.fail(); }
com.ctrip.framework.apollo.internals.RemoteConfigRepository#getConfigServices private List<ServiceDTO> getConfigServices() { List<ServiceDTO> services = m_serviceLocator.getConfigServices(); if (services.size() == 0) { thrownewApolloConfigException("No available config service"); }
return services; }
1 2 3 4 5 6 7 8
com.ctrip.framework.apollo.internals.ConfigServiceLocator#getConfigServices public List<ServiceDTO> getConfigServices() { if (m_configServices.get().isEmpty()) { updateConfigServices(); }
Given an integer array nums, return the sum of floor(nums[i] / nums[j]) for all pairs of indices 0 <= i, j < nums.length in the array. Since the answer may be too large, return it modulo 10^9 + 7.
The floor() function returns the integer part of the division.
Input: nums = [2,5,9] Output: 10 Explanation: floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0 floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1 floor(5 / 2) = 2 floor(9 / 2) = 4 floor(9 / 5) = 1 We calculate the floor of the division for every pair of indices in the array then sum them up.
Example 2:
Input: nums = [7,7,7,7,7,7,7] Output: 49
Constraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^5
简析
这题不愧是 hard,要不是看了讨论区的一个大神的解答感觉从头做得想好久, 主要是两点,对于任何一个在里面的数,随便举个例子是 k,最简单的就是循环所有数对 k 除一下, 这样效率会很低,那么对于 k 有什么规律呢,就是对于所有小于 k 的数,往下取整都是 0,所以不用考虑, 对于所有大于 k 的数我们可以分成一个个的区间,[k,2k-1),[2k,3k-1),[3k,4k-1)……对于这些区间的 除了 k 往下取整,每个区间内的都是一样的,所以可以简化为对于任意一个 k,我只要知道与k 相同的有多少个,然后比 k 大的各个区间各有多少个数就可以了
/** * Here is the brief description for all the predefined environments: * <ul> * <li>LOCAL: Local Development environment, assume you are working at the beach with no network access</li> * <li>DEV: Development environment</li> * <li>FWS: Feature Web Service Test environment</li> * <li>FAT: Feature Acceptance Test environment</li> * <li>UAT: User Acceptance Test environment</li> * <li>LPT: Load and Performance Test environment</li> * <li>PRO: Production environment</li> * <li>TOOLS: Tooling environment, a special area in production environment which allows * access to test environment, e.g. Apollo Portal should be deployed in tools environment</li> * </ul> */