Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem added named playlist #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/problems/playlist/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Playlist

You are given a playlist of a radio station since its establishment. The playlist has a total of `n` songs. What is the longest sequence of successive songs where each song is unique?


### Output

Return the length of the longest sequence of unique songs.

## Testcase 1

### Input:

[1, 2, 1, 3, 2, 7, 4, 2]
### Output:

5


### Explanation:
The longest sequence of unique songs is `[1, 3, 2, 7, 4]` which has a length of 5.

## Constraints
- (1 ≤ n ≤ 2 × 10^5)
- (1 ≤ arr[i] ≤ 10^9)
6 changes: 6 additions & 0 deletions apps/problems/playlist/Structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Problem Name: "Playlist"
Function Name: longestSequence
Input Structure:
Input Field: list<int> arr
Output Structure:
Output Field: int result
17 changes: 17 additions & 0 deletions apps/problems/playlist/boilerplate-full/function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include <iostream>
#include <vector>
#include <string>

##USER_CODE_HERE##

int main() {
int size_arr;
std::cin >> size_arr;
std::vector<int> arr(size_arr);
for(int i = 0; i < size_arr; ++i) std::cin >> arr[i];
int result = longestSequence(arr);
std::cout << result << std::endl;
return 0;
}

9 changes: 9 additions & 0 deletions apps/problems/playlist/boilerplate-full/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

##USER_CODE_HERE##

const input = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split('\n').join(' ').split(' ');
const size_arr = parseInt(input.shift());
const arr = input.splice(0, size_arr).map(Number);
const result = longestSequence(arr);
console.log(result);

14 changes: 14 additions & 0 deletions apps/problems/playlist/boilerplate-full/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

use std::io::{self, BufRead};

##USER_CODE_HERE##

fn main() {
let stdin = io::stdin();
let mut input = stdin.lock().lines().map(|l| l.unwrap());
let size_arr: usize = input.next().unwrap().parse().unwrap();
let arr: Vec<i32> = input.take(size_arr).map(|s| s.parse().unwrap()).collect();
let result = longestSequence(arr);
println!("{}", result);
}

4 changes: 4 additions & 0 deletions apps/problems/playlist/boilerplate/function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int longestSequence(list<int> arr) {
// Implementation goes here
return result;
}
4 changes: 4 additions & 0 deletions apps/problems/playlist/boilerplate/function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function longestSequence(arr) {
// Implementation goes here
return result;
}
4 changes: 4 additions & 0 deletions apps/problems/playlist/boilerplate/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn longestSequence(arr: Vec<i32>) -> i32 {
// Implementation goes here
result
}
38 changes: 38 additions & 0 deletions apps/problems/playlist/solutions/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int

void solve() {
int n;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
map<int,int>mp;
int i=0,j=0;
int ans=0;
while(j<n){
mp[a[j]]++;
if(mp.size()==(j-i+1)){
ans=max(ans,j-i+1);
j++;
}else if(mp.size()<(j-i+1)){
while(mp.size()<(j-i+1) and i<=j){
mp[a[i]]--;
if(mp[a[i]]==0)mp.erase(a[i]);
i++;
}
j++;
}
}
cout<<ans;
}

int main(){
int t;
t=1;
// cin>>t;
while(t--){
solve();
}
}
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
1 1 1 1 1 1 1 1 1 1
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
2 2 1 1 2 1 2 1 2 1
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/10.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/11.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5
1 1 3 4 5
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/13.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/14.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
2 3 4 5 2 10 9 8 6 1
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/16.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/17.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
1000000000
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/18.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/19.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
3 3 3 3 5 1 5 1 1 4
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
45 9 37 81 69 99 49 71 90 30
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
372869721 745347772 7844865 407111219 83020106 177963453 122424219 493937023 469468920 316110714
2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/5.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/6.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/7.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/8.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions apps/problems/playlist/tests/inputs/9.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200000
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200000
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
447
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/14.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200000
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/16.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
199998
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/17.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/18.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200000
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
87381
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
148
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3498
1 change: 1 addition & 0 deletions apps/problems/playlist/tests/outputs/9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
65407