【Leetcode】1395. Count Number of Teams
題目地址:
https://leetcode.com/problems/count-number-of-teams/
給定一個長 n n n陣列 A A A,題目保證無重複數字,問其有多少個三元組 0 ≤ i < j < k < n 0\le i<j<k<n 0≤i<j<k<n使得 A [ i ] < A [ j ] < A [ k ] A[i]<A[j]<A[k] A[i]<A[j]<A[k]或者 A [ i ] > A [ j ] > A [ k ] A[i]>A[j]>A[k] A[i]>A[j]>A[k]。
列舉中間數字,然後累加一下左邊比它小的個數乘以右邊比它大的個數,再累加左邊比它大的個數乘以右邊比它小的個數。程式碼如下:
public class Solution {
public int numTeams(int[] rating) {
int res = 0;
for (int i = 1; i < rating.length - 1; i++) {
int leftGreater = 0, rightGreater = 0, leftSmaller = 0, rightSmaller = 0;
for (int j = 0; j < i; j++) {
if (rating[j] > rating[i]) {
leftGreater++;
} else {
leftSmaller++;
}
}
for (int j = i + 1; j < rating.length; j++) {
if (rating[j] > rating[i]) {
rightGreater++;
} else {
rightSmaller++;
}
}
res += leftSmaller * rightGreater;
res += leftGreater * rightSmaller;
}
return res;
}
}
時間複雜度 O ( n 2 ) O(n^2) O(n2),空間 O ( 1 ) O(1) O(1)。
相關文章
- Leetcode Number of islandsLeetCode
- Leetcode 38 Count and SayLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- [LeetCode] 2491. Divide Players Into Teams of Equal SkillLeetCodeIDE
- Leetcode 1366. Rank Teams by Votes (python+cpp)LeetCodePython
- Leetcode 933 Number of Recent CallsLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- [LeetCode] 811. Subdomain Visit CountLeetCodeAI
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 17 Letter Combinations of a Phone NumberLeetCode
- Leetcode 137. Single Number IILeetCode
- [LeetCode] 2257. Count Unguarded Cells in the GridLeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- Leetcode 202 Happy Number Javascript 解決方案LeetCodeAPPJavaScript
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- [LeetCode] 2960. Count Tested Devices After Test OperationsLeetCodedev
- [LeetCode] 3184. Count Pairs That Form a Complete Day ILeetCodeAIORM
- 【Leetcode】1180. Count Substrings with Only One Distinct LetterLeetCode
- [LeetCode] 1366. Rank Teams by Votes 透過投票對團隊排名LeetCode
- LeetCode Letter Combinations of a Phone Number(017)解法總結LeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- 222. Count Complete Tree Nodes(Leetcode每日一題-2020.11.24)LeetCode每日一題
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode