We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Search
- Ice Cream Parlor
- Discussions
Ice Cream Parlor
Ice Cream Parlor
+ 0 comments def icecreamParlor(m, arr): for i in range(len(arr)): for j in range(i+1,len(arr)): if arr[i]+arr[j] == m: return [i+1,j+1]
+ 2 comments Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money.
Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.
+ 0 comments JavaScript O(n) Solution
function icecreamParlor(m, arr) { // Write your code here let results = {}; for(let i = 0; i <= arr.length; i++){ if(results.hasOwnProperty(arr[i])) { return [results[arr[i]] + 1, i + 1] } results[m - arr[i]] = i; } }
+ 0 comments rust solution
use std::collections::HashMap; fn icecreamParlor(m: i32, arr: &[i32]) -> Vec<i32> { let mut map = HashMap::new(); arr.iter().enumerate().fold((0, vec![]), |(i, res), (j, &x)| { if res.is_empty() && map.contains_key(&(m - x)) { (i + 1, vec![map[&(m - x)], j as i32 + 1]) } else { map.entry(x).or_insert(j as i32 + 1); (i + 1, res) } }).1 }
+ 0 comments Binary search
vector<int> icecreamParlor(int m, vector<int> arr) { vector<int> res; vector<ii> v; for (int i = 0; i < arr.size(); i++) v.push_back(ii(arr[i], i + 1)); sort(v.begin(), v.end()); int rem, ind; for (int i = 0; i < v.size(); i++){ rem = m - v[i].first; ind = lower_bound(v.begin() + i + 1, v.end(), ii(rem, 0)) - v.begin(); if (v[ind].first == rem){ res.push_back(v[i].second); res.push_back(v[ind].second); break; } } sort(res.begin(), res.end()); return res; }
Load more conversations
Sort 877 Discussions, By:
Please Login in order to post a comment