【Lintcode】1793. Balanced Sales Array
題目地址:
https://www.lintcode.com/problem/balanced-sales-array/description
給定一個陣列 A A A,求某個下標 k k k使得 ∑ i = 1 k − 1 A [ i ] = ∑ i = k + 1 l A − 1 A [ i ] \sum_{i=1}^{k-1}A[i]=\sum_{i=k+1}^{l_A-1}A[i] i=1∑k−1A[i]=i=k+1∑lA−1A[i]題目保證解存在。
思路是字首和,可以降低時間複雜度。程式碼如下:
public class Solution {
/**
* @param sales: a integer array
* @return: return a Integer
*/
public int BalancedSalesArray(int[] sales) {
// write your code here
int[] preSum = new int[sales.length + 1];
for (int i = 0; i < sales.length; i++) {
preSum[i + 1] = preSum[i] + sales[i];
}
for (int i = 0; i < sales.length; i++) {
if (preSum[i] == preSum[sales.length] - preSum[i + 1]) {
return i;
}
}
return -1;
}
}
時空複雜度 O ( l A ) O(l_A) O(lA)。
相關文章
- LintCode-Partition Array
- 【Lintcode】576. Split Array
- 【Lintcode】1623. Minimal Distance In The Array
- Balanced Subsequences
- SAP Cloud for Customer裡Sales Order和Sales Quote的建模方式Cloud
- SCM605--Controlling Sales Documents with Sales Document Types
- Leetcode Balanced Binary TreeLeetCode
- How to get propose products in Sales Order
- What is the difference between gross sales and revenue?ROS
- Additional Data Tab in Sales Order
- 【RMQ】poj 3264 Balanced LineupMQ
- Leetcode-Balanced Binary TreeLeetCode
- Balanced Binary Tree leetcode javaLeetCodeJava
- array new 與 array deletedelete
- SAP CRM Fiori應用如何啟用Sales Office和Sales Group兩個欄位
- PHP array_flip() array_merge() array+array的使用總結PHP
- marketing和sales的區別
- POJ3264 Balanced Lineup【RMQ】MQ
- array
- Array()與Array.of()方法區別
- JS Array.reduce 實現 Array.map 和 Array.filterJSFilter
- PHP用foreach來表達array_walk/array_filter/array_map/array_reducePHPFilter
- 淺談SAP Cloud for Sales 自動化Cloud
- Sales Order Form · Additional Line InformationORM
- Create sales scheduling agreement with FM
- SCM600--Master Data in Sales and DistributionAST
- POJ 3264 Balanced Lineup【RMQ問題】MQ
- LeetCode(110) Balanced Binary TreeLeetCode
- [題解]SP10606 Balanced Numbers
- array_filter ()、array_map ()、array_walk () 區別?容易記混淆!!!Filter
- [LintCode] Daily TemperaturesAI
- LintCode 子樹
- LintCode-Backpack
- LintCode-HeapifyAPI
- Array物件物件
- Array Repetition
- Unique Array
- waring:in_array()[function.in-array]:wrongdatatypeforsecondargumentFunction