Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 62cff8d

Browse files
committed
443. String Compression
1 parent efb5249 commit 62cff8d

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
1047. Remove All Adjacent Duplicates In String
3+
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/
4+
*/
5+
6+
/* TIME COMPLEXITY O(N) */
7+
8+
/**
9+
* @param {string} s
10+
* @return {string}
11+
*/
12+
var removeDuplicates = function (s) {
13+
let arrStack = [] // Create a stack whic contain non-repeate element
14+
for (const ch of s) {
15+
if (arrStack.length && arrStack[arrStack.length - 1] === ch) // If the stack top elemt is match to the string character then remove that top element.
16+
arrStack.pop()
17+
else arrStack.push(ch) // if not match add to the array.
18+
}
19+
return arrStack.join('')
20+
};
21+
22+
console.log(removeDuplicates("aababaab"));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
121. Best Time to Buy and Sell Stock
3+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4+
5+
*/
6+
7+
/**
8+
* @param {number[]} prices
9+
* @return {number}
10+
*/
11+
12+
// This is the solution but Time Limit Exceeded
13+
14+
/*Method 1 */
15+
/* TIME COMPLEXITY O(N) */
16+
17+
var maxProfit2 = function (prices) {
18+
var minimumSofar = prices[0]; // Create a minimum veriable to store the minimux value in the array
19+
var maxProfit = 0; // Create a maximum veriable to store the maximun value
20+
for (let i = 0; i < prices.length; i++) {
21+
{
22+
if (minimumSofar > prices[i]) { // Check that the Ith possitino value is less then the minimum value if condition is true modify the minimum value to new value.
23+
minimumSofar = prices[i];
24+
}
25+
26+
if ((prices[i] - minimumSofar) > maxProfit) { // check Ith possition value minus minimum value profit is greater then the MaxProfit if condition is true then modify our maxProfit
27+
maxProfit = (prices[i] - minimumSofar);
28+
}
29+
}
30+
}
31+
return maxProfit;
32+
}
33+
34+
/*Method 2 */
35+
/* TIME COMPLEXITY O(N2) */
36+
37+
var maxProfit1 = function (prices) {
38+
var max = 0;
39+
var start = 0;
40+
for (let i = 1; i < prices.length; i++) {
41+
if ((prices[start] < prices[i]) && (prices[i] - prices[start]) > max) {
42+
max = prices[i] - prices[start];
43+
}
44+
if (i == prices.length - 1) {
45+
i = ++start;
46+
}
47+
}
48+
return max;
49+
50+
}
51+
console.log(maxProfit2([7, 1, 5, 3, 6, 4]));

‎Medium/443. String Compression.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
443. String Compression
3+
https://leetcode.com/problems/string-compression/
4+
*/
5+
6+
/**
7+
* @param {character[]} chars
8+
* @return {number}
9+
*/
10+
/*
11+
12+
Input:
13+
["a"]
14+
Output:
15+
[]
16+
Expected:
17+
["a"]
18+
19+
Input:
20+
["a","a","a","b","b","a","a"]
21+
Output:
22+
["a","5","b","2"]
23+
Expected:
24+
["a","3","b","2","a","2"]
25+
26+
Input:
27+
["a","b","b","b","b","b","b","b","b","b","b","b","b"]
28+
Output:
29+
["a","b","12"]
30+
Expected:
31+
["a","b","1","2"]
32+
33+
34+
*/
35+
36+
37+
var compress = function (chars) {
38+
if (chars.length == 1) {
39+
return chars.length;
40+
}
41+
var currentValue = chars[0];
42+
var count = 1;
43+
var arr = [];
44+
for (let i = 1; i < chars.length; i++) {
45+
if (currentValue === chars[i]) {
46+
count++;
47+
} else {
48+
arr.push(currentValue);
49+
if (count > 9) {
50+
`${count}`.split("").map((value) => {
51+
arr.push(value);
52+
});
53+
} else {
54+
if (count != 1) {
55+
arr.push(count.toString());
56+
}
57+
}
58+
currentValue = chars[i];
59+
count = 1;
60+
}
61+
62+
if (i == chars.length - 1) {
63+
arr.push(currentValue);
64+
if (count > 9) {
65+
`${count}`.split("").map((value) => {
66+
arr.push(value);
67+
});
68+
} else {
69+
if (count != 1) {
70+
arr.push(count.toString());
71+
}
72+
}
73+
}
74+
}
75+
chars.splice(0);
76+
for (let i = 0; i < arr.length; i++) {
77+
chars.push(arr[i]);
78+
}
79+
return chars;
80+
};
81+
console.log(compress(["a", "b", "b", "b", "c", "c", "a", "a", "a"]));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /