leetcode solve
leetcode solve
Given a non-empty array of integers nums, every element appears twice except for one. Find that
single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Output: 1
Example 2:
Output: 4
Example 3:
Output: 1
Constraints:
o Each element in the array appears twice except for one element which appears only
once.
subarray
its sum
.
Example 1:
Output: 6
Example 2:
Output: 1
Example 3:
Output: 23
Constraints:
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and
conquer approach, which is more subtle.
class Solution {
public:
//kadane's algorithm
int currentSum = 0;
maxSum = max(currentSum,maxSum);
currentSum = 0;
return maxSum;
};
• class Solution {
• public:
• /* int n = nums.size();
• int frequency = 0;
• if(val == ele){
• frequency++;
• }
• }
• return val;
• }
• }
•
• return -1; */
• /* int n = nums.size();
• sort(nums.begin(),nums.end());
• int fre = 1;
• fre++;
• }else{
• fre = 1;
• ans = nums[i];
• }
• return ans;
• }
• }
• return ans; */
• int fre = 0;
• int ans = 0;
• if(fre ==0){
• ans = nums[i];
• } if(ans == nums[i]){
• fre++;
• }else{
• fre--;
• }
• }
• return ans;
• }
• };
• Leetcode 50 || Pow(x, n)
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
Example 1:
Input: x = 2.00000, n = 10
Output: 1024.00000
Example 2:
Input: x = 2.10000, n = 3
Output: 9.26100
Example 3:
Input: x = 2.00000, n = -2
Output: 0.25000
Constraints:
o n is an integer.
public:
double ans = 1;
while(binForm>0){
return ans;
};
You want to maximize your profit by choosing a single day to buy one stock and choosing a different
day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit,
return 0.
Example 1:
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
class Solution {
public:
int bestBuy = prices[0]; // store first day as the best buy day
int maxProfit = 0;
if(prices[i] > bestBuy){ // compare bestBuy number with the i th day element
}
bestBuy = min(bestBuy,prices[i]); //find minimum between previous price and i'th day price
return maxProfit;
};
• class Solution {
• public:
• because Constraints:
• n == height.length
• /* int maxWater = 0;
• int heightWater = min(height[i], height[j]); // find minimum water height level from
seleted two line
• }
• }
• return maxWater; */
•
• // 2 Pointer Approch : O(n)
• int maxWater = 0;
• int leftBar = 0;
• maxWater = max(maxWater,area);
• }
• return maxWater;
• }
• };
Solved
Medium
Topics
Companies
Hint
You are given an integer array height of length n. There are n vertical lines drawn such that the two
endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the
most water.
!https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max
area of water (blue section) the container can contain is 49.
Example 2:
Output: 1
Constraints:
o n == height.length
• https://brainstation.io/career-guides/software-engineer-interview-
questions?utm_source=chatgpt.com