LeetCode first question and time complexity calculation, leetcode Calculation

Source: Internet
Author: User

LeetCode first question and time complexity calculation, leetcode Calculation

Problem description: Given a group of specified integer arrays, find the two numbers in the array that are equal to or equal to a specific number.

Function (method) twoSum returns the indexes of these two numbers. index1 must be smaller than index2.

In addition, you can assume that an array has only one group of solutions.

One chestnut:

Input: numbers = {2, 7, 11, 15}, target = 9 Output: index1 = 1, index2 = 2

 

The algorithm is implemented as follows:

1/** 2 * time complexity O (n) 3 * @ param array 4 * @ param target 5 * @ return Map <Integer, integer> 6 */7 public static Map <Integer, Integer> twoSum (int [] array, int target) {8 9 // Map <value, index> 10 Map <Integer, integer> result = new HashMap <Integer, Integer> (); 11 12 Map <Integer, Integer> container = new HashMap <Integer, Integer> (); 13 for (int I = 0; I <array. length; I ++) {14 if (! Container. containsKey (target-array [I]) {15 container. put (array [I], I + 1); 16} else {17 result. put (target-array [I], container. get (target-array [I]); 18 result. put (array [I], I + 1); 19 break; 20} 21} 22 23 return result; 24}

In addition, the implementation of the algorithm for double-layer cyclic judgment is not listed here because the time complexity is O (n².

 

Computing of time complexity

One chestnut:
  

1 int value = 0; // This code is executed once. 2 for (int I = 0; I <n; I ++) {3 value + = n; // This code is executed n times 4}

The algorithm executes 1 + n times. If n approaches infinity, 1 is negligible, that is, the algorithm executes n times. The time complexity is usually expressed by the O symbol. the time complexity of this algorithm is O (n ).

 

  When an algorithm is computing time complexity, the following rules can be observed:
1) Remove all addition constants in the running time.
2). Only the highest-order items are retained.
3) if the maximum order does not exist and is not 1, remove the constant multiplied by the maximum order to get the time complexity.

Another Chestnut
  

1 for (int i = 0; i < n; i++) {2     for (int j = i; j < n; j++) {3         // do something4     }5 }    

 


When I = 0, the fo loop in it is executed n times, And when I wait 1, The for loop in it is executed n-1 times, when I is equal to 2, fro executes n-2 times ........ therefore, the number of executions is:
N + (n-1) + (n-2) +... + 1
= N (n + 1)/2
= N²/2 + n/2


Based on our above Time Complexity Algorithm
1. Remove all addition constants during running time: no addition constants are required.
2. retain only the highest order items: retain only n²/2
3. Remove the constant that is multiplied by the highest order: Remove 1/2 and leave only n².
In the end, the time complexity of this algorithm is O (n²)


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.