LeetCode 1322: Ads Performance Solution
Master LeetCode problem 1322 (Ads Performance), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1322. Ads Performance
Problem Explanation
Explanation:
To solve this problem, we need to calculate the percentage of ads that received positive feedback based on the given input arrays of the number of clicks and the number of impressions for each ad.
We can calculate the click-through rate (CTR) for each ad by dividing the number of clicks by the number of impressions and then multiply by 100 to get the percentage. If the CTR is greater than or equal to 1%, we consider it a positive feedback ad.
Here are the steps to solve the problem:
- Initialize a variable
positiveAdsto keep track of the number of ads with positive feedback. - Iterate through each ad and calculate the CTR using the formula
clicks / impressions * 100. - If the CTR is greater than or equal to 1%, increment the
positiveAdscount. - Finally, return the percentage of positive feedback ads by dividing
positiveAdsby the total number of ads and multiplying by 100.
Time Complexity: O(n) where n is the number of ads Space Complexity: O(1)
: :
Solution Code
class Solution {
public int[] percentageAds(int[] clicks, int[] impressions) {
int positiveAds = 0;
int n = clicks.length;
for (int i = 0; i < n; i++) {
double ctr = (double)clicks[i] / impressions[i] * 100;
if (ctr >= 1.0) {
positiveAds++;
}
}
return new int[]{positiveAds * 100 / n};
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1322 (Ads Performance)?
This page provides optimized solutions for LeetCode problem 1322 (Ads Performance) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1322 (Ads Performance)?
The time complexity for LeetCode 1322 (Ads Performance) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1322 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1322 in Java, C++, or Python.